http://poj.org/problem?id=3268

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

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define MAXV 1010
#define inf 0x3f3f3f3fint mp[MAXV][MAXV], d[MAXV], dback[MAXV];
bool vis[MAXV];
int n, m, x;int dijkstra(){int v, mi;for(int i = 1; i <= n; i ++) {vis[i] = 0;d[i] = mp[x][i];dback[i] = mp[i][x];}for(int i = 1; i <= n; i ++){mi = inf;for(int j = 1; j <= n; j ++)if(!vis[j] && d[j] < mi) {v = j;mi = d[j];}vis[v] = 1;for(int j = 1; j <= n; j ++) {if(!vis[j] && mp[v][j] + d[v] < d[j])d[j] = mp[v][j] + d[v];}}memset(vis, 0, sizeof(vis));for(int i = 1; i <= n; i ++) {mi = inf;for(int j = 1; j <= n; j ++)if(!vis[j] && dback[j] < mi){v = j;mi = dback[j];}vis[v] = 1;for(int j = 1; j <= n; j ++){if(!vis[j] && mp[j][v] + dback[v] < dback[j])dback[j] = mp[j][v] + dback[v];}}mi = -1;for(int i = 1; i <= n; i ++) {if(d[i] + dback[i] > mi)mi = d[i] + dback[i];}return mi;
}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) mp[i][j] = inf;else mp[i][j]=0;}for(int i = 1;i <= m; i ++){scanf("%d%d%d", &a, &b, &c);mp[a][b] = c;}printf("%d\n",dijkstra());}return 0;
}

  

转载于:https://www.cnblogs.com/zlrrrr/p/9879164.html

POJ 3268 D-Silver Cow Party相关推荐

  1. POJ 3268:Silver Cow Party 求单点的来回最短路径

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15989   Accepted: 7303 ...

  2. 【POJ - 3268 】Silver Cow Party(Dijkstra最短路+思维)

    题干: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...

  3. D - Silver Cow Party POJ - 3268

    D - Silver Cow Party POJ - 3268 dijkstra 是 O(n2),堆优化一下, O(nlogn) 对每个点跑一次 dj, 取 max(dis(x->i)+dis( ...

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

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

  5. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

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

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

  7. POJ 3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 30924   Accepted: 9536 D ...

  8. POJ 3268 (dijkstra算法)

    Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N  ...

  9. BFS POJ 3278 Catch That Cow

    题目传送门 1 /* 2 BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 3 */ 4 #include <cstdio> 5 #include <iostrea ...

最新文章

  1. Redis键命令(查找键、判断键值是否存在、查看键值类型、删除键值、设置过期时间、查看键值有效时间)
  2. 据说腾讯的算法高手都是C++背景
  3. Javascript:字符串分割split()妙用
  4. VTK:PolyData之ColoredPoints
  5. python编程语言一览_编程语言学习:python有趣用法汇总
  6. 这一大波电子“艺术”图,美翻了!
  7. .NET | 多线程下的调用上下文 : CallContext
  8. YUM只下载软件不安装的设定
  9. HDU2178 猜数字【数学计算+水题】
  10. R语言重抽样与自助法
  11. spring @Transactional注解的使用和失效场景
  12. 自抗扰控制理论(一)ADRC的原理
  13. (2)香橙派+apache2与php+天猫精灵=自建平台语音支持--香橙派操作系统安装
  14. 从word中复制图片到网页编辑器中
  15. 如何用计算机做样本抽样,抽样设计和样本抽选.doc
  16. 什么?HomeKit、米家、Aqara等生态也能通过智汀与天猫精灵生态联动?
  17. python和excel数据分析有什么区别_用Excel、SQL、Python做数据分析有何不同?
  18. 报错笔记-[error] 1615#0: *14 open() “/usr/local/nginx/html/course/zk.jpg“ failed (2: No such file or dir
  19. Android端实现多人音视频聊天应用(一)
  20. [句型] 二十六、特殊疑问句 3

热门文章

  1. js校验复选框(多选按钮)是否被选中的方法
  2. Docker的安装、镜像源更换与简单应用
  3. OpenCV 笔记(06)— Mat 结构、像素值存储方法、创建 Mat 对象各种方法、Mat 对象的运算
  4. 顺时针打印矩阵 python
  5. 业余时间用哪里,哪里就有发展的可能
  6. 【Spring】基于xml实现事务控制(银行转账)
  7. 深度学习数据集定义与加载
  8. 2021年大数据Hadoop(二十五):YARN通俗介绍和基本架构
  9. VsCode允许JSON文件注释
  10. [JAVA EE] Filter过滤器