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 ...
随机推荐
- 转:Ext GridPanel根据条件显示复选框
Ext GridPanel实现复选框选择框: var selectModel = new Ext.grid.CheckboxSelectionModel({ singleSelect : false ...
- Jquery常用功能
jQuery 1.4给开发者带来了很多值得兴奋的新特性,同时使用jQuery的人也越来越多,为了方便大家对jQuery的使用,下面列出了一些jQuery使用技巧.比如有禁止右键点击.隐藏搜索文本框文字 ...
- Docker安装Gitlab
一.Ubuntu16.4上Docker安装Gitlab 1.安装docker 参见:https://docs.docker.com/engine/installation/linux/ubuntuli ...
- AngularJS测试框架 karma备忘
AngularJS测试框架karma安装 安装karma $ --save-dev 安装karma组件 $ npm install karma-jasmine karma-chrome-launche ...
- select 模型
http://www.cnblogs.com/Anker/p/3258674.html http://www.cnblogs.com/cozy/articles/2088128.html http:/ ...
- 自动生成makefile的脚本
如果需要测试某一个特性,写了一个test.cpp 某天又增加了一个utils.cpp,依此类推,测试文件越来越多 每次测试时都要手动维护一个makefile实在是不明智的 于是萌生了用脚本自动维护的念 ...
- php版的求表达式的真值表-TrueValueTable
贴上代码: <?php error_reporting(E_ALL & ~E_NOTICE); $expression=$_GET['TrueTable']; //读取输入框数据 if( ...
- Hust 1231 Coin
题目链接 题意: 亮亮有N个有瑕疵的硬币,有瑕疵意味着抛一枚硬币正面向上的概率 不等于 反面向上的概率 也即概率不等于0.5. 现在亮亮一次抛N个硬币 , 恰好有K个硬币正面向上 ,接着他又想抛一次 ...
- sqlserver 学习
http://www.cnblogs.com/CareySon/category/411344.html SQL Server查找的最小单位实际上是页.也就是说即使你只查找一行很小的数据,SQL Se ...
- [BZOJ 1098] [POI2007] 办公楼biu 【链表优化BFS】
题目链接:BZOJ - 1098 题目分析 只有两个点之间有边的时候它们才能在不同的楼内,那么就是说如果两个点之间没有边它们就一定在同一座楼内. 那么要求的就是求原图的补图的连通块. 然而原图的补图的 ...