Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22864   Accepted: 10449

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: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi, 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

求两次最短路,第一次求t到其余各点的最短路,第二次求各点到t的最短路。
最后求的是所有点到t的距离的最大值
#include <iostream>
#include <deque>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define maxn 100010
using namespace std;
vector<pair<int,int> > E[maxn];
int d1[maxn],d2[maxn];
int n,m,t,x[maxn],y[maxn],z[maxn];
int max(int a,int b){
if(a>=b){
return a;
}
return b;
}
void init1(){
for(int i=;i<maxn;i++){
E[i].clear();
d1[i] = 1e9;
}
}
void init2(){
for(int i=;i<maxn;i++){
E[i].clear();
d2[i] = 1e9;
}
}
void dijkstra(int t,int d[]){
d[t] = ;
priority_queue<pair<int,int> > q;
q.push(make_pair(-d[t],t));
while(!q.empty()){
int now = q.top().second;
q.pop();
for(int i=;i<E[now].size();i++){
int v = E[now][i].first;
if(d[v] > d[now] + E[now][i].second){
d[v] = d[now] + E[now][i].second;
q.push(make_pair(-d[v],v));
}
}
}
}
int main()
{
while(cin >> n >> m >> t){
init1();
for(int i=;i<m;i++){
cin >> x[i] >> y[i] >> z[i];
E[x[i]].push_back(make_pair(y[i],z[i]));
}
dijkstra(t,d1);//正求一次
init2();
for(int i=;i<m;i++){
E[y[i]].push_back(make_pair(x[i],z[i]));
} //记得在这里要把所有的路反过来
dijkstra(t,d2);//反求一次
int num = -;
for(int i=;i<=n;i++){
num = max(num,d1[i]+d2[i]);
}
cout << num << endl;
}
return ;
}

POJ 3268 Silver Cow Party 单向最短路的更多相关文章

  1. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

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

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

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

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

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

  4. poj 3268 Silver Cow Party(最短路,正反两次,这个模版好)

    题目 Dijkstra,正反两次最短路,求两次和最大的. #define _CRT_SECURE_NO_WARNINGS //这是找出最短路加最短路中最长的来回程 //也就是正反两次最短路相加找最大的 ...

  5. POJ 3268 Silver Cow Party(最短路&Dijkstra)题解

    题意:有n个地点,有m条路,问从所有点走到指定点x再走回去的最短路中的最长路径 思路:用Floyd超时的,这里用的Dijkstra. Dijkstra感觉和Prim和Kruskal的思路很像啊.我们把 ...

  6. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

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

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

  8. POJ 3268 Silver Cow Party 最短路

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

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

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

随机推荐

  1. NYOJ 53 最少步数

    题      目    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=58 思路借鉴   DFS-Deep First Search-深度优先 ...

  2. JSON合并,并按时间排序

    mergeJson: function (json1, json2) { var json = Object.assign([], json1, json2); return json.sort(fu ...

  3. 01-Spring Security框架学习

    目录 01-Spring Security框架学习 简介 Spring Security 是什么 Spring Security 解决那些问题 Spring Security 的优点 历史背景 Spr ...

  4. kafka消息的处理机制(五)

    这一篇我们不在是探讨kafka的使用,前面几篇基本讲解了工作中的使用方式,基本api的使用还需要更深入的去钻研,多使用才会有提高.今天主要是探讨一下kafka的消息复制以及消息处理机制. 1. bro ...

  5. Pandas 库之 DataFrame

    How to use DataFrame ? 简介 创建 DataFrame 查看与筛选数据:行列选取 DataFrame 数据操作:增删改 一.About DataFrame DataFrame 是 ...

  6. Linux与Unix到底有什么不同?

    来自:开源中国 原文:Linux vs. Unix: What's the difference? 链接: https://opensource.com/article/18/5/difference ...

  7. RuntimeError: one of the variables needed for gradient computation has been modified by an inplace

    vgg里面的 ReLU默认的参数inplace=True 当我们调用vgg结构的时候注意 要将inplace改成 False 不然会报错 RuntimeError: one of the variab ...

  8. CEF避坑指南(一)——下载并编译第一个示例

    CEF即Chromium Embedded Framework,Chrome浏览器嵌入式框架.它提供了接口供程序员们把Chrome放到自己的程序中.许多大型公司,如网易.腾讯都开始使用CEF进行前端开 ...

  9. 敏捷之旅--携程Scrum Master 新官上任三把火?

      随着敏捷在国内的推行,越来越多的公司和组织开始使用敏捷领导团队. 敏捷团队如雨后春笋之势涌现. 敏捷教练的团队也越来越壮大.   原先只需要一个敏捷教练就能搞定,但是随着团队越来越多,我们难免会将 ...

  10. 详解python函数的参数

    详解python函数的参数 一.参数的定义 1.函数的参数在哪里定义 在python中定义函数的时候,函数名后面的括号里就是用来定义参数的,如果有多个参数的话,那么参数之间直接用逗号, 隔开 案列: ...