http://vjudge.net/contest/view.action?cid=48211#problem/C
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Titime units to traverse.
Output
Sample Input
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3
Sample Output
10
Hint
简单思路:先用spfa算法求出x为源的最短路径,然后,将图中所有的边进行反向,在做一次spfa算法,那么两次的和即为所求。去最大的一组即可。另一个思路就是用Floyd算法求多源最短路,那样更简洁明了,但是其时间复杂度为O(n^3)不能承受。
spfa
Accepted | 1108 KB | 16 ms | C++ | 1886 B | 2014-06-21 20:54:59 |
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=1e5+1;
const int INF=1e9;
struct node
{
int b;
int w;
int next;
};
node edge[maxn];
int s;
int n;
int head[maxn];
int que[maxn];
int visit[maxn];
int dis[maxn];
int num[maxn];
int ip;
void add(int u,int v,int c)
{
edge[ip].b=v; edge[ip].w=c; edge[ip].next=head[u]; head[u]=ip++;
}
void spfa(int start,int numpoint)
{
memset(visit,0,sizeof(visit));
for(int i=0;i<=numpoint;i++)
dis[i]=INF;
int front=-1,tail=-1;
dis[start]=0; visit[start]=1; que[++tail]=start;
int top,to;
int temp;
while(front!=tail)
{
if(++front>numpoint) front-=numpoint;//循环队列
top=que[front]; visit[top]=0;
for(int p=head[top];p!=-1;p=edge[p].next)
{
to=edge[p].b; temp=dis[top]+edge[p].w;
if(dis[to]>temp)
{
dis[to]=temp;
if(!visit[to])
{
if(++tail>numpoint) tail-=numpoint;
que[tail]=to;
visit[to]=1;
}
}
}
}
}
int a[1000005][3];
int main()
{
int n,m,x,u,v,w;
while(~scanf("%d%d%d",&n,&m,&x))
{
memset(head,-1,sizeof(head));
ip=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a[i][0],&a[i][1],&a[i][2]);
add(a[i][0],a[i][1],a[i][2]);
}
spfa(x,n);
for(int i=1;i<=n;i++)
if(i!=x)
num[i]=dis[i];
memset(head,-1,sizeof(head));
ip=0;
for(int i=1;i<=m;i++)
add(a[i][1],a[i][0],a[i][2]);
spfa(x,n);
int maxx=-INF;
for(int i=1;i<=n;i++)
if(i!=x)
maxx=max(maxx,dis[i]+num[i]);
printf("%d\n",maxx);
}
return 0;
}
folyd (TLE)
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int oo=1e9+1;
int map[1005][1004];
void prepare()
{
for(int i=0;i<1005;i++)
for(int j=0;j<1005;j++)
if(i!=j)
map[i][j]=map[j][i]=oo;
}
int main()
{
int m,n,x,u,v,w;
while(~scanf("%d%d%d",&n,&m,&x))
{
prepare();
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
map[u][v]=w;
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
int maxx=-oo;
for(int i=1;i<=n;i++)
if(x!=i)
maxx=max(maxx,map[x][i]+map[i][x]);
printf("%d\n",maxx);
}
return 0;
}