POJ 3615 Cow Hurdles

http://poj.org/problem?id=3615

floyd 最短路径的变形 dist[i][j]变化为 : i j之间的最大边

那么输入的时候可以直接把dist[i][j] 当作i j 之间的边进行输入

转移方程 dist[i][j] = max(dist[i][j], min(dist[i][k], dist[k][j]))

 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 #define INF 0x3fffffff
 5 using namespace std;
 6 const int maxn = 305;
 7 int Map[maxn][maxn];
 8
 9 int dist[maxn][maxn];//dist[i][j]表示 i 到 j 的最大边是多少?
10 //转移方程 dist[i][j] = min( dist[i][j] ,max(dist[i][k], dist[k][j])) ---> "**"
11 //
12 //与floyd的区别 dist是 i-j的最短‘距离‘
13 int main()
14 {
15     int m, n, k;
16     scanf("%d%d%d", &m, &n, &k);
17     for(int i = 1; i <= m; i++)
18         for (int j = 1; j <= m; j++)
19             dist[i][j] = INF;
20     for (int i = 0; i < n; i++)
21     {
22         int from, to, cost;
23         scanf("%d%d%d", &from, &to, &cost);
24         dist[from][to] = cost;
25     }
26     for(int i = 1; i <= m; i++)
27     {
28         for (int j = 1; j <= m; j++)
29         {
30             for (int k = 1; k <= m; k++)
31             {
32                // if (dist[j][k] == -1) dist[j][k] = max(dist[j][i], dist[i][k]);
33                 //else
34                     dist[j][k] = min(dist[j][k], max(dist[j][i], dist[i][k]));
35             }
36         }
37     }
38     for (int i = 0; i < k; i++)
39     {
40         int from, to;
41         scanf("%d%d", &from, &to);
42         if (dist[from][to] == INF) cout << -1 << endl;
43         else  cout << dist[from][to] << endl;
44     }
45     return 0;
46 }
时间: 2024-10-13 05:25:51

POJ 3615 Cow Hurdles的相关文章

POJ 3615 Cow Hurdles (Floyd算法)

Cow Hurdles Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6142   Accepted: 2752 Description Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are

【POJ 3615】Cow Hurdles

Cow Hurdles Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6486   Accepted: 2948 Description Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are

poj 1985 Cow Marathon 【树的直径】

题目:poj 1985 Cow Marathon 题意:给出一个树,让你求树的直径. 分析: 树的直径:树上两点之间的最大距离. 我们从任意一点出发,BFS一个最远距离,然后从这个点出发,在BFS一个最远距离,就是树的直径. AC代码: /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostream> #include <algorithm> #include

POJ3615 Cow Hurdles【Floyd】

Cow Hurdles Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6155 Accepted: 2760 Description Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are gett

POJ 3613 Cow Relays 恰好n步的最短路径

http://poj.org/problem?id=3613 题目大意: 有T条路,从s到e走n步,求最短路径. 思路: 看了别人的... 先看一下Floyd的核心思想: edge[i][j]=min(edge[i][j],edge[i][k]+edge[k][j]) i到j的最短路是i到j的直接路径或者经过k点的间接路径,但是矩阵的更新总是受到上一次更新的影响 如果每次的更新都存进新矩阵,那么edge[i][k]+edge[k][j]是不是表示只经过三个点两条边的路径呢? min(edge[i

poj 3270 Cow Sorting 置换群 简单题

假设初始状态为 a:2 3 1 5 4 6 则目标状态为 b:1 2 3 4 5 6且下标为初始状态中的3 1 2 4 5 6(a[3],a[1]...) 将置换群写成循环的形式 (2,3,1),(5,4),6就不用移动了. 移动方式2种 1:选循环内最小的数和其他len-1个数交换 2:选整个序列最小的数和循环内最小的数交换,转到1,再换回来. #include<cstdio> #include<queue> #include<algorithm> #include&

poj 3270 Cow Sorting(初涉置换群)

http://poj.org/problem?id=3270 大致题意:给出n个整数,要将它们转化成递增序列,每交换其中两个数的代价是这两个数之和.问排序成功后的最小代价. 该题考察的是置换群知识.在黑书p247上有详细的讲解.总结下置换群,方便复习. 群:给定一个集合G={a,b,c...}和集合G上的二元运算 ·,如果满足封闭性,结合律,存在单位元和逆元,则成集合G在运算'·'之下是一个群. 置换:n个元素1,2,....,n之间的置换可表示为  1     2     3     ...

poj 3348 Cow 凸包面积

Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8122   Accepted: 3674 Description Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are f

POJ 1946 Cow Cycling

Cow Cycling Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2516   Accepted: 1396 Description The cow bicycling team consists of N (1 <= N <= 20) cyclists. They wish to determine a race strategy which will get one of them across the fin