博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[kuangbin带你飞]专题四 最短路练习 D - Silver Cow Party(最短路spfa+转置邻接矩阵)...
阅读量:4557 次
发布时间:2019-06-08

本文共 3068 字,大约阅读时间需要 10 分钟。

D - Silver Cow Party(双向边)

题目链接:

题目:

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 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 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.
 
题意:有1~N个农场和每个农场的奶牛,每个奶牛需要从它当前的农场到农场编号为X的农场,
到达目的的农场后需要再回到原来各自编号的农场,由于去和回来的权值不同,
所以这时候需要转置邻接矩阵,我们可以换一下思路:
以X农场为起始点,这样一个spfa下来就可以算从X出发到各个农场的最小权值和,
然后再转置road[i][j],就可以将边反向,
再来一个spfa就可以算去的状态了,代码如下:
spfa算法比较快,用了94ms就AC辽
 
//// Created by hanyu on 2019/7/18.//#include
#include
#include
#include
#define MAX 0x3f3f3f3fusing namespace std;const int maxn=1005;int dgo[maxn],dback[maxn],road[maxn][maxn];bool book[maxn];int N,M,X;void spfa(int *d){ memset(book,false,sizeof(book)); for(int i=1;i<=maxn;i++) d[i]=MAX; queue
qu; qu.push(X); book[X]= true; d[X]=0; while(!qu.empty()) { int now=qu.front(); qu.pop(); book[now]=false; for(int i=1;i<=N;i++) { if(d[i]>d[now]+road[now][i]) { d[i]=d[now]+road[now][i]; if(!book[i]) { qu.push(i); book[i]=true; } } } }}int main(){ scanf("%d%d%d",&N,&M,&X); for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { if(i==j) road[i][j]=0; else road[i][j]=MAX; } } memset(dgo,MAX,sizeof(dgo)); memset(dback,MAX,sizeof(dback)); int x,y,z; for(int i=1;i<=M;i++) { scanf("%d%d%d",&x,&y,&z); road[x][y]=z; } spfa(dgo); for(int i=1;i<=N;i++) for(int j=1;j<=i;j++) swap(road[i][j],road[j][i]);//转置邻接矩阵 int maxx=-1; spfa(dback); for(int i=1;i<=N;i++) { maxx=max(maxx,dgo[i]+dback[i]);//X农场到其他每个农场的最小权值+其他农场到X农场的最小权值 } printf("%d\n",maxx); return 0;}

 

 

转载于:https://www.cnblogs.com/Vampire6/p/11208903.html

你可能感兴趣的文章
2015.04.23,外语,读书笔记-《Word Power Made Easy》 12 “如何奉承朋友” SESSION 33
查看>>
Spring+SpringMVC+JDBC实现登录
查看>>
生与死之间
查看>>
NEFU 109
查看>>
HDU 5435
查看>>
git从已有分支拉新分支开发
查看>>
滚动条隐藏兼容写法
查看>>
SQL2005查询所有表的大小
查看>>
Shell 正则表达式
查看>>
Docker run命令参数整理
查看>>
qt-opencv配置mingw编译器
查看>>
CSS之Medial Queries的另一用法:实现IE hack的方法
查看>>
oo第三单元总结
查看>>
linux-CentOS6.4下安装oracle11g详解
查看>>
实力为王 八年DBA经验谈
查看>>
2-sat 问题 【例题 Flags(2-sat+线段树优化建图)】
查看>>
ext3.2 右击动态添加node的treepanel
查看>>
Database links
查看>>
1035 插入与归并(25 分)
查看>>
STL中排序函数的用法(Qsort,Sort,Stable_sort,Partial_sort,List::sort)
查看>>