Silver Cow Party  

Time Limit(Common/Java):2000MS/20000MS     Memory Limit:65536KByte
Total Submit: 58            Accepted: 28

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: 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 farmBi, 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.

Source

USACO February 2007

思路:对原图求一次最短路,再对反图进行一次最短路,然后找最求解即可.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
const int maxn = 1100;
const int maxm = 101000;
const int INF = 0x7fffffff;
vector<pair<int, int> > g[maxn];
vector<pair<int, int> > rg[maxn];//reverse;
int gn, gm, x;
bool inque[maxn];
queue<int> Q;
int d[maxn];//保存原图最短径.
int rd[maxn];//保存反图的最短路径值. void spfa2(int s) {
int i;
memset(inque, false, sizeof(inque));
for(i = 1; i < maxn; i++) rd[i] = INF;
rd[s] = 0;
while(!Q.empty()) Q.pop();
Q.push(s);
inque[s] = true;
while(!Q.empty()) {
int u = Q.front();
Q.pop();
for(i = 0; i < (int)rg[u].size(); i++) {
int t = rg[u][i].first;
if( rd[u] != INF && rd[u] + rg[u][i].second < rd[t]) {
rd[t] = rd[u] + rg[u][i].second;
if(!inque[t]) {
inque[t] = true;
Q.push(t);
}
}
}
inque[u] = false;
}
} void work(const int s) {
int i;
int maxv = -1;
for(i = 1; i <= gn; i++) {
if(i != s) {
if(d[i] + rd[i] > maxv) {
maxv = d[i] + rd[i];
}
}
}
printf("%d\n", maxv);
} void spfa1(int s) {
int i;
memset(inque, false, sizeof(inque));
for(i = 1; i < maxn; i++) d[i] = INF;
d[s] = 0;
while(!Q.empty()) Q.pop();
Q.push(s);
inque[s] = true;
while(!Q.empty()) {
int u = Q.front();
Q.pop();
for(i = 0; i < (int)g[u].size(); i++) {
int t = g[u][i].first;
if(d[u] != INF && d[u] + g[u][i].second < d[t]) {
d[t] = d[u] + g[u][i].second;
if(!inque[t]) {
inque[t] = true;
Q.push(t);
}
}
}
inque[u] = false;
}
} int main()
{
int i;
int x, y, w;
int start;
pair<int, int> t;
while(scanf("%d%d%d", &gn, &gm, &start) != EOF) {
for(i = 1; i <= gn; i++) {
g[i].clear();
rg[i].clear();
}
for(i = 0; i < gm; i++) {
scanf("%d%d%d", &x, &y, &w);
t.first = y;
t.second = w;
g[x].push_back(t);
t.first = x;
t.second = w;
rg[y].push_back(t);
}
spfa1(start);
spfa2(start);
work(start);
}
return 0;
}

最新文章

  1. js拖拽效果实现
  2. myBatis,Spring,SpringMVC三大框架ssm整合模板
  3. 【POJ 2503】Babelfish(字符串)
  4. nodejs 执行shell 命令
  5. WEB开发中常用的正则表达式
  6. JDBC之一:JDBC快速入门
  7. php将unicode编码转为utf-8方法
  8. C#动态增加边框
  9. poj 3897 Maze Stretching 二分+A*搜索
  10. ASP.NETwindows身份验证详细步骤-域验证登录
  11. JVM-2.Class文件结构
  12. Boyer-Moore Majority Vote Algorithm
  13. vue各种插件汇总
  14. Busybox的syslogd认识与使用
  15. JavaScript&#183;cookie
  16. 如何用 Node.js 和 Elasticsearch 构建搜索引擎
  17. css+js杂记
  18. CF487E Tourists 圆方树、树链剖分
  19. [CodeVS4633][Mz]树链剖分练习
  20. Oracle11g温习-第十三章:索引

热门文章

  1. HTML5 自适应rem布局
  2. 桂电在线-转变成bootstrap版2(记录学习bootstrap)
  3. quick-x 计时器的写法
  4. 运用预加载提升H5移动页面的用户体验
  5. 存储过程修改产品描述页图片alt描述信息
  6. python运维开发之第二天
  7. about Red_Hat_Enterprise_Linux_7
  8. Hibernate 注解 字段不映射的注解
  9. 【HDU1538】A Puzzle for Pirates(经典的海盗问题)
  10. 【zz】C++中struct与class的区别