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

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

Each of the following M lines contains three integers Si​, Ti​, and Li​ (0

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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair
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 G[N];
int in[N];
int dis[N];
int n,m;

void topSort() {
    stack 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

 

你可能感兴趣的:(#,图论——DAG图最长路,#,其它,OJ)