POJ
来源:互联网 发布:英译汉软件免费下载 编辑:程序博客网 时间:2024/06/08 19:44
题目描述:
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.
题意:
几头牛聚会,从自己的家出发到聚会地点,再回家,求出每只牛的最短路,然后从最短路里选出最大的。
解题思路:
求一次聚会地点到各个牛家的最短路,就求出了回去的最短路,把图行列反转,再求一次,就是牛去聚会的最短路。加起来,求最大,。
# include <cstdio># include <algorithm># define INF 999999 using namespace std;const int max_v = 1005;int cost[max_v][max_v];int d[max_v],tem[max_v] = {-1,0};bool used[max_v];int V;void dj(int s){ fill(d,d+V,INF); fill(used,used+V,false); d[s] = 0; while(true){ int v = -1; for(int u = 0;u < V;u++) { if(!used[u] && (v == -1 || d[u] < d[v])) v = u; } if( v == -1) break; used[v] = true; for(int u = 0;u < V;u++) { d[u] = min(d[u],d[v] + cost[v][u]); } } if(tem[0] == -1) // 选为标记的数字一定不能在数据中出现 for(int i = 0;i < V;i++) { tem[i] = d[i]; }}int main(){ int n,m,x; int a,b,c; for(int i = 0;i < 1002;i++) //初始化很重要 { for(int j = 0;j < 1002;j++) { cost[i][j] = INF; } } scanf("%d %d %d",&V,&m,&x); for(int i = 0;i < m;i++) { scanf("%d %d %d",&a,&b,&c); cost[a-1][b-1] = c; } dj(x-1); int tt; for(int i = 0;i < V;i++) { for(int j = 0;j < i;j++) //脑子要把程序的流程过一遍,防止转化俩次 { tt = cost[i][j]; cost[i][j] = cost[j][i]; cost[j][i] = tt; } } dj(x-1); int sum = 0; for(int i = 0;i < V;i++) { sum = max(d[i]+tem[i],sum); } printf("%d\n",sum); return 0;}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 如何创建一个可执行bat文件
- 常用 Git 命令清单
- oracle 逗号分隔字段拆分,记录下
- 辗转相余求最大公约数
- MAC 打开.bash_profile
- POJ
- java 按值调用 与 按引用(对象的引用,指针)调用、区分对象与对象引用
- Git 使用规范流程
- 0-1背包问题
- 2018今日头条内推笔试2
- hdu-6162
- Mysql查询性能优化的心得
- 《计算机视觉-一种现代方法(第2版)》读书笔记一:导论大纲
- angularjs bootstrap 购物车