# ICPC2017网络赛（乌鲁木齐）H: Skiing （SPFA最长路）

556人阅读 评论(0)

H: Skiing

time limit 1000ms memory limit 131072KB

i i
In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort has M diﬀerent ski paths and N diﬀerent ﬂags situated at those turning points. The i-th path from the S -th ﬂag to the T -th ﬂag has length L . 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 ﬂag, passing through several ﬂags along the paths, and end at another ﬂag. Now, you should help Bob ﬁnd the longest available ski trail in the ski resort. Input Format The ﬁrst line contains an integer T, indicating that there are T cases. In each test case, the ﬁrst line contains two integers N and M where 0 < N ≤ 10000 and 0 < M ≤ 100000 as described above. Each of the following M lines contains three integers S , T , and L  (0 < L < 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. Sample Input
1 5 4 1 3 3 2 3 4 3 4 1 3 5 2
Sample Output
6

【题意】：

【解析】：

【代码】：

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define mset(a,i) memset(a,i,sizeof(a))
using namespace std;
typedef long long ll;
struct node{
int to,val,next;
}e[101010];
int n,m,x,y,val,cnt ;
ll dis[10101];
queue <int> Q ;
{
e[cnt] = (node){ y,v,head[x] } ;
}
void SPFA(int s)
{
int u,v;
while(!Q.empty())Q.pop();
Q.push(s);
while(!Q.empty())
{
u=Q.front() ;
Q.pop() ;
{
v=e[i].to;
if(dis[v]<dis[u]+e[i].val)
{
dis[v]=dis[u]+e[i].val;
Q.push(v);
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
mset(IN,0);
mset(dis,0);
cnt=0;
scanf("%d%d",&n,&m) ;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&val);
IN[y]++;
}
if(n==1)
{
printf("0\n");
continue;
}
for(int i=1;i<=n;i++)
{
if(IN[i]==0)
SPFA(i);
}
ll ans=0;
for(int i=1;i<=n;i++)
{
ans=max(ans,dis[i]);
}
printf("%lld\n",ans);
}
return 0 ;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：42580次
• 积分：1805
• 等级：
• 排名：千里之外
• 原创：125篇
• 转载：9篇
• 译文：0篇
• 评论：28条
博客专栏
 ACM算法 文章：1篇 阅读：275
 ACM荣耀之路 文章：51篇 阅读：19767
阅读排行
评论排行
最新评论