TOJ1693(Silver Cow Party)
Silver Cow Party
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
Source
思路:对原图求一次最短路,再对反图进行一次最短路,然后找最求解即可.
#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)的更多相关文章
- POJ3268(Silver Cow Party)
题意: 有n头牛去第x个点开party(有点高大上~),单向路,去到还得回来,问这n头牛每一头花费的总时间的最大值是多少 模板spfa: #include <iostream> #incl ...
- Silver Cow Party(最短路,好题)
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- 【POJ - 3268 】Silver Cow Party (最短路 Dijkstra算法)
Silver Cow Party Descriptions 给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x, ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions:28457 Accepted: 12928 ...
- Silver Cow Party---poj3268(最短路,迪杰斯特拉)
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u De ...
随机推荐
- sae的kvdb使用注意
之前没仔细看,原来sae的kvdb使用一定要先调用初始化函数 $kv = new SaeKV(); $kv->init();//必须使用 $kv->set('index', $data);
- 查看linux进程(强制中止进程),服务及端口号,
进程状态查询 ps -aux [test@pan ~]$ ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START ...
- strtotime 的几点不同
在php里面,strtotime()有点比较特殊,date函数也有点问题 if ( date(1,time()) == "Monday") //似乎 date(1,time( ...
- [HDOJ - 5208] Where is Bob 【DFS+按位贪心】
题目链接:HDOJ - 5208 题目分析 使用按位贪心的思想,即从高位向低位枚举,尽量使这一位的答案为 1 . 我们使用 DFS ,每次就是对于 [l1, r1] [l2, r2] x 进行处理 ...
- [Codeforces Round #296 div2 D] Clique Problem 【线段树+DP】
题目链接:CF - R296 - d2 - D 题目大意 一个特殊的图,一些数轴上的点,每个点有一个坐标 X,有一个权值 W,两点 (i, j) 之间有边当且仅当 |Xi - Xj| >= Wi ...
- 如何使用 Java 构建微服务?
[编者按]微服务背后的大理念是将大型.复杂且历时长久的应用在架构上设计为内聚的服务,这些服务能够随着时间的流逝而演化.本文主要介绍了利用 Java 生态系统构建微服务的多种方法,并分析了每种方法的利弊 ...
- Principles of Motion Sensing
Principlesof Motion Sensing Various sensors capable of detecting motionin free space have been comme ...
- latch free
latch free 等待事件: latch: cache buffers chains 这个等待事件其实还有另外一个重要的原因,那么就是逻辑读太高,SQL执行计划走错了导致的. 当进程想要获取锁存器 ...
- 【转】Xcode 7 真机调试详细步骤
原文网址:http://www.jianshu.com/p/fa5f90b61ad6 文/ldjhust(简书作者)原文链接:http://www.jianshu.com/p/fa5f90b61ad6 ...
- HDU-2522 A simple problem
http://acm.hdu.edu.cn/showproblem.php?pid=2522 学习://除数的运算的应用和算法.除法的本质,如果余数出现重复就表示有循环节 A simple probl ...