poj3268Silver Cow Party——最短路变形
来源:互联网 发布:三观尽毁的淘宝评论 编辑:程序博客网 时间:2024/06/05 04:17
poj3268Silver Cow Party:http://poj.org/problem?id=3268
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
题意:有n个农场,共有m条单源路。告诉你x号农场举办party,这n个农场里的牛都要去参加,参加完还要返回各自的农场(去和返不一定是同一条路),每条牛都走最短路,求这些牛中所走的路中最长的一条。
思路:先将去的时候的各自最短路求出来,然后将回来的时候的最短路求出来,两项相加取最大值就可以了。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <queue>#include <stack>#include <cmath>#include <algorithm>using namespace std;#define maxn 2000 + 10#define INF 10000001int n,x;int grap[maxn][maxn],d[maxn],vis[maxn],ba[maxn];void Dij(){ for(int i = 1; i <= n; i++) { d[i] = grap[i][x]; ba[i] = grap[x][i]; } memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; i++) { int u ; int minn = INF; for(int j = 1; j <= n; j++) if(!vis[j] && minn > d[j]) { u = j; minn = d[j]; } vis[u] = 1; for(int j = 1; j <= n; j++) if(!vis[j] && (grap[j][u] + minn < d[j])) d[j] = grap[j][u] + minn; } memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; i++) { int u ,minn = INF; for(int j = 1; j <= n; j++) if(!vis[j] && ba[j] < minn) { u = j; minn = ba[j]; } vis[u] = 1; for(int j = 1; j <= n; j++) if(!vis[j] && ba[j] > grap[u][j] + minn) ba[j] = grap[u][j] + minn; }}int main(){ int m,u,v,w; scanf("%d%d%d",&n,&m,&x); for(int i = 1; i <= n; i++) for(int j = i; j <= n; j++) grap[i][j] = grap[j][i] = (i == j?0:INF); for(int i = 0; i < m; i++) { scanf("%d%d%d",&u,&v,&w); grap[u][v] = min(grap[u][v],w); } Dij(); int ans = 0; for(int i = 1; i <= n; i++) ans = max(ans,d[i] + ba[i]); printf("%d\n",ans); return 0;}
参考:http://blog.csdn.net/wangjian8006/article/details/7872048
- poj3268Silver Cow Party——最短路变形
- poj3268Silver Cow Party最短路问题(dijkstra+邻接矩阵转置)
- POJ3268Silver Cow Party(单源最短路径变形)
- poj3268 Silver Cow Party(最短路变形)
- POJ3268Silver Cow Party
- Poj 3628 Silver Cow Party ( 最短路变形
- POJ3268—Silver Cow Party(反向最短路)
- poj3268Silver Cow Party(单源最短路径)
- POJ3268Silver Cow Party~Dij算法+矩阵倒置
- poj3268Silver Cow Party之dijkstra解法
- POJ3268————Silver Cow Party(最短路)
- poj3268——Silver Cow Party(最短路+godv之力)
- POJ 3268 Silver Cow Party(好题,最短路—dijkstra算法+置换矩阵处理)
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
- POJ3268 Silver Cow Party DIJ最短路
- poj 3268 Silver Cow Party(最短路)
- POJ3268 Silver Cow Party 最短路
- 【POJ3268】Silver Cow Party 最短路
- 第二周项目三 小试循环
- Java之矩阵求特征值
- 人工智能学习笔记(一):简述人工智能的应用领域
- poj1062——最短路Dijkstra
- 蓝屏dump分析教程
- poj3268Silver Cow Party——最短路变形
- SDWebImage简介
- 多线程死锁
- Hadoop集群安装配置
- C++ 函数的嵌套调用
- Java设计模式之建造者模式
- ubuntu root认证失败 用户名 不在 sudoers文件中,此事将被报告
- 需要优化软件UI,so 沉寂许久之后开始学习界面的课程,把文档直接贴过来自己试着翻译
- 传输速率与吞吐量