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. 【小结】有关mysql扩展库和mysqli扩展库的crud操作封装

    现阶段php如果要操作mysql数据库 php给我们提供了3套库 1.mysql扩展库   面向过程操作 2.mysqli扩展库  面向对象操作和面向过程操作并存  安全性和效率高于mysql扩展库 ...

  2. 无Xaml的WPF展示

    我们创建一个wpf应用程序,我们把里面的xaml文件全部删除,添加一个新类: 如下图: 然后我们cs文件中的代码: using System; using System.Collections.Gen ...

  3. IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等

    // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affec ...

  4. Hive的MoveTask错误

    最近在部署Hive上线,结果在线上线下同时出现了MoveTask报错的现象,虽然两者错误的日志以及错误信息一样,但是经过分析解决又发现两者的原因是不一样的. 首先线下的错误日志: 2015-05-18 ...

  5. kernel编程规范

    1. 制表符8个空格2. 每行最长80字符3. 代码块的{放在首行,但是函数的{放在次行4. 只有一行的if块,不加{}5. 不在()前后加空格6. 正常关键字后加一个空格,if, switch, c ...

  6. BZOJ 3676 回文串

    Description 考虑一个只包含小写拉丁字母的字符串\(s\).我们定义\(s\)的一个子串\(t\)的"出现值"为\(t\)在\(s\)中的出现次数乘以\(t\)的长度.请 ...

  7. Semaphore的介绍和使用

    转自:http://www.itzhai.com/the-introduction-and-use-of-semaphore.html 相关介绍 public class Semaphore exte ...

  8. quartz定时任务时间配置

    quartz定时任务时间设置描述(2011-03-03 16:23:50)转载▼标签: quartz时间it 分类: 凌乱小记  这些星号由左到右按顺序代表 :     *    *     *    ...

  9. CountDownLatch的使用

    CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.主要方法public CountDownLatch(int count);publi ...

  10. C语言中结构体对齐问题

    C语言中结构体对齐问题 收藏 关于C语言中的结构体对齐问题 1,比如: struct{short a1;short a2;short a3;}A;struct{long a1;short a2;}B; ...