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;
}

TOJ1693(Silver Cow Party)的更多相关文章

  1. POJ3268(Silver Cow Party)

    题意: 有n头牛去第x个点开party(有点高大上~),单向路,去到还得回来,问这n头牛每一头花费的总时间的最大值是多少 模板spfa: #include <iostream> #incl ...

  2. Silver Cow Party(最短路,好题)

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

  3. POJ 3268 Silver Cow Party (双向dijkstra)

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

  4. poj 3268 Silver Cow Party(最短路)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17017   Accepted: 7767 ...

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

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

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

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

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

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

  8. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:28457   Accepted: 12928 ...

  9. Silver Cow Party---poj3268(最短路,迪杰斯特拉)

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

随机推荐

  1. c++中的vector原理

    vectorvector就是动态数组.它也是在堆中分配内存,元素连续存放,有保留内存,如果减少大小后,内存也不会释放.如果新值>当前大小时才会再分配内存. 它拥有一段连续的内存空间,并且起始地址 ...

  2. python中跟字符串相关的一些操作

    公司让用python自动生成代码,以前没看过python.所以匆匆的看了两天python就连猜带蒙就上马开干了..因此好多操作可能看的时候看懂了,用的时候知道有这么个东西,具体用法就忘记了..用到了就 ...

  3. UFLDL教程(五)之self-taught learning

    这里所谓的自学习,其实就是利用稀疏自编码器对无标签样本学习其特征 该自学习程序包括两部分: 稀疏自编码器学习图像特征(实现自学习)---用到无标签的样本集 softmax回归对样本分类---用到有标签 ...

  4. 重构第三天:提升方法&下移方法

    如果两个子类拥有相同的方法,把它们移动到超类中来 Warning! 但是从子类到父类移动方法的时候要注意, 不要过度采用这种方法,否则会出现继承滥用的情况.如果一个父类A有多个方法,假设m1, m2方 ...

  5. MySQL数据表修复, 如何修复MySQL数据库(MyISAM / InnoDB)

    常用的Mysql数据库修复方法有下面3种: 1. mysql原生SQL命令: repair 即执行REPAIR TABLE SQL语句 语法:REPAIR TABLE tablename[,table ...

  6. [LeetCode#12] Roman to Integer

    Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...

  7. vijosP1779国王游戏

    题目:https://vijos.org/p/1779 题解:忽然想起来我好像还没写过高精度除以单精度,于是拿这题练练手...没想到1A了... 代码: #include<cstdio> ...

  8. 4G来临 IT业转型之路当在不远

    摘 要:4G商用未启,品牌营销争夺已经展开.目前,除了中国移动推出全新4G品牌“andM”之外,中国电信和中国联通均选择继续沿用3G的品牌. 4G商用未启,品牌营销争夺已经展开.12月10日,中国电信 ...

  9. stand meeting

    “每日站立会议”.每日站立会议有一些具体的指导原则: 会议准时开始. (没有特殊情况10点开始) 欢迎所有人参加,但只允许有实际工作的团队成员发言. 不论团队规模大小,会议被限制在15分钟.(每个人2 ...

  10. MIPI D-PHY 总结

    Operating Modes: Control, High-Speed, and Escape 1.The Lane is only in High-Speed mode during Data b ...