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. python基于百度AI开发文字识别

    很多场景都会用到文字识别,比如app或者网站里都会上传身份证等证件以及财务系统识别报销证件等等 第一步,你需要去百度AI里去注册一个账号,然后新建一个文字识别的应用 然后你将得到一个API Key 和 ...

  2. Codeforces Round #530 (Div. 2) D. Sum in the tree 树上贪心

    D. Sum in the tree 题意 给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少 思路 对于每一个-1(也就是值未 ...

  3. js的undefined和null

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. typedef基本用法

    [代码演示] 例一 例二 例三

  5. 关于Sublime如何配置C++环境的问题

    前言 传说sublime是全球最好的编辑器,可是只是编辑器啊!!!如果要运行,对于我们这些蒟蒻来说,不得不去使用DEV_C++.我们总是幻想能让sublime变成一个轻量级IDE,那该多好啊!!! 那 ...

  6. spring boot配置druid数据连接池

    Druid是阿里巴巴开源项目中一个数据库连接池. Druid是一个jdbc组合,包含三个部分, 1.DruidDriver代理Driver,能够提供基于Filter-Chain模式得插件体系2.Dru ...

  7. c语言getipaddrtable获得ip地址与sendArp获得mac地址以及一些字节序问题记录

    https://docs.microsoft.com/zh-cn/windows/win32/api/iphlpapi/nf-iphlpapi-getipaddrtable msdn,有很多c的源码还 ...

  8. 3.0 java学习网站

    1.http://www.rupeng.com/Courses/Index/51 2.https://www.zhihu.com/question/25255189

  9. 主席树板子 p2104

    #include<cstdio> #include<algorithm> #include<vector> using namespace std; ; int n ...

  10. 微信小程序遮罩层覆盖input失效

    问题:微信小程序中,我们常使用遮罩层,如点击按钮弹出下拉框.弹框等等.若在遮罩层下存在input.textarea.canvas.camera.map.video等标签时,会出现遮罩层覆盖失效的问题. ...