H.Skiing

In this winter holiday, Bob has a plan for skiing at the mountain resort.

This ski resort has MM different ski paths and NN different flags situated at those turning points.

The ii-th path from the S_iS​i​​-th flag to the T_iT​i​​-th flag has length L_iL​i​​.

Each path must follow the principal of reduction of heights and the start point must be higher than the end point strictly.

An available ski trail would start from a flag, passing through several flags along the paths, and end at another flag.

Now, you should help Bob find the longest available ski trail in the ski resort.

Input Format

The first line contains an integer TT, indicating that there are TT cases.

In each test case, the first line contains two integers NN and MM where 0 < N \leq 100000<N≤10000 and 0 < M \leq 1000000<M≤100000 as described above.

Each of the following MM lines contains three integers S_iS​i​​, T_iT​i​​, and L_i~(0 < L_i < 1000)L​i​​ (0<L​i​​<1000) describing a path in the ski resort.

Output Format

For each test case, ouput one integer representing the length of the longest ski trail.

样例输入

1
5 4
1 3 3
2 3 4
3 4 1
3 5 2

样例输出

6

代码:
#include<bits/stdc++.h>
//#include<regex>
#define db double
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define MP make_pair
#define PB push_back
#define fr(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int N=1e6+;
const int mod=1e9+;
const int MOD=mod-;
const db eps=1e-;
const db pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
typedef pair<int,int> P;
vector<P>g[N];
int a[N],f[N];
queue<int> q;
bool vis[N];
int main()
{
int t;
ci(t);
while(t--)
{
int n,m;
memset(f,, sizeof(f));
memset(a,, sizeof(a));
ci(n),ci(m);
for(int i=;i<=n;i++) g[i].clear();
for(int i=;i<m;i++){
int x,y,z;
ci(x),ci(y),ci(z);
g[x].push_back(P(y,z));
a[y]++;// 入度
}
for(int i=;i<=n;i++){
if(!a[i]) q.push(i); //入度点
}
int ma=;
while(q.size())//按拓扑序来找最长路
{
int u=q.front();
q.pop();
for(int i=;i<g[u].size();i++){
int v=g[u][i].first;
int d=g[u][i].second;
f[v]=max(f[v],f[u]+d);
ma=max(ma,f[v]);
a[v]--;
if(!a[v]) q.push(v);
}
}
pi(ma);
} }
 

最新文章

  1. 转:CentOS, 找不到dump命令:command not found
  2. SNMP报文抓取与分析(一)
  3. innerHTML
  4. Linux cscope命令
  5. Pycharm中的实用功能(网上看到的,感觉还不错)
  6. DataGridView的DataGridViewComboBoxColumn列点击一次,自动处于编辑状态
  7. C++11 多线程 教学(2)
  8. JS学习笔记-数组
  9. 【线段树】【3-21个人赛】【同样的problemB】
  10. HTTPS 中双向认证SSL 协议的具体过程
  11. 在CentOS下安装配置MySQL(转)
  12. Markdown轻量级标记语言
  13. Zabbix实战-简易教程(6)--Server端高可用
  14. React 轮播图实现
  15. leetcode每日刷题计划-简单篇day5
  16. (三)Bootstrap.jar
  17. gcc和gdb使用笔记
  18. Objective-C中的一些特殊的数据类及NSLog的输出格式
  19. linux numastat的理解
  20. day16 常用类(String、StringBuffer、StringBuilder)

热门文章

  1. java 中的阻塞队列
  2. JAVA基础系列(一) 概述与相关概念
  3. Elasticsearch支持的字段类型
  4. MvcPager.dll使用实现无刷新分页以及MvcPager的Nuget程序包实现刷新分页
  5. 从零开始的全栈工程师——js篇2.12(面向对象)
  6. 洛谷P1397 [NOI2013]矩阵游戏(十进制矩阵快速幂)
  7. html的语法 3
  8. 使用FusionCharts创建可更新数据的JavaScript图表
  9. 【转】如何在Git中撤销一切
  10. 【文件拷贝】使用Total Commander Portable拖动拷贝文件,支持队列