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. UE4 游戏模块初始化顺序

    最近看教学,有个讲解UE4初始化顺序的,记录一下. 首先创建一个Actor,Character,GameInstance,GameMode,LevelScriptActor(关卡),PlayerCon ...

  2. 数字麦克风PDM信号采集与STM32 I2S接口应用(二)

    在使用STM32的数字麦克风I2S接口时,计算采样率让人头疼,芯片手册上没有明确的说法,而手册上的计算方法经过测试确和实验不符.借助搜索引擎,大部分资料都是来自于开发板卖家或开发板论坛,主要是咪头采集 ...

  3. python基础之条件控制

    行与缩进 python使用缩进来表示代码块,不需要使用大括号 {} ,同一个代码块的语句必须包含相同的缩进空格数. 条件控制 #条件控制语法: if 条件: 代码块 elif 条件: 代码块 else ...

  4. Jmeter 接口测试参数处理

    问题: 一.签名参数sign算法由文字描述,算法需自己编写 二. 参数param_json为变化的json串(json串内订单号唯一) 解决: 一. 签名sign: 1. 手动拼接后在https:// ...

  5. 【0806 | Day 9】三张图带你了解数据类型分类和Python深浅拷贝

    一.数据类型分类 二.Python深浅拷贝

  6. python自动化运维技术读书笔记

    import psutilprint(psutil.cpu_times(percpu=True)) #使用cpu_times方法获取CPU完整信息需要显示所有逻辑CPU信息 import psutil ...

  7. mysql的引擎问题,主键和外键的创建问题,以及创建外键不成功,却创建了一个索引

    mysql的引擎问题: 需要知道的三个引擎:InnoDB--是一个事务处理引擎,不支持全文检索,支持事务操作,即DML操作: Memory--是一个数据存储在内存,速度很快,功能上等同于MyIsam, ...

  8. wordpress修改登录密码

    wordpress忘记密码更改 网上搜到的方法: 1.后台邮件重置: 2,phpmyadmin登录数据库,执行mysql语句或者在wp_users表中重置密码: 3,利用php文件重置. 这是提供一种 ...

  9. JAVA笔记【类】

    java的概述和编程基础在这里我就不过多的强调了,因为已经有学习C和C++的基础了,我在这里强调一下类和对象. [一]类的定义: Java类的定义包括类声明和类体两个部分,其中类体又包含变量声明,方法 ...

  10. springboot项目中的普通Session和使用redis存储session

    普通session: session store type使用来存放session的存储方式,目前Spring boot中只支持Redis方式, 由于本应用暂无需将session放入redis的需求, ...