find the most comfortable road(并差集,找差值最小的权值)
find the most comfortable road
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5359 Accepted Submission(s): 2327
星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam
Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对
Flycar的“舒适度”有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服
,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ),
但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。(SARS是双向的)。
第一行有2个正整数n (1<n<=200)和m (m<=1000),表示有N个城市和M条SARS。
接下来的行是三个正整数StartCity,EndCity,speed,表示从表面上看StartCity到EndCity,限速为speedSARS。speed<=1000000
然后是一个正整数Q(Q<11),表示寻路的个数。
接下来Q行每行有2个正整数Start,End, 表示寻路的起终点。
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2
0
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int INF=0x7f7f7f7f;
const int MAXN=;
const int MAXM=;
int pre[MAXN];
struct Node{
int u,v,d;
};
Node dt[MAXM];
int cmp(Node a,Node b){
return a.d<b.d;
}
int find(int x){
int r=x;
while(r!=pre[r])r=pre[r];
// pre[x]=r;
return r;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i=;i<m;i++){
scanf("%d%d%d",&dt[i].u,&dt[i].v,&dt[i].d);
}
sort(dt,dt+m,cmp);
int Q,s,e,f1,f2;
scanf("%d",&Q);
while(Q--){
int ans=INF;//这个竟然被我写到外边
scanf("%d%d",&s,&e);
for(int j=;j<m;j++){
for(int i=;i<=n;i++)pre[i]=i;
for(int i=j;i<m;i++){
int u=dt[i].u,v=dt[i].v,d=dt[i].d;
f1=find(u);f2=find(v);
if(f1!=f2)pre[f2]=f1;
if(find(s)==find(e)){
ans=min(ans,d-dt[j].d);
break;
}
}
}
if(ans==INF)puts("-1");
else printf("%d\n",ans);
}
}
return ;
}
find the most comfortable road(并差集,找差值最小的权值)的更多相关文章
- HDU 1598 find the most comfortable road(最小生成树之Kruskal)
题目链接: 传送门 find the most comfortable road Time Limit: 1000MS Memory Limit: 32768 K Description XX ...
- hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- HDU 1598 find the most comfortable road (MST)
find the most comfortable road Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- HDU-1598 find the most comfortable road
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1589 Find The Most Comfortable Road 最小生成树+枚举
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU1598 find the most comfortable road 【并查集】+【枚举】
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- find the most comfortable road(hdu1598)不错的并查集
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 1598 find the most comfortable road (并查集+枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...
随机推荐
- input 输入验证
js验证输入框内容 只能输入英文 只能输入英文 无法粘贴,右键不会弹出粘贴菜单 只能输入数字: 只能输入数字,小数点: 只能输入数字,小数点,下划线: 只能输入英文和数字: 只能输入汉字: 禁止输入法 ...
- BZOJ 1951: [Sdoi2010]古代猪文( 数论 )
显然答案是G^∑C(d,N)(d|N).O(N^0.5)枚举N的约数.取模的数999911659是质数, 考虑欧拉定理a^phi(p)=1(mod p)(a与p互质), 那么a^t mod p = a ...
- Hive学习之动态分区及HQL
Hive动态分区 1.首先创建一个分区表create table t10(name string) partitioned by(dt string,value string)row format d ...
- Web存储(Web Storage)介绍
Web存储即在客户端存储数据. 在没有Web Storage之前,是通过cookie来在客户端存储数据的.但是由于 浏览器能存cookie数比较少.如IE8,Firefox,opera每个域可以保存的 ...
- MySQL 二进制日志过滤
binlog_do_db; binlog_ignore_db; 这个两个参数是用来控制对哪个数据库的更改要记录日志:下面以binlog_ignore_db为例子. 假如binlog_ignore_db ...
- oracle 数据库数据迁移解决方案
大部分系统由于平台和版本的原因,做的是逻辑迁移,少部分做的是物理迁移,接下来把心得与大家分享一下 去年年底做了不少系统的数据迁移,大部分系统由于平台和版本的原因,做的是逻辑迁移,少部分做的是物理迁 ...
- NAND FLASH特性说明
1.NAND FLASH 的特殊性. 1)存在坏块.由于NAND生产工艺的原因,出厂芯片中会随机出现坏块.坏块在出厂时已经被初始化,并在特殊区域中标记为不可用,在使用过程中如果出现坏块,也需要进行标记 ...
- A20 GPIO中断类型差别结果迥异的问题思考
A20GPIO中断类型差别结果迥异的问题思考 最近在使用全志A20做开发时,发现在处理中断的时候,用电平触发模式,报中断比较乱,用边沿触发则很稳定,不会乱报.笔者感到比较困惑,笔者用电平触发写的cod ...
- gravitas是什么意思_gravitas在线翻译_英语_读音_用法_例句_海词词典
gravitas是什么意思_gravitas在线翻译_英语_读音_用法_例句_海词词典 gravitas
- uva 10548 - Find the Right Changes(拓展欧几里得)
题目链接:uva 10548 - Find the Right Changes 题目大意:给定A,B,C,求x,y,使得xA+yB=C,求有多少种解. 解题思路:拓展欧几里得,保证x,y均大于等于0, ...