描述:

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 ≤ XN). 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: N, M, and X
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 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

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的每个点到x以及从x回来的最短路之和的最大值。就可以用两次dijkstra算法求解,一次算从i到x,一次算从x到i,最后求得相加的最大值即可。

代码:

#include <iostream>
#include <stdio.h>using namespace std;
#define inf 1<<29
int n,m,x;
bool vis[1010];
int map[1010][1010];
int go[1010],dback[1010];  //go是从i—>x  back是从x—>iint dijkstra()
{int i,j,f,v;for(i=1;i<=n;i++){vis[i]=0;go[i]=map[i][x];dback[i]=map[x][i];}for(i=1;i<=n;i++){f=inf;for(j=1;j<=n;j++){if(!vis[j]&&dback[j]<f){v=j;f=dback[j];}}vis[v]=1;for(j=1;j<=n;j++)if(!vis[j]&&map[v][j]+dback[v]<dback[j])dback[j]=map[v][j]+dback[v];}for(i=1;i<=n;i++) vis[i]=0;for(i=1;i<=n;i++){f=inf;for(j=1;j<=n;j++){if(!vis[j]&&go[j]<f){v=j;f=go[j];}}vis[v]=1;for(j=1;j<=n;j++){if(!vis[j]&&map[j][v]+go[v]<go[j])go[j]=map[j][v]+go[v];}}f=-1;for(i=1;i<=n;i++){if(go[i]+dback[i]>f)f=go[i]+dback[i];}return f;
}int main()
{int a,b,c;while(~scanf("%d%d%d",&n,&m,&x)){for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){if(i!=j) map[i][j]=inf;else map[i][j]=0;}for(int i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);map[a][b]=c;}printf("%d\n",dijkstra());}return 0;
}

转载于:https://www.cnblogs.com/y1040511302/p/10466583.html

poj 3268 Silver Cow Party(最短路dijkstra)相关推荐

  1. POJ - 3268 Silver Cow Party(最短路)

    题目链接:点击查看 题目大意:给出n个点以及m条单项路径和一个点x,设从x点到i的距离及从i回到x点的距离分别为d1和d2,求d1+d2的最大值(1<=i<=n) 题目分析:看到这个题的第 ...

  2. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  3. [POJ](3268)Silver Cow Party ---最短路径(图)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23762   Accepted: 1085 ...

  4. POJ 3268 Silver Cow Party

    题目链接 题意 单向图,N - 1个牛去聚会,求所有牛去聚会和回家路径和的最大值 AC 很骚的操作 首先从派对的地方跑Dijkstra求出回家的最短路,然后将所有边翻转再次从聚会跑Dijkstra就是 ...

  5. POJ 3268 Silver Cow Party--正反Dijkstra

  6. 【POJ】3268 Silver Cow Party (将有向图的边反转)

    问题链接:http://poj.org/problem?id=3268 [问题描述] One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  7. 【POJ】3268 Silver Cow Party

    题目链接:http://poj.org/problem?id=3268 题意 :有N头奶牛,M条单向路.X奶牛开party,其他奶牛要去它那里.每头奶牛去完X那里还要返回.去回都是走的最短路.现在问这 ...

  8. POJ 3268 D-Silver Cow Party

    http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  9. poj 1502 MAPMaelstrom 单源最短路dijkstra

    从第一个点出发,求到其他点最短路的最大值 #pragma warning(disable:4996) #include<iostream> #include<string> # ...

  10. Silver Cow Party (最短路)

    One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...

最新文章

  1. java runnable 启动_Java开发笔记(九十七)利用Runnable启动线程
  2. Winsock异步模式I/O模型WSAEventSelect的使用
  3. Android全局修改字体大小,Android 仿微信全局字体大小调整
  4. 解决Mysql无法导入存在null数据的问题
  5. BASIC-1 闰年判断
  6. Python3 B格注释
  7. 思科c240 m3 服务器安装系统,2U机架式 思科UCS C240 M3让你心动
  8. java实例分析宠物商店_java实例分析:宠物商店.ppt
  9. 随想录(强大的kprobe)
  10. L - Finding the Bases(KMP+dp)
  11. 如何使用USB摄像头搭配Visionpro进行视觉识别
  12. 计算机应用基础题excel,计算机应用基础EXCEL练习题.doc
  13. 计算机开机后无法网络拨号怎样处理,电脑不能拨号上网显示调制解调器已删除怎么办...
  14. 小米深圳通服务器维护中,小米已开通深圳通功能 换手机还能退钱移卡
  15. iOS11 增加的新技能
  16. 静态HTML旅行主题网页设计与实现——联途旅游网服务平台网(39页)html css javascript
  17. 百度回应“柬埔寨吧”为境外赌场招工:情况属实 已报案
  18. 给初学者推荐的10个Python免费学习网站,赶紧收藏吧
  19. Java 埃拉托色尼筛选法
  20. Autodesk Alias AutoStudio 2022 x64

热门文章

  1. RAID磁盘阵列与配置
  2. 红米手机使用应用沙盒动态修改运营商参数
  3. 算法实现 int sqrt(int x) 函数。
  4. ps多种去水印方法与技巧-适合各种水印
  5. jsdroid 教程_ps教程自学平台
  6. MySql创建视图的三种方法
  7. 设置广告类型的html小窗口,网页两边悬浮窗广告代码
  8. 如何操作最快的硬盘对拷工具
  9. 浅谈12306核心模型设计思路和架构设计
  10. 实验设计的道德伦理考量