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

来源:互联网 发布:数据站点应当互不相同 编辑:程序博客网 时间:2024/05/05 10:16

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_iSi-th flag to the T_iTi-th flag has length L_iLi.

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<N10000 and 0 < M \leq 1000000<M100000as described above.

Each of the following MM lines contains three integers S_iSiT_iTi, and 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.

样例输入

15 41 3 32 3 43 4 13 5 2

样例输出

6

题目来源

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


题意:有向图求最长路

解题思路:DP求最长路,用到了Floyd的思想,把每一点的最长路都求一遍,记录最大即可


#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>#define INF 1<<29#define MAXN 100005using namespace std;const int MAXV=100005;int max(int a,int b){   return a>b?a:b;}int dp[100005];struct edge{    int v1,v2,w,next;}e[MAXV];int edge_num;int head[MAXV];void insert_edge(int v1,int v2,int w){    e[edge_num].v1=v1;    e[edge_num].v2=v2;    e[edge_num].w=w;    e[edge_num].next=head[v1];    head[v1]=edge_num++;}int dr(int i){    if(dp[i])        return dp[i];    for(int j=head[i];j!=-1;j=e[j].next)            dp[i]=max(dp[i],dr(e[j].v2)+e[j].w);    return dp[i];}int main() {    int t;    scanf("%d",&t);    while(t--){        int a,m;        memset(dp,0,sizeof(dp));        edge_num=0;        memset(head,-1,sizeof(head));        scanf("%d%d",&a,&m);        int t1,t2,t3;        for(int i=0;i<m;i++){            scanf("%d%d%d",&t1,&t2,&t3);            insert_edge(t1,t2,t3);        }        int ans=0;        for(int i=1;i<=a;i++)            ans=max(ans,dr(i));        printf("%d\n",ans);    }    return 0;}


阅读全文
0 0
原创粉丝点击