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
  1. Sample Input
  2.  
  3. Sample Output

题意:

  有编号为1-N的牛,它们之间存在一些单向的路径。给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求这些牛中所花的最长的来回时间是多少。

  输入第一行n,m,x   表示n头牛 m条路  起点x

思路:

  每头牛返回的最短时间很简单就可以算出来,这相当于从目标牛为起点求单源最短路径。

  但每头牛出发到目标牛的最短时间无法直接算出来,稍微转换一下,发现这个最短时间其实可以通过把矩阵转置,然后再从目标牛求一次单源最短路径得到。

  得到这两个最短路径之后,取它们的和的最大者即可。

  转置:假如起点是1,从1到2是2km,从2到1是3km,回来的时候是从1到2是2km,这个不用转置。去的时候从2到1是3km,转置后从1到2是3km,从2到1是2km,我们要的是从1到2这一个。

代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #define MAX 0x3f3f3f3f
  5. using namespace std;
  6. int map[][];//矩阵存储信息
  7. int n,m,s;
  8. int come[],dis[];//come表示回来的最短路,dis表示回来的最短路
  9. int logo[];//标记数组
  10. void dijkstra()
  11. {
  12. int min,k;
  13. memset(logo,,sizeof(logo));
  14. for(int i=; i<=n; ++i)
  15. dis[i]=map[s][i];
  16. dis[s]=;
  17. logo[s]=;
  18. for(int i=; i<=n; ++i)
  19. {
  20. min=MAX;
  21. for(int j=; j<=n; ++j)
  22. {
  23. if(!logo[j]&&dis[j]<min)
  24. {
  25. min=dis[j];
  26. k=j;
  27. }
  28. }
  29. logo[k]=;
  30. for(int j=; j<=n; ++j)
  31. if(!logo[j]&&dis[j]>dis[k]+map[k][j])
  32. dis[j]=dis[k]+map[k][j];
  33. }
  34. }
  35. int main()
  36. {
  37. scanf("%d%d%d",&n,&m,&s);
  38. int sum=;
  39. for(int i=; i<=n; i++)
  40. for(int j=; j<=n; j++)
  41. map[i][j]=MAX;
  42. for(int i=; i<m; i++)
  43. {
  44. int a,b,c;
  45. scanf("%d%d%d",&a,&b,&c);
  46. if(c<map[a][b])
  47. map[a][b]=c;
  48. }
  49. dijkstra();
  50. for(int i=; i<=n; i++)
  51. come[i]=dis[i];//计算回来的最短路,然后用come记录下俩
  52. for(int i=; i<=n; i++)//将矩阵转置,表示从终点回来
  53. for(int j=i+; j<=n; j++)
  54. {
  55. int c;
  56. c=map[j][i];
  57. map[j][i]=map[i][j];
  58. map[i][j]=c;
  59. }
  60. dijkstra();
  61. for(int i=; i<=n; i++)
  62. {
  63. if(sum<come[i]+dis[i]&&i!=s)
  64. sum=come[i]+dis[i];//计算最大值
  65. }
  66. printf("%d\n",sum);
  67. return ;
  68. }

POJ3268 Silver Cow Party Dijkstra最短路的更多相关文章

  1. POJ3268 Silver Cow Party(dijkstra+矩阵转置)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15156   Accepted: 6843 ...

  2. POJ3268 Silver Cow Party【最短路】

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

  3. poj3268 Silver Cow Party(最短路)

    非常感谢kuangbin专题啊,这道题一开始模拟邻接表做的,反向边不好处理,邻接矩阵的话舒服多了. 题意:给n头牛和m条有向边,每头牛1~n编号,求所有牛中到x编号去的最短路+回来的最短路的最大值. ...

  4. POJ3268 Silver Cow Party —— 最短路

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  5. 【POJ - 3268 】Silver Cow Party (最短路 Dijkstra算法)

    Silver Cow Party Descriptions 给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x, ...

  6. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

  7. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  8. POJ_3268 Silver Cow Party 【最短路】

    一.题面 POJ3268 二.分析 该题的意思就是给定了一个由每个节点代表农场的有向图,选定一个农场X办party,其余农场的都要去,每个农场的cow都走最短路,走的时间最久的cow耗时多少. 了解题 ...

  9. POJ 3268 Silver Cow Party 单向最短路

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22864   Accepted: 1044 ...

随机推荐

  1. vue全家桶(Vue+Vue-router+Vuex+axios)(Vue+webpack项目实战系列之二)

    Vue有多优秀搭配全家桶做项目有多好之类的咱就不谈了,直奔主题. 一.Vue 系列一已经用vue-cli搭建了Vue项目,此处就不赘述了. 二.Vue-router Vue的路由,先献上文档(http ...

  2. WebService小记

    这个问题找了好多地方都没有结果,自己暂且总结一下吧,也不算是解决问题的根本途径,但是也不失为一种办法.当时用了wsimport  wsdl2java xfire 都没有解决,大牛能解决的话,欢迎留言. ...

  3. JAVA基础——变量和常量

    JAVA的变量和常量知识总结 一.认识java标识符 标识符就是用于给 Java 程序中变量.类.方法等命名的符号. 使用标识符时,需要遵守几条规则: 1.  标识符可以由字母.数字.下划线(_).美 ...

  4. CentOS7.2上用KVM安装虚拟机window10踩过的坑

    最近两个星期一直在琢磨kvm安装window10操作系统,并且通过桥接模式与外界通信,经历了九九八十一难,终于搞定.下面就记录以下我们在探索的过程中踩过的坑. 安装KVM 1. 系统要求:需要一台可以 ...

  5. 【OGG】OGG基础知识整理

    [OGG]OGG基础知识整理 一.GoldenGate介绍 GoldenGate软件是一种基于日志的结构化数据复制软件.GoldenGate 能够实现大量交易数据的实时捕捉.变换和投递,实现源数据库与 ...

  6. InstallShield -6109

    背景:C#项目打包生成时一直提示生成失败,消息号-6109, 查找了好多资料均未能解决,有说ActiveX问题,有说注册表问题,作了相应修改依然未果:后来翻来翻去看到有关User32.dll引用时失败 ...

  7. jzoj3760. 【BJOI2014】Euler

    题目大意: 欧拉函数  φ(n)  定义为不超过正整数 n 并且与 n 互素的整数的数目. 可以证明 φ(n) =  n ∗ ∏ (1 − 1 / pi). 其中 pi(1 <= i <= ...

  8. java多线程系列(二)

    对象变量的并发访问 前言:本系列将从零开始讲解java多线程相关的技术,内容参考于<java多线程核心技术>与<java并发编程实战>等相关资料,希望站在巨人的肩膀上,再通过我 ...

  9. Android系统--输入系统(十六)APP跟输入系统建立联系_InputChannel和Connection

    Android系统--输入系统(十六)APP跟输入系统建立联系_InputChannel和Connection 0. 核心:socketpair机制 1. 回顾Dispatch处理过程: 1.1 放入 ...

  10. css display:box 新属性

    一.display:box; 在元素上设置该属性,可使其子代排列在同一水平上,类似display:inline-block;. 二.可在其子代设置如下属性 前提:使用如下属性,必须在父代设置displ ...