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 ≤ XN). 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 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.
大意是求所有奶牛往返最短路程总和里的最大值。建一张原图一张反图,跑两边Dijkstra,最后遍历两个d数组,更新答案。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int N=,M=;
int head1[N],ver1[M],edge1[M],Next1[M],head2[N],ver2[M],edge2[M],Next2[M],d1[N],d2[N];//建一个原图,一个反图
bool v1[N],v2[N];
int n,m,tot1=,tot2=,p;
priority_queue<pair<int,int> >q1;
priority_queue<pair<int,int> >q2;
void add1(int x,int y,int z)
{
ver1[++tot1]=y;
edge1[tot1]=z;
Next1[tot1]=head1[x];
head1[x]=tot1;
}
void add2(int x,int y,int z)
{
ver2[++tot2]=y;
edge2[tot2]=z;
Next2[tot2]=head2[x];
head2[x]=tot2;
}
void dijkstra1()
{
memset(d1,0x3f,sizeof(d1));//d数组的初始化一定要根据题意,有时要初始化为0,有时初始化为正无穷(最短路)有时初始化为负无穷( dij或者floyd变式求某条最长边)
memset(v1,,sizeof(v1));
d1[p]=;
q1.push(make_pair(,p));
while(q1.size())
{
int x=q1.top().second;q1.pop();
if(v1[x])continue;
v1[x]=;
int i;
for(i=head1[x];i;i=Next1[i])
{
int y=ver1[i],z=edge1[i];
if(d1[y]>d1[x]+z)
{
d1[y]=d1[x]+z;
q1.push(make_pair(-d1[y],y));
}
}
}
}
void dijkstra2()
{
memset(d2,0x3f,sizeof(d2));
memset(v2,,sizeof(v2));
d2[p]=;
q2.push(make_pair(,p));
while(q2.size())
{
int x=q2.top().second;q2.pop();
if(v2[x])continue;
v2[x]=;
int i;
for(i=head2[x];i;i=Next2[i])
{
int y=ver2[i],z=edge2[i];
if(d2[y]>d2[x]+z)
{
d2[y]=d2[x]+z;
q2.push(make_pair(-d2[y],y));
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&p);
int i;
for(i=;i<=m;i++)
{
int t1,t2,t3;
scanf("%d%d%d",&t1,&t2,&t3);
add2(t2,t1,t3);
add1(t1,t2,t3);
}
dijkstra1();
dijkstra2();
int ans=;
for(i=;i<=n;i++)
{
if(i!=p)ans=max(ans,d1[i]+d2[i]);
}
cout<<ans;
return ;
}

POJ3268 Silver Cow Party (建反图跑两遍Dij)的更多相关文章

  1. 洛谷P1073最优贸易(跑两遍dij)

    题目描述 CC C国有n n n个大城市和m mm 条道路,每条道路连接这 nnn个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 mmm 条道路中有一部分为单向通行的道路,一部分为 ...

  2. LuoguP1342请柬 【最短路/建反图】By cellur925

    题目传送门 开始就想直接正向跑一遍Dij把到各点的最短路加起来即可,后来发现与样例少了些,于是再读题发现需要也求出学生们回来的最短路. 但是注意到本题是有向图,如果是无向图就好说. 那么我们怎么解决? ...

  3. 炸弹:线段树优化建边+tarjan缩点+建反边+跑拓扑

    这道题我做了有半个月了...终于A了... 有图为证 一句话题解:二分LR线段树优化建边+tarjan缩点+建反边+跑拓扑统计答案 首先我们根据题意,判断出来要炸弹可以连着炸,就是这个炸弹能炸到的可以 ...

  4. Luogu P1073 最优贸易【最短路/建反图】 By cellur925

    题目传送门 这么经典的题目,还是看了lyd的题解....唉难过. 一句话题意:在一张点有全都的图上找一条从1到n的路径,存在两个点p,q(p<q),使val[q]-val[p]最大. 给出的图是 ...

  5. Magic Potion(最大流,跑两遍网络流或者加一个中转点)

    Magic Potion http://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjin ...

  6. POJ3268 Silver Cow Party —— 最短路

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

  7. POJ-3268 Silver Cow Party---正向+反向Dijkstra

    题目链接: https://vjudge.net/problem/POJ-3268 题目大意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号X,其他牛要去拜访它并且拜访完之后要返回 ...

  8. POJ 2438 Children’s Dining (哈密顿图模板题之巧妙建反图 )

    题目链接 Description Usually children in kindergarten like to quarrel with each other. This situation an ...

  9. POJ3268 Silver Cow Party(dijkstra+矩阵转置)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15156   Accepted: 6843 ...

随机推荐

  1. 【音乐欣赏】《Abnormalize》 - 凛として時雨

    曲名:Abnormalize 作者:凛として時雨 [00:00.96]誰にも見せられないもの [00:06.44]頭の中溢れて [00:11.96]間違いさえも無い世界へ [00:17.16]迷い込ん ...

  2. LED Decorative Light Supplier Introduction - LED Track Light Products

    LED Decorative Light Supplier    introduction: LED track light is a track light with LED as the ligh ...

  3. 一些常用的css

    不换行  white-space:nowrap table纵向合并的单元格垂直居中 display:table-cell; vertical-align:middle; table合并横向单元格  c ...

  4. kuangbin专题 专题九 连通图 Network UVA - 315

    题目链接:https://vjudge.net/problem/UVA-315 题目:求割点. #include <iostream> #include <cstdio> #i ...

  5. MNIST 数据集

    mnist 数据集:包含 7 万张黑底白字手写数字图片,其中 55000 张为训练集,5000 张为验证集,10000 张为测试集.每张图片大小为 28*28 像素,图片中纯黑色像素值为 0,纯白色像 ...

  6. JetBrains PyCharm 2018.2.1 x64永久激活码

    812LFWMRSH-eyJsaWNlbnNlSWQiOiI4MTJMRldNUlNIIiwibGljZW5zZWVOYW1lIjoi5q2j54mIIOaOiOadgyIsImFzc2lnbmVlT ...

  7. Bugku-CTF之本地包含( 60)

    Day36  

  8. vue 生命钩子周期之理解

    对于vue的初学者来说,理解vue的生命钩子周期是很有必要的.什么是生命钩子周期呢,顾名思义就是 “实例初始化”  到  “实例被销毁” 的过程. 理解vue的生命钩子周期,我们就可以更好的在项目中运 ...

  9. 概率dp poj 3071

    题目首先给出一个n,表示比赛一共进行n轮,那么队伍就有2^n只队伍输入一个2^n*2^n的矩阵,p[i][j]代表队伍i打败队伍j的概率dp[i][j]代表第i轮比赛的时候,队伍j赢的概率首先初始化时 ...

  10. eclipse从svn导入静态文件

    1.从eclipse 选择 导入 2.选择仓库和项目,选择finish 3.选择project项目导出