Problem Description

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

This ski resort has M different ski paths and N different flags situated at those turning points.

The ii-th path from the Si​-th flag to the Ti​-th flag has length Li​.

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

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

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

Each of the following M lines contains three integers Si​, Ti​, and Li​ (0<Li​<1000) describing a path in the ski resort.

Output

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

Sample Input

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

​​​​​​​Sample ​​​​​​​Output

6

题意:t 组数据,每组给出一个 n 个点 m 条边的带权有向图,问整个图的最长路

思路:DAG 图求最长路,利用拓扑排序来解决即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const int MOD = 1E9+7;
const int N = 50000+5;
const int dx[] = {1,-1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;struct Node{int to,dis;Node(){}Node(int to,int dis):to(to),dis(dis){}
};
vector<Node> G[N];
int in[N];
int dis[N];
int n,m;void topSort() {stack<int > S;for(int i=1; i<=n; i++)if(!in[i])S.push(i);while(!S.empty()) {int x=S.top();S.pop();for(int j=0; j<G[x].size(); j++) {int y=G[x][j].to;dis[y]=max(dis[y],dis[x]+G[x][j].dis);in[y]--;if(!in[y])S.push(y);}}
}int main() {int T;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);memset(in,0,sizeof(in));memset(dis,0,sizeof(dis));for(int i=0; i<=n; i++)G[i].clear();for(int i=1; i<=m; i++) {int x,y,dis;scanf("%d%d%d",&x,&y,&dis);Node temp;temp.to=y;temp.dis=dis;in[y]++;G[x].push_back(temp);}topSort();int res=-INF;for(int i=1;i<=n;i++)res=max(res,dis[i]);printf("%d\n",res);}return 0;
}

Skiing(2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H)相关推荐

  1. 计蒜客-2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题Skiing(拓扑序求DAG最长路)

    题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所以最长路肯定是一个入度为0到出度为0的路径,拓扑序在确定当前点之前能够考虑到所有到它的情况,所以最后取个最值即可. 代码: # ...

  2. 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛B: Out-out-control cars

    问题 B: Out-out-control cars 题目描述 Two out-of-control cars crashed within about a half-hour Wednesday a ...

  3. 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)

    摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...

  4. 计蒜客 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B coin(求乘法逆元)

    Bob has a not even coin, every time he tosses the coin, the probability that the coin's front face u ...

  5. 2010 ACM/ICPC Online-Contest-SCU[四川赛区网络预选赛]

    Problem A.A Simple Problem 比赛时唯一做出来的一道题. 描述: Time limit: 1 second Memory limit: 256 megabytes There' ...

  6. Maximum Flow(2017 ACM-ICPC 亚洲区(西安赛区)网络赛 E)

    Problem Description Given a directed graph with nn nodes, labeled 0,1,⋯,n−1. For each <i, j> s ...

  7. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 E Maximum Flow

    原题解链接:http://blog.csdn.net/kkkkahlua/article/details/78009087 他用的最小割的来求解最大流.认为只要讨论每一个点到0和n-1那个更小哪条边就 ...

  8. 2017acm乌鲁木齐赛区网络赛F题tarjan缩点

    poj1236是问把一棵树变成强联通分量,于是答案就是rudu为0的和出度为0的最大值,因为假设入度为0的多一些,先每个出度为0的连接一个入度为0的,那么还剩一些入度为0的,这时候入度为0的随意连接一 ...

  9. 2014 ACM/ICPC 北京赛区网络赛解题报告汇总

    首页 算法竞赛» 信息聚合 ONLINE JUDGE 书刊杂志 BLOG» 新闻故事» 招聘信息» 投稿须知 2014 ACM/ICPC 北京赛区网络赛解题报告汇总 九月 21st, 2014 | P ...

最新文章

  1. oracle存储过程的基本语法
  2. 图片SIFT特征匹配处理
  3. 数据包格式_理解MQTT协议数据包结构
  4. C/C++信息隐写术(一)之认识文件结构
  5. php自动打印小票_服装店专用小票机自带进销存
  6. Android Dialog用法
  7. AFNetworking到底做了什么?(二)
  8. 关于移动开发的一些meta设置
  9. Unity如何查看当前内存使用情况
  10. MySQL复制一张表数据到另一张新表
  11. 阿里巴巴字体库使用方法
  12. 选哪个:游戏开发和传统软件开发?
  13. 69个经典Java面试题和答案详解,手慢无
  14. 用剥洋葱理解递归逆序输出单链表
  15. C语言自学笔记,第一天
  16. 利用Power BI制作RFM客户分析模型
  17. 2022杭电多校赛第八场
  18. 一文读懂知识图谱的主要技术
  19. 7.5 Java(农夫果园【完结篇】:一个农场,专门种植销售各类水果,在这个系统中需要描述下列水果葡萄、草莓、苹果)
  20. Simulink S-function 学习及使用实例

热门文章

  1. 手把手教你学Python:一文看懂字符串基本操作
  2. 泡沫or趋势?我们到底需要什么样的区块链?
  3. 装了这几个插件后,我不得不给 IDEA 上个防沉迷
  4. 99%程序员不知道的编程必备工具,人工智能助你编程更轻松
  5. Vue-- $attrs与$listeners的详解
  6. Jeecg 平台开发手册下载(20151014)
  7. Oracle开发专题之:分析函数(OVER)
  8. MySQL基础篇(02):从五个维度出发,审视表结构设计
  9. Mac配置炫酷终端以及直接预览Markdown和各类代码
  10. Active Contour Models 主动轮廓模型