hdu 1598(最小生成树)
find the most comfortable road
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5884 Accepted Submission(s): 2554
星有许多城市,城市之间通过一种奇怪的高速公路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 <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
const int M = ;
const int INF = ;
int father[N];
struct Edge{
int s,e,len;
}edge[M];
int n,m;
int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
}
int cmp(Edge a,Edge b){
return a.len<b.len;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=m;i++){
scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].len);
}
sort(edge+,edge+m+,cmp);
int t;
scanf("%d",&t);
while(t--){
int s,e;
scanf("%d%d",&s,&e);
int Min = INF;
for(int i=;i<=m;i++){ ///枚举起始边
for(int j=;j<=n;j++){
father[j] = j;
}
for(int j=i;j<=m;j++){ ///枚举结束边
int x = _find(edge[j].s);
int y = _find(edge[j].e);
if(x!=y) father[x]=y;
if(_find(s)==_find(e)){
Min = min(Min,edge[j].len-edge[i].len);
break;
}
}
}
if(Min>=INF) printf("-1\n");
else printf("%d\n",Min);
}
}
}
hdu 1598(最小生成树)的更多相关文章
- HDU 1233(最小生成树)
HDU 1233(最小生成树 模板) #include <iostream> #include <algorithm> #include <cstdio> usin ...
- HDU - 1598 find the most comfortable road 【最小生成树】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1598 思路 用kruskal 算法 将边排序后 跑 kruskal 然后依次将最小边删除 再去跑 kr ...
- 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(枚举+并查集,类似于最小生成树)
一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...
- HDU 1598 find the most comfortable road (最小生成树) >>
Problem Description XX明星有许多城市,通过与一个陌生的城市高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...
- HDU 1102 最小生成树裸题,kruskal,prim
1.HDU 1102 Constructing Roads 最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
随机推荐
- Java与Scala的两种简易版连接池
Java版简易版连接池: import java.sql.Connection; import java.sql.DriverManager; import java.util.LinkedList; ...
- 亲手搭建一个基于Asp.Net WebApi的项目基础框架2
本篇目的:封装一些抽象类 1::封装日志相关类 2:封装一个Service操作类 3:封装缓存操作类 4:封装其他一些常用Helper 1.1在Framework项目里面建立好相关操作类文件夹,以便于 ...
- lintcode
public class Solution { /** * @param s: The first string * @param b: The second string * @return tru ...
- 编译gearman提示缺少boost
编译german时提示缺少boost: checking for boostlib >= 1.39... configure: We could not detect the boost lib ...
- JavaScript简易学习笔记
学习地址:http://www.w3school.com.cn/js/index.asp 文字版: https://github.com/songzhenhua/github/blob/master/ ...
- MySQL、MongoDB、Redis 数据库之间的区别与使用(本章迭代更新)
MySQL.MongoDB.Redis 数据库之间的区别与使用 MySQL.MongoDB.Redis 数据库之间的区别与使用(本章迭代更新) update:2019年2月20日 15:21:19(本 ...
- 课时5:闲聊之Python的数据类型
目录: 一.引言 二.数据类型 >整型 >浮点型 >布尔类型 三.类型转换 四.获得关于类型的信息 五.课时05课后习题及答案 *********** 一.引言 ********** ...
- 【多线程学习(1)】创建java多线程
1)java多线程的创建方式有三种: 1.继承Thread类 2.实现Runnable接口 3.实现Callable接口 第一种: //继承Thread类 class ExtendsThread ex ...
- 【bzoj4636】蒟蒻的数列 离散化+线段树
原文地址:http://www.cnblogs.com/GXZlegend/p/6801379.html 题目描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个 ...
- JS获取照片拍摄的角度属性,用于旋转控制
我工作十余年,从负责一个模块,到负责一个产品,再到负责整个支付平台的架构设计,包括业务架构.产品架构到应用架构,再到技术架构,是一个从点到面逐渐转型的过程,同样是个“自相似”的现象,我一开始写博客,再 ...