题意:

求X到某点来回路程的最短路的最大值。

模板更新中。。。

题目

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

分析:

X到某点的最短路可以由dijkstra算法求得,但是某点回来的路程需要对每个点都使用dijkstra算法,时间复杂度过大。
逆向思维:返程为往程的逆过程。
此时,只需要使用两次dijkstra算法就可以了。

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int M=1e3+10;
int m,n,k,ans;
int dp[M][M],dis[M],s[M],book[M];
void dijkstra()
{memset(book,0,sizeof(book));for(int i=1; i<=n; i++)dis[i]=dp[k][i];dis[k]=0;for(int i=1; i<n; i++){int ans=inf,kk;for(int j=1; j<=n; j++)if(!book[j]&&ans>dis[j]){ans=dis[j];kk=j;}book[kk]=1;for(int j=1; j<=n; j++)dis[j]=min(dis[j],dis[kk]+dp[kk][j]);}
}
int  main()
{scanf("%d%d%d",&n,&m,&k);memset(dp,inf,sizeof(dp));for(int i=1; i<=n; i++)dp[i][i]=0;for(int i=1; i<=m; i++){int t1,t2,t3;scanf("%d%d%d",&t1,&t2,&t3);if(dp[t1][t2]>t3)dp[t1][t2]=t3;}dijkstra();for(int i=1; i<=n; i++)s[i]=dis[i];for(int i=1; i<=n; i++)for(int j=i+1; j<=n; j++)swap(dp[i][j],dp[j][i]);dijkstra();for(int i=1; i<=n; i++)ans=max(ans,s[i]+dis[i]);printf("%d\n",ans);return 0;
}

Silver Cow Party POJ - 3268(dijkstra+反向交换)相关推荐

  1. D - Silver Cow Party POJ - 3268

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

  2. Silver Cow Party POJ - 3268(正反建图+dijkstra)

    One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1-N is going to attend the big cow ...

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

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

  4. Silver Cow Party POJ - 3268

    One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...

  5. Silver Cow Party (POJ - 3268 )

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

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

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

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

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

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

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

  9. POJ 3268 (dijkstra算法)

    Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N  ...

最新文章

  1. CodeForces - 1215C Swap Letters(暴力+思维+模拟)
  2. WinForm窗体PropertyGrid控件的使用
  3. linux php 中文文件名乱码,linux文件名乱码
  4. SourcesTree使用手册1:与Git进行通信
  5. Attribute 和 Parameter 的区别
  6. Ubuntu 18.04 8T硬盘挂载
  7. java程序员简历项目经验_程序员简历中的项目经验怎么写
  8. 解决Mac App Store下载软件一直转圈无法完成的问题,黑白苹果均有效
  9. 服务器改无线路由器怎么设置,怎么把旧路由器改装成中继器
  10. 文件系统测试工具IOZONE
  11. stm32 USB HID多点触摸屏上报安卓触摸信号
  12. php仿信用卡积分商城,中信银行信用卡积分购物商城
  13. 猿创征文|一个.Net过气开发工程师的成长之路
  14. 解决双蛋问题的C代码实现
  15. 如何快速解决 Mybatis 异常:Invalid bound statement (not found)
  16. python 全栈开发,Day86(上传文件,上传头像,CBV,python读写Excel,虚拟环境virtualenv)
  17. 【邻接矩阵乘法】沼泽鳄鱼
  18. 9.PS-强大的画笔工具
  19. 获取处于选中状态的checkbox的id值
  20. Linux-新用户创建和删除

热门文章

  1. linux c之通过popen执行shell命令
  2. C++之‘nullptr’ was not declared in this scope
  3. linux c之通过消息队列实现进程通信
  4. python创建不可变集合_python不可变集合是什么
  5. mysql为什么添加索引_当我添加新索引时,为什么MySQL中索引的基数保持不变?
  6. mysql 开发规范_专业级的MySQL开发设计规范及SQL编写规范
  7. 两度破译“白宫密码”,让美国政府部门崩溃,却称自己是抱娃敲代码的普通妈妈
  8. 史上最烧脑的学习方法,看完瞬间涨姿势!
  9. 众里寻 Bug 千百度,蓦然回首,它却在隔壁老张处……
  10. Oracle常用知识总结