poj 3268 Silver Cow Party Dijkstra 和SPFA

来源:互联网 发布:淘宝达人大v认证难不难 编辑:程序博客网 时间:2024/04/29 19:06

 一开始做这个题目,很快写完,结果超时,我傻了,居然是n的3次方。。后来无语教我简化了,过了。。。

  题目大意就是给出N个点和M条边,每两个点之间是单行道,来回路长不一定相等,给定一个确定的点,算所有点中去这个确定的点再回去的最短路径中的最大值。

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 7080 Accepted: 3089

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 Ti time 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

还是用Dijkstra,加上他的转置就可以了。。

代码:

#include <stdio.h>#include <iostream>#include <algorithm>#include <stdlib.h>#include <string.h>#define INF 99999999;using namespace std;int vis[1001];int map1[1001][1001],map2[1001][1001];int d1[1001],d2[1001],n;void Dijkstra1 (int s,int d[],int map[][1001]){    int i,j,m,p;    memset(vis,0,sizeof(vis));    for(i=0;i<n;i++)    {        d[i]=map[s][i];    }    for(i=0;i<n;i++)    {        m=INF;        p=-1;        for(j=0;j<n;j++)        {            if(vis[j]==0 && d[j]<m)            {                m=d[j];                p=j;            }        }        if(p==-1) break;        vis[p]=1;        for(j=0;j<n;j++)        {            if(vis[j]==0 && d[j]>map[p][j]+d[p])            {                d[j]=map[p][j]+d[p];            }        }    }}int main(){    int m,i,j,k,q,a,b,t,max;    while(scanf("%d%d%d",&n,&m,&q)!=EOF)    {        for(i=0;i<n;i++)        {            for(j=0;j<n;j++)            {                if(i==j){map1[i][j]=0;map2[j][i]=0;}                else{map1[i][j]=INF;    map2[j][i]=INF;}            }        }        for(i=0;i<m;i++)        {            scanf("%d%d%d",&a,&b,&t);            a--;            b--;map1[a][b]=min(t,map1[a][b]);map2[b][a]=min(t,map2[b][a]);  //map2  为map1的转置。        }        Dijkstra1(q-1,d1,map1);        Dijkstra1(q-1,d2,map2);max=0;for(i=0;i<n;i++){if(max<d1[i]+d2[i])max=d1[i]+d2[i];//printf("...%d...%d..\n",d1[i],d2[i]);}        printf("%d\n",max);    }}

中午的时候又写了一下这道题目的spfa算法  ,还挺简单,无语说这个速度跟快,尤其是在边少的时候。。

代码:

#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <queue>#define INF 999999999using namespace std;int vis[1001];int d1[1001],d2[1001];int map1[1001][1001],map2[1001][1001],n;void spfa(int s,int d[],int map[][1001])  //出发点s。{    memset(vis,0,sizeof(vis));    queue<int> q;    int i,j,m,p;    for(i=0;i<n;i++)  //一开始付初值,这个我原来写的是Dijkstra那个付初值,后来老错。    {        d[i]=INF;    }while(!q.empty())   //清空队列q.pop();    q.push(s);    //初始化d[s]=0;    while(!q.empty())    {        int now=q.front();        q.pop();vis[now]=0;        for(i=0;i<n;i++)        {            if(d[i]>map[now][i]+d[now])    //路径松弛。            {                d[i]=map[now][i]+d[now];                if(vis[i]==0)        //判断是否在队列中,不在,压入队列。                {                    q.push(i);                    vis[i]=1;                }            }        }    }    return ;}int main(){    int m,i,j,a,b,t,q;    while(scanf("%d%d%d",&n,&m,&q)!=EOF)    {        for(i=0;i<n;i++)        {            for(j=0;j<n;j++)            {                if(i==j)                {                    map1[i][j]=0;                    map2[j][i]=0;                }                else                {                    map1[i][j]=INF;                    map2[i][j]=INF;                }            }        }        for(i=0;i<m;i++)        {            scanf("%d%d%d",&a,&b,&t);            a--;            b--;            map1[a][b]=__min(t,map1[a][b]);            map2[b][a]=__min(t,map2[b][a]);        }       spfa(q-1,d1,map1);       spfa(q-1,d2,map2);       int max=0;       for(i=0;i<n;i++)       {           if(max<d1[i]+d2[i])           {               max=d1[i]+d2[i];           }//   printf("....%d....%d..\n",d1[i],d2[i]);       }       printf("%d\n",max);    }    return 0;}






原创粉丝点击