最短路径__Silver Cow Party ( Poj 3268 )
来源:互联网 发布:算法导论 第三版 目录 编辑:程序博客网 时间:2024/05/22 15:34
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 Titime 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
题意 :一共n个农场,m个有向道路,每个农场都有个奶牛,每个奶牛都找最短的路径到农场x去,又找最短路径回到自己的农场.问哪个的奶牛的路程最长,为多少?
思路: 正向、反向建边.两次Dijkstra 算法即可.
#include <stdio.h>#include <string.h>#include <math.h>#define INF 99999999#define N 1010int mpt1[N][N];int mpt2[N][N];int dis1[N];int dis2[N];int visit[N];void Dijkstra(int x,int n,int* dis,int mpt[N][N]){ int i,j; for(i = 1; i <= n ; i ++) dis[i] = mpt[x][i]; memset(visit,0,sizeof(visit)); visit[x] = 1; for(i = 1; i < n ; i ++) { int Minj = -1,Min = INF; for(j = 1; j <= n ; j ++) { if(visit[j]) continue; if( Min > dis[j]) { Min = dis[j]; Minj = j; } } if(Minj == -1)continue; visit[Minj] = 1; for(j = 1; j <= n ; j ++) { if(dis[j] > dis[Minj]+mpt[Minj][j] ) dis[j] = dis[Minj]+mpt[Minj][j]; } }}int Min1(int a,int b){if( a < b)return a;return b;}int main(){int n,m,i,j,x;while(scanf("%d%d%d",&n,&m,&x)!=EOF){for(i = 1; i <= n ; i ++){for(j = 1; j <= n ; j ++){if( i == j ) mpt1[i][j] = 0,mpt2[i][j] = 0;else mpt1[i][j] = INF,mpt2[i][j] = INF;}}int u,v,len;for(i = 0 ; i < m ; i++){scanf("%d %d %d",&u,&v,&len);mpt1[u][v] = len;mpt2[v][u] = len;}Dijkstra(x,n,dis1,mpt1);Dijkstra(x,n,dis2,mpt2);int ans = 0;for(i = 1 ; i <= n ; i ++) { if(ans < dis1[i] + dis2[i]) ans = dis1[i] + dis2[i]; } printf("%d\n",ans);}return 0;}
- 最短路径__Silver Cow Party ( Poj 3268 )
- poj 3268 Silver Cow Party(最短路径)
- [POJ](3268)Silver Cow Party ---最短路径(图)
- POJ 3268 Silver Cow Party(最短路径dijkstra算法)
- POJ 3268:Silver Cow Party 求单点的来回最短路径
- POJ 3268 Silver Cow Party(最短路径之迪杰斯特拉算法)
- POJ3268 Silver Cow Party(最短路径)
- POJ3268-Silver Cow Party(最短路径)
- 北大ACM3268——Silver Cow Party~~最短路径
- POJ3268---Silver Cow Party (最短路径)
- Silver Cow Party (SPFA+最短路径)
- 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(最短路)
- IOS fatal error: unexpectedly found nil while unwrapping an Optional value的一个原因
- R Global Option Settings
- [leetcode] 90. Subsets II 解题报告
- 8.14 Boolean Evaluation
- git 学习笔记3
- 最短路径__Silver Cow Party ( Poj 3268 )
- Android内存泄露案例分析
- Unified Functional Testing Code Collections
- 软件需求分层处理的多种常见方式
- [150227]魔女的夜宴サノバウィッチ-SABBAT OF THE WITCH【日文硬盘版】[带全CG存档&攻略+日本语启动补丁]
- CSS左侧固定宽 右侧自适应(兼容所有浏览器)
- 最短路径___Currency Exchange ( Poj 1860 )
- js 数组去除的几种方法
- 【ASC45D】【构造 电阻网络法+暴力冲精度法+比例转换法】醉汉走路二分叉 回家概率恰为p/q