poj 3268 最短路

来源:互联网 发布:opengl游戏编程 编辑:程序博客网 时间:2024/04/28 00:33

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

Line 1: Three space-separated integers, respectively: NM, and X
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi, requiring Titime units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3

Sample Output

10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
题目大意:块田中(1≤N≤1000)都有1只牛去参加盛大的母牛聚会,这个聚会被安排在X号田(1≤X ≤N)。一共有M( 1≤M≤100,000)条单行道分别连接着两块田,且通过路i需要花Ti(1≤Ti≤100)的时间。每头母牛必需参加宴会并且在宴会结束时回到自己的田地,但是每头牛都很懒而喜欢选择化是最少的一个方案。来时的路和去时的可能不一样。求每头牛要来回的最短时间。找出来回路程最长的。输出来回最长的距离

简单思路:先用spfa算法求出x为源的最短路径,然后,将图中所有的边进行反向,在做一次spfa算法,那么两次的和即为所求。去最大的一组即可。另一个思路就是用Floyd算法求多源最短路,那样更简洁明了,但是其时间复杂度为O(n^3)不能承受。

spfa

Accepted1108 KB16 msC++1886 B2014-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;}


0 0
原创粉丝点击