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: 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.

思路

这道题用到了最短路的思想。这道题的要求也是单向的路径,在编写代码的时候需要注意一下。

#include <iostream>
#include <stdio.h>
#include <cstring>using namespace std;
int n,m,x,minn,u,ans=0;
int ai,bi,ti;
int f[1005][1005];
int dis[1005],book[1005],way[1005];
const int inf=99999999;void distant(int x)
{//book[]数组用来记录查询过的点for(int i=1;i<=n;i++)book[i]=0;//重置book数组book[x]=1;for(int i=1;i<=n;i++)//记录点i到x的距离dis[i]=f[x][i];for(int i=2;i<=n;i++){minn=inf;for(int j=1;j<=n;j++)//两个农场之间取最短的路径{if(!book[j]&&dis[j]<minn){minn=dis[j];u=j;}}book[u]=1;for(int j=1;j<=n;j++){if(!book[j]&&dis[j]>dis[u]+f[u][j])dis[j]=dis[u]+f[u][j];}}
}int main()
{while(scanf("%d %d %d",&n,&m,&x)!=EOF){for(int i=0;i<=n;i++)//这里i的范围要取小于n,如果取小于1005z则会runtime error{//重置数组for(int j=0;j<=n;j++){if(i==j){f[i][j]=0;}else{f[i][j]=inf;}}}for(int i=0;i<m;i++){scanf("%d %d %d",&ai,&bi,&ti);if(f[ai][bi]>ti)f[ai][bi]=ti;}distant(x);for(int i=1;i<=n;i++)way[i]=dis[i];//记录distant函数之后的dis的数据for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){int tem;tem=f[j][i];f[j][i]=f[i][j];f[i][j]=tem;}}distant(x);for(int i=1;i<=n;i++){ans=max(ans,way[i]+dis[i]);}printf("%d\n",ans);}return 0;
}

Silver Cow Party (最短路)相关推荐

  1. POJ - 3268 Silver Cow Party(最短路)

    题目链接:点击查看 题目大意:给出n个点以及m条单项路径和一个点x,设从x点到i的距离及从i回到x点的距离分别为d1和d2,求d1+d2的最大值(1<=i<=n) 题目分析:看到这个题的第 ...

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

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

  3. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  4. D - Silver Cow Party POJ - 3268

    D - Silver Cow Party POJ - 3268 dijkstra 是 O(n2),堆优化一下, O(nlogn) 对每个点跑一次 dj, 取 max(dis(x->i)+dis( ...

  5. [POJ](3268)Silver Cow Party ---最短路径(图)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23762   Accepted: 1085 ...

  6. POJ 3268:Silver Cow Party 求单点的来回最短路径

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15989   Accepted: 7303 ...

  7. 【POJ - 3268 】Silver Cow Party(Dijkstra最短路+思维)

    题干: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...

  8. [USACO07FEB]银牛派对Silver Cow Party---最短路模板题

    银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...

  9. Silver Cow Party POJ - 3268 (固定起点和固定终点的最短路)

    思路:有向图.假设在X牧场参加party,从X回家的时候,以X为起点,使用一次Dijkstra算法即可.难点在于去X参加party的最短路如何求解. 这时候我们可以反向建图,即把原来有向图的方向全部反 ...

最新文章

  1. 三国演义人物出场统计代码含义_实例2之《三国演义》人物出场统计
  2. UNITY 多个子MESH与贴图的对应关系
  3. 《深入理解Android:Telephony原理剖析与最佳实践》一1.3 Android Telephony框架结构...
  4. mysql set 子表,mysql update set 更新表数据
  5. kafka shell
  6. 如何修改MySQL8.0.5以上版本root密码
  7. jQuery Datatable 实用简单实例
  8. python目录及文件_零基础小白必看:python基本操作-文件、目录及路径
  9. LeetCode MySQL 1193. 每月交易 I(date_format)
  10. 超全Linux备份工具集合,满足你的所有需要!
  11. 关于url传参中文乱码问题
  12. K8S_Google工作笔记0004---平台规划和部署方式介绍_搭建k8s集群准备
  13. Linux中使用 if 、for、while等循环来写脚本
  14. (二)什么是IT售前?为什么需要IT售前?
  15. 红颜本无心 奈何为祸水
  16. 开博第一篇,为什么要开通博客,开通博客的申请理由
  17. 南京java架构师工资_java架构师工资一般是多少?怎么提升才能获得高薪?
  18. c语言菜单即功能,C语言 菜单专题
  19. 机器学习之线性回归原理详解、公式推导(手推)、简单实例
  20. 【时间序列聚类】KMedoids聚类+DTW算法

热门文章

  1. 人工智能隐私保护:如何在保护隐私的同时保护数据的可维护性和可验证性
  2. 这款跳来跳去的小游戏,是微信小程序的年度大招
  3. Android Tombstone 分析
  4. 【主题词——满天星】
  5. Nginx 0day LDAP RCE 漏洞情报分析
  6. 如何使自己的笔记本电脑的扬声器插上耳机可以发声
  7. RTOS——RT-Thread快速入门
  8. 算法题:旅途(楚楚街2016招聘笔试)
  9. 手把手教你理解圣杯布局和双飞翼布局
  10. Oracle之行转列,列转行大全