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/ ...
随机推荐
- <jsp:directive.page>标签
directive 英 [dɪ'rektɪv; daɪ-] 美 [daɪ'rɛktɪv] n. 指示:指令 adj. 指导的:管理的 等效于 <%page import="com.ct ...
- 一篇入门的php Class 文章
刚在大略浏览了一下首页更新的那篇有关Class的文章(指PHPE的那篇 http://www.phpe.net/articles/389.shtml ),很不错,建议看看. 对类的摸索--俺用了半年 ...
- 保存属性至xml并读取
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- JAVA并发,锁与方法
引自:<thinking in java> synchronized void f(){/* ... */}; synchronized void g(){/* ... */}; 所有对象 ...
- Java 初始化的个人理解
先说明一下由来吧,下面是同学的一道笔试题,以前感觉对java初始化也是了解一二的,结果,看到这题泪奔了,不会...上网查,自己添加println,总算是能把自己讲明吧了,不知理解的对不对,先记录下吧, ...
- bootstrap 更改container 的width
参考:http://stackoverflow.com/questions/15884102/bootstrap-how-do-i-change-the-width-of-the-container ...
- Vim 实用技术,第 1 部分: 实用技巧(转)
原文链接:http://blog.jobbole.com/20604/ 0. Vim 简介 作为开源世界最重要的编辑器之一(另一个是 Emacs),Vim 以其强大的功能和可定制能力被众多开发者所喜爱 ...
- elk 数据存储
让我们在集群中唯一一个空节点上创建一个叫做blogs的索引,默认情况下,一个索引被分配5个主分片, 但是为了演示的目的, 我们只分配3个主分片和一个复制分片(每个主分片都有一个复制分片): PUT / ...
- hdoj 2546 饭卡(0-1背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 思路分析:该问题为0-1背包问题的变形题:问题求余额最少,设开始的余额为V,则求得用V-5可以买 ...
- css3的一些属性
以前还没有注意过css的一些属性,近期发现有一些样式很好用,现在整理一遍. CSS3 动画属性 @keyframes : 规定动画 可以通过keyframes 改变一个块的样式当然这是要配合anima ...