POJ 3268 Silver Cow Party (双向dijkstra)

题目链接:http://poj.org/problem?id=3268

Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19211   Accepted: 8765

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 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
题目大意:(单向路)有n头牛,他们商量好去x家举行party,需要你帮他们计算出其他人去到x家party结束后回到自己家所需要的最短路程,然后输出最长路程。(每头牛去的路程可能和回家的路程不同)
解题思路:用两遍dijkstra 分别计算出其他牛到x家的最短路径,然后再计算出他们从x家返回到自己家的最短路程,最后找出最长路径
复制代码
  1 #include <stdio.h>
  2 #include <string.h>
  3 #define inf 9999999999
  4 int p1[1010][1010];
  5 int p2[1010][1010];
  6 int vis1[1010];
  7 int vis2[1010];
  8 int dis1[1010];
  9 int dis2[1010];
 10 int n,m,x;
 11 void dijkstra_go()  //计算从自己家到x家所需要的最短路径(注意用的p2数组)反向思维
 12 {
 13     int i,j,pos = 0,minn;
 14     for (i = 1; i <= n; i ++)
 15     {
 16         vis1[i] = 0;
 17         dis1[i] = p2[x][i];
 18     }
 19     vis1[x] = 1;
 20     dis1[x] = 0;
 21 
 22     for (i = 1; i <= n; i ++)
 23     {
 24         minn = inf;
 25         for (j = 1; j <= n; j ++)
 26         {
 27             if (!vis1[j] && dis1[j] < minn)
 28             {
 29                 minn = dis1[j];
 30                 pos = j;
 31             }
 32         }
 33         vis1[pos] = 1;
 34         for (j = 1; j <= n; j ++)
 35         {
 36             if (!vis1[j] && dis1[j] > dis1[pos]+p2[pos][j])
 37                 dis1[j] = dis1[pos]+p2[pos][j];
 38         }
 39     }
 40 }
 41 void dijkstra_back()  //计算从x家回到自己家所需要的最短路径
 42 {
 43     int i,j,pos = 0,minn;
 44     for (i = 1; i <= n; i ++)
 45     {
 46         vis2[i] = 0;
 47         dis2[i] = p1[x][i];
 48     }
 49     vis2[x] = 1;
 50     dis2[x] = 0;
 51     for (i = 1; i <= n; i ++)
 52     {
 53         minn = inf;
 54         for (j = 1; j <= n; j ++)
 55         {
 56             if (!vis2[j] && dis2[j] < minn)
 57             {
 58                 minn = dis2[j];
 59                 pos = j;
 60             }
 61         }
 62         vis2[pos] = 1;
 63         for (j = 1; j <= n; j ++)
 64         {
 65             if (!vis2[j] && dis2[j] > dis2[pos]+p1[pos][j])
 66                 dis2[j] = dis2[pos]+p1[pos][j];
 67         }
 68     }
 69 }
 70 int main ()
 71 {
 72     int a,b,t;
 73     int i,j;
 74     int sum[1010];
 75     while (~scanf("%d%d%d",&n,&m,&x))
 76     {
 77         for (i = 1; i <= n; i ++)
 78         {
 79             for (j = 1; j <= n; j ++)
 80             {
 81                 p1[i][j] = inf;
 82                 p2[i][j] = inf;
 83             }
 84         }
 85 
 86         for (i = 0; i < m; i ++)
 87         {
 88             scanf("%d%d%d",&a,&b,&t);
 89             p1[a][b] = t;
 90             p2[b][a] = t;
 91         }
 92         dijkstra_go();
 93         dijkstra_back();
 94         int maxx = 0;
 95         for (i = 1; i <= n; i ++)
 96         {
 97             if (i == x)
 98                 continue;
 99             sum[i] = dis1[i]+dis2[i];
100             if (maxx < sum[i])
101                 maxx = sum[i];
102         }
103         printf("%d\n",maxx);
104     }
105     return 0;
106 }
View Code
复制代码

 



本文作者:gaoyanliang

本文链接:https://www.cnblogs.com/yoke/p/5866151.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   gaoyanliang  阅读(185)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起