2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛  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≤100000as 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

http://blog.csdn.net/yo_bc/article/details/77917288:

给定一个有向无环图,求最长路径。
由于是有向无环图,所以最长路肯定是一个入度为0到出度为0的路径,拓扑排序在确定当前点之前能够考虑到所有到它的情况,所以最后取个最值即可。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=1e4+;
vector<pair<int,int> >edge[maxn];
int T,n,m;
int deg[maxn],val[maxn];
queue<int> q; void init()
{
for(int i=;i<=n;i++){
edge[i].clear();
}
memset(val,,sizeof(val));
memset(deg,,sizeof(deg));
} void toposort()
{
while(!q.empty()) q.pop();
for(int i=;i<=n;i++){
if(!deg[i]) q.push(i);
}
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=;i<edge[u].size();i++){
int v=edge[u][i].first;
val[v]=max(val[v],val[u]+edge[u][i].second);
if(--deg[v]==) q.push(v);
}
}
printf("%d\n",*max_element(val+,val++n));
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
int u,v,w;
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
edge[u].push_back(make_pair(v,w));
deg[v]++;
}
toposort();
}
return ;
}

最新文章

  1. http 301和302的区别
  2. Python基于websocket实时通信的实现—GoEasy
  3. ecshop 重置后台密码 MD5+salt
  4. Swift 中的函数(下)
  5. Thinkphp关闭缓存方法总结(转)
  6. FreeDroid开发过程中遇到的一些问题
  7. iOS开发——图形编程Swift篇&amp;CAShapeLayer实现圆形图片加载动画
  8. Qt学习博客推荐
  9. leetcode先刷_Maximum Subarray
  10. ASP.NET MVC 开发微信支付H5(外置浏览器支付)
  11. python打包压缩文件夹zip+组装文件夹
  12. Redis主从和HA配置
  13. spring 普通类注入为null,通过自定义SpringUtils解决
  14. liunx本地网卡流量监控
  15. MIP是什么
  16. linux下切换python2和python3(转)
  17. IntelliJ IDEA连接cvs超时Error refreshing view: Timeout while trying to connect to host
  18. iframe中的历史记录问题汇总及解决方案[转]
  19. Zedboard学习(一):移植Ubuntu桌面操作系统 标签: ubuntu移植zedboardFPGA 2017-07-04 21:53 26人阅读
  20. IRC程序学习

热门文章

  1. c# 调用7za.exe执行压缩命令
  2. LUOGU P3539 [POI2012]ROZ-Fibonacci Representation
  3. 玩转webpack之webpack的entry output
  4. canvas用2d渲染出3d的感觉
  5. MySQL:比较两个数据表不同部分
  6. springmvc 支持对象与json 自动转换的配置
  7. Hibernate_条件查询客户列表
  8. codevs1214 线段覆盖
  9. web前端学习(四)JavaScript学习笔记部分(1)-- JavaScript基础教程
  10. Python运用于数据分析的简单教程