hdu 1598 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): 4899 Accepted Submission(s): 2131
但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, 表示寻路的起终点。
4 4
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2
1 0 中文题目就不用说题意了
并查集,排序加枚举,下边是我理解大神代码猴,自己加的凝视。,唉,,仅仅怪自己太菜
2015,。7,,22#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int sc,ec,speed;
}a[1100];
int f[210];
int n,m; bool cmp(node a,node b){ return a.speed<b.speed; }
void init() { for(int i=0;i<210;i++) f[i]=i; }
int find(int k) { if(f[k]!=k) k=find(f[k]); return k; }
void merge(int a,int b){ a=find(a); b=find(b); if(a!=b) f[a]=b; }
int slove(int s,int e)
{
int i,j,ans;
init();
for(i=0;i<m;i++)//推断是否连通
{
merge(a[i].sc,a[i].ec);
}
if(find(s)!=find(e)) return -1;//假设不连通就返回-1
ans=0x3f3f3f;
for(i=0;i<m;i++)//以每条边为起始边。这条边权就是这条线的最小速度
{
init();
for(j=i;j<m;j++)//依次增加边。直到连通。ans存放最小的差值
{
if(a[j].speed-a[i].speed>=ans) continue;
merge(a[j].sc,a[j].ec);
if(find(s)==find(e))//假设起点和终点连通时 ,此时的a[j].speed就是这条路线的最大速度
{
if(ans>a[j].speed-a[i].speed)
ans=a[j].speed-a[i].speed;
}
}
}
return ans;
}
int main(){
int i,t,s,e;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a[i].sc,&a[i].ec,&a[i].speed);
}
sort(a,a+m,cmp);//按权值排序
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&s,&e);
printf("%d\n",slove(s,e));
}
}
return 0;
}
hdu 1598 find the most comfortable road(并查集+枚举)的更多相关文章
- 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 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- 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(枚举+卡鲁斯卡尔最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 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 (MST)
find the most comfortable road Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- 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) 并检查集合
Problem Description XX星有很多城市,城市之间通过一种奇怪的快速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...
- HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)
一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- poj 3250 Bad Hair Day
Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21084 Accepted: 7202 Des ...
- Dictionary字典类使用范例
原文发布时间为:2009-11-04 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Web.UI.WebControls;using System ...
- AspNetPager分页控件的使用以及常见错误
原文发布时间为:2009-05-25 -- 来源于本人的百度文章 [由搬家工具导入] 【Repeater采用AspNetPager分页成功↓】 using System;using System.Co ...
- window golang1.12.4 安装配置&IDE vscode
step 1:下载golang golang官网地址是 https://golang.org/dl/ 如果没有墙的话,可以用这个地址 https://golang.google.cn/dl/ 如图: ...
- springBoot 读取配置文件yml中的信息
yml中自定义一些变量 var: analyze_url: test ocr_url: test microsoft_key: test 映射到类变量中 @Getter @Component publ ...
- CCCC L2-010. 排座位【并查集/分类讨论】
L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...
- Codeforces 912D Fishes (概率&期望,优先队列的应用)
题目链接 Fishes 题意 在一个$n*m$的矩阵中,随机选择一个$r * r$的区域覆盖. 一开始我们可以在这个$n*m$的矩阵中选择$k$个点标记为$1$. 我们要选择一个最佳的标记策略,使得 ...
- GCJ——Minimum Scalar Product(2008 Round1 AA)
题意: 给定两组各n个数,可任意调整同一组数之中数字的顺序,求 sum xi*yi i=1..n的最小值. Small: n<=8 abs xy,yi<=1000 Large: n< ...
- php中for与foreach对比
总体来说,如果数据库过几十万了,才能看出来快一点还是慢一点,如果低于10万的循环,就不用测试了.php推荐用foreach.循环数字数组时,for需要事先count($arr)计算数组长度,需要引入自 ...
- new子类会先运行父类的构造函数
发现子类构造函数运行时,先运行了父类的构造函数.为什么呢? 原因:子类的所有构造函数中的第一行,其实都有一条隐身的语句super(); super(): 表示父类的构造函数,并会调用于参数相对应的父类 ...