POJ 3268Silver Cow Party
来源:互联网 发布:维珍澳航 知乎 编辑:程序博客网 时间:2024/06/13 05:47
Description
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
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
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
Source
感觉没玩太大的花样,比较裸的最短路了。可是就是不会,感觉好难啊
一个是这是单项的路,另一个是要把图正着算(算以x为终点,个点的距离)一次然后倒过来再算(算以x为起点到个点的距离)一次(这么做为了方便最后选出最大)
具体还有点小问题没弄懂……但是我感觉好累脑子跑不动了……翻译也暂时不写了好了OTZ明天解决一下,先把代码贴上吧……
2016.8.5
好的,早上脑子果然清醒。if ( i == j ) map[i][j] = 0 是因为最后算的时候相等的两个点的路我也加进去了!其实可以直接memset(map,INF,sizeof(map))这么赋值,最后结果相加的时候去点i == j的情况就行了
题意:http://jijiwaiwai163.blog.163.com/blog/static/18629621120111018233997/ 发现这个博客里详细翻译了,我自己就不写了=。=
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 1010#define INF 0x3f3f3f3fint n,m,x;int map[N][N],dis[N],vis[N],disb[N]; int dijkstra(){int i,j,k;memset(vis,0,sizeof(vis));for(i=1;i<=n;i++){dis[i]=map[x][i];disb[i]=map[i][x];}for(i=1;i<=n;i++){int tmp=INF;for(j=1;j<=n;j++)if(!vis[j]&&dis[j]<tmp){tmp=dis[j];k=j;}vis[k]=1;for(j=1;j<=n;j++)if(!vis[j]&&dis[j]>map[k][j]+dis[k])dis[j]=map[k][j]+dis[k];}memset(vis,0,sizeof(vis));for(i=1;i<=n;i++){int tmp=INF;for(j=1;j<=n;j++)if(!vis[j]&&disb[j]<tmp)tmp=disb[j],k=j;vis[k]=1;for(j=1;j<=n;j++)if(!vis[j]&&disb[j]>disb[k]+map[j][k])disb[j]=disb[k]+map[j][k];}int maxx=-1;for(i=1;i<=n;i++)maxx=max(maxx,dis[i]+disb[i]);return maxx;}int main(){int i,j,a,b,w;while(~scanf("%d%d%d",&n,&m,&x)){for(i=1;i<=n;i++){for(j=1;j<=n;j++){if(i==j)map[i][j]=0;else map[i][j]=INF;}}//memset(map,INF,sizeof(map));←这么赋值会wa=。=…… for(i=1;i<=m;i++){scanf("%d%d%d",&a,&b,&w);map[a][b]=w;}int end=dijkstra();printf("%d\n",end);}return 0;}
- poj 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ-3268-Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- POJ-3268-Silver Cow Party
- poj 3268--Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ-3268 Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- 设计模式学习之路 - 模板方法模式 - 专注于流程
- 1050. 螺旋矩阵(25)-PAT乙级真题
- 常用JS键盘鼠标事件
- HDU:3665 Seaside(dijkstra算法求最短路径)
- 分拆素数和
- POJ 3268Silver Cow Party
- find_if_not 源码剖析
- 神经网络:学习(2)
- spring (2.5, 3.2) 在 jre 1.8下的fix
- LinkedList的源码分析
- Kafka集群搭建02-Kafka 集群部署
- 数学 ( 容斥原理&&求区间内互质数的个数)——Co-prime ( HDU 4135 )
- listview添加footerview的注意事项
- poj 2488 A Knight's Journey