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 ;
}

最新文章

  1. day05 java JDBC案例&mdash;Android小白的学习笔记
  2. awk 反过滤不想要的列 和循环列
  3. js判断手机浏览器并跳转到手机网站
  4. supervisord
  5. android 如何让文本中某个关键字高亮显示?
  6. Linux命令后台执行技巧小结
  7. Zoom属性的使用介绍(IE私有属性)
  8. WPF界面设计技巧(5)—自定义列表项呈现内容
  9. Google 浏览器好用插件推荐
  10. vue cli3.0配制axios代理
  11. checkpoint NGFW 实验(一)
  12. WorldCount代码检查与优化——软件测试第三次作业
  13. Luogu4640 BJWC2008 王之财宝 容斥、Lucas
  14. BZOJ.4894.天赋(Matrix Tree定理 辗转相除)
  15. 定位到行的快捷键iOS
  16. C#简单的通用分页
  17. C语言程序设计I—第九周教学
  18. 超简单让ubuntu开启wifi热点(亲测16.04与14.04可用)
  19. kafka监听类
  20. [ 总结 ] nginx 编译参数中文详解

热门文章

  1. MyEcplise中编码格式的修改问题
  2. springboot+jwt
  3. [CF]Round510
  4. Python小白的零碎记录
  5. MySQL性能优化---优化方案
  6. AcWing 867. 分解质因数
  7. vue移动端项目在手机上调试
  8. 【原】Harbor安装及使用
  9. 【音乐欣赏】《JINGO JUNGLE》 - MYTH &amp; ROID
  10. 【游戏体验】Infiltraing the Airship(火柴人潜入飞船)