POJ-3615_Cow Hurdles
Cow Hurdles
Time Limit: 1000MS Memory Limit: 65536K
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 getting tired, though, so they want to be able to use as little energy as possible to jump over the hurdles.
Obviously, it is not very difficult for a cow to jump over several very short hurdles, but one tall hurdle can be very stressful. Thus, the cows are only concerned about the height of the tallest hurdle they have to jump over.
The cows' practice room has N (1 ≤ N ≤ 300) stations, conveniently labeled 1..N. A set of M (1 ≤ M ≤ 25,000) one-way paths connects pairs of stations; the paths are also conveniently labeled 1..M. Path i travels from station Si to station Ei and contains exactly one hurdle of height Hi (1 ≤ Hi ≤ 1,000,000). Cows must jump hurdles in any path they traverse.
The cows have T (1 ≤ T ≤ 40,000) tasks to complete. Task i comprises two distinct numbers, Ai and Bi (1 ≤ Ai ≤ N; 1 ≤ Bi ≤ N), which connote that a cow has to travel from station Ai to station Bi (by traversing over one or more paths over some route). The cows want to take a path the minimizes the height of the tallest hurdle they jump over when traveling from Ai to Bi . Your job is to write a program that determines the path whose tallest hurdle is smallest and report that height.
Input
- Line 1: Three space-separated integers: N, M, and T
- Lines 2..M+1: Line i+1 contains three space-separated integers: Si , Ei , and Hi
- Lines M+2..M+T+1: Line i+M+1 contains two space-separated integers that describe task i: Ai and Bi
Output
- Lines 1..T: Line i contains the result for task i and tells the smallest possible maximum height necessary to travel between the stations. Output -1 if it is impossible to travel between the two stations.
Sample Input
5 6 3
1 2 12
3 2 8
1 3 5
2 5 3
3 4 4
2 4 8
3 4
1 2
5 1
Sample Output
4
8
-1
题意:给与点和边,然后询问任意两点间的距离。若无法到达则输出-1
题解:用最暴力的Floyed算法求出所有点的最短路,然后根据询问输出就可以了。
#include <queue>
#include <cmath>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 255;
const int INF = 1e9 + 7;
int s[maxn][maxn];
int main()
{
int m,n,i,j,k,t,a,b,c;
scanf("%d%d%d",&n,&m,&t);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
s[i][j] = i==j?0:INF;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(s[a][b]>c)
s[a][b] = c;
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(max(s[i][k],s[k][j])<s[i][j])
s[i][j] = max(s[i][k],s[k][j]);
}
while(t--)
{
scanf("%d%d",&a,&b);
if(s[a][b]==INF)
printf("-1\n");
else
printf("%d\n",s[a][b]);
}
return 0;
}
POJ-3615_Cow Hurdles的更多相关文章
- POJ 3615 Cow Hurdles
http://poj.org/problem?id=3615 floyd 最短路径的变形 dist[i][j]变化为 : i j之间的最大边 那么输入的时候可以直接把dist[i][j] 当作i j ...
- POJ 3615 Cow Hurdles(最短路径flyod)
Cow Hurdles Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9337 Accepted: 4058 Descr ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 图论常用算法之一 POJ图论题集【转载】
POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
随机推荐
- 【python之路43】tornado的用法(一)
一.tonado的代码 1.返回字符串 #!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornad ...
- loj6402 校门外的树(dp,多项式求逆)
https://loj.ac/problem/6402 庆祝一下,,,第一个我自己做出来的,,,多项式的题(没办法,我太弱 虽然用了2个小时才想出来,但这毕竟是0的突破…… 首先声明,虽然我写的题解很 ...
- python 升级后正确安装 pip
由于服务器的python 版本是2.6.6 , 为了使用 twisted 升级至 2.7.13 , 如果此时直接用 yum install python-pip 安装 pip, 则实际pip 会安装在 ...
- MySQL数据库的全局锁和表锁
1.概念 数据库锁设计的初衷是处理并发问题.作为多用户共享的资源,当出现并发访问的时候,数据库需要合理地控制资源的访问规则.而锁就是用来实现这些访问规则的重要数据结构. 2.锁的分类 根据加锁的范围, ...
- Laravel 批量替换某个字段
Likeword::offset(16854)->chunk(100, function ($word_list) { foreach ($word_list as $word) { $new ...
- 卡特兰数(catalan)总结
卡特兰数的公式 递推公式1:$f(n)=\sum \limits_{i=0}^{n-1}f(i)*f(n-i-1)$ 递推公式2:$f(n)=\frac{f(n-1)*(4*n-2)}{n+1}$ 组 ...
- Django项目:CRM(客户关系管理系统)--11--04PerfectCRM实现King_admin注册功能03
#base_admin.py #Django admin 注册功能的形式 # sites = { # 'crm':{ # 'customers':CustomerAdmin, # 'customerf ...
- C#和JS交互 WebBrowser实例
本文实现了WebBrowser的简单例子 1.引用System.Windows.Froms.dll 2.引用WindowsFormsIntegration.dll 代码如下: public parti ...
- 如何成功安装旧版本火狐,成功安装firebug
1.下载一个老版本火狐浏览器: 历史版本下载 2.下载安装完成后,立即在火狐浏览器的选项设置里面把自动更新关闭 3.手动安装firebug:最后搜到可以下载成功的地址:http://www.onlin ...
- JDK8 Stream 数据流效率分析
JDK8 Stream 数据流效率分析 Stream 是Java SE 8类库中新增的关键抽象,它被定义于 java.util.stream (这个包里有若干流类型: Stream<T> ...