Skiing( 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 )
2017-09-13 21:47
399 查看
In this winter holiday, Bob has a plan for skiing at the mountain resort.
This ski resort has MM
different ski paths and NNdifferent
flags situated at those turning points.
The iii-th
path from the SiS_i-th
flag to the TiT_i-th
flag has length LiL_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.
there are TTcases.
In each test case, the first line contains two integers NN
and MM
where 0<N≤100000 < N \leq 100000<N≤10000
and 0<M≤1000000 < M \leq 1000000<M≤100000
as described above.
Each of the following MMM
lines contains three integers SiS_i,TiT_i,
and Li (0<Li<1000)L_i~(0 < L_i < 1000)Li (0<Li<1000)
describing a path in the ski resort.
ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
1.拓扑排序。
BFS
This ski resort has MM
different ski paths and NNdifferent
flags situated at those turning points.
The iii-th
path from the SiS_i-th
flag to the TiT_i-th
flag has length LiL_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 T, indicating thatthere are TTcases.
In each test case, the first line contains two integers NN
and MM
where 0<N≤100000 < N \leq 100000<N≤10000
and 0<M≤1000000 < M \leq 1000000<M≤100000
as described above.
Each of the following MMM
lines contains three integers SiS_i,TiT_i,
and Li (0<Li<1000)L_i~(0 < L_i < 1000)Li (0<Li<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
题目来源
2017ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
1.拓扑排序。
BFS
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <vector> #define inf 0x3f3f3f #define ms(x) memset(x,0,sizeof(x)) #define mf(x) memset(x,-1,sizeof(x)) using namespace std; const int N = 20002; struct node{ int y,z; node() {} node(int ty, int tz): y(ty), z(tz){} }; vector<node>q ; int in ; int f ; int main(){ int T; int n,m; scanf("%d",&T); while(T--){ queue<int>que; ms(in); ms(f); int ans = 0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) q[i].clear(); for(int i=0;i<m;i++){ int x, y, z; scanf("%d%d%d",&x,&y,&z); q[x].push_back(node(y,z)); in[y]++; } for(int i=1;i<=n;i++){ if(!in[i]) que.push(i); } while(!que.empty()){ int cur = que.front(); que.pop(); for(int i=0;i<q[cur].size();i++){ int v = q[cur][i].y; int w = q[cur][i].z; f[v] = max(f[v],f[cur]+w); ans = max(ans, f[v]); in[v]--; if(!in[v]) que.push(v); } } cout<<ans<<endl; } return 0; }
相关文章推荐
- H. Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
- 计蒜客-2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-H-Skiing
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H. Skiing
- 计蒜客 16957 Skiing(2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H)
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 E.Half-consecutive Numbers
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
- 计蒜客-2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-A-Banana
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 F island
- 计蒜客 16959 Our Journey of Dalian Ends(2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 J)
- [计蒜客16951] Out-out-control cars [2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 B]
- A. Banana 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 C. Coconut
- 【2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 】H Skiing 【求DAG图的最长路】
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 C coconut
- [计蒜客16959] Colored Graph [2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 J]
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H (简单DP)
- E. Half-consecutive Numbers 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
- 计蒜客-2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-C-Coconut
- A. Banana 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 E. Half-consecutive Numbers