您的位置:首页 > 理论基础 > 计算机网络

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

2017-09-09 21:01 393 查看
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



题目来源

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 100005
using 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐