hdu 1598 find the most comfortable road 枚举+并查集
来源:互联网 发布:重庆网络问政爆料平台 编辑:程序博客网 时间:2024/04/29 19:11
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598
题意:给出n个城市,m条路径,若路径中的最大速度和最小速度差越小则越舒适,求给定节点的最小差。
先对路径按速度排序,枚举路径作为最小速度,依次增加路径,直到两个城市连通,即可得到当前最小速度下最小差。取这些最小差的最小值即可。
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define N 1100#define INF 0x7ffffffusing namespace std;struct node{ int u,v,w;}e[N];int pre[N];bool cmp(node a,node b){ return a.w<b.w;}int findset(int v){ int t1,t2=v; while(v!=pre[v]) v=pre[v]; while(t2!=v) { t1=pre[t2]; pre[t2]=v; t2=t1; } return v;}void unions(int u,int v){ int t1=findset(u); int t2=findset(v); if(t1!=t2) pre[t1]=t2;}int main(){ int n,m,u,v,q; while(~scanf("%d%d",&n,&m)) { int maxw=0; for(int i=0;i<m;i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); sort(e,e+m,cmp); cin>>q; while(q--) { cin>>u>>v; int ans=INF; for(int i=0;i<m;i++) { for(int i=1;i<=n;i++) pre[i]=i; for(int j=i;j<m;j++) { unions(e[j].u,e[j].v); findset(u); findset(v); if(pre[u]==pre[v]) { ans=min(ans,e[j].w-e[i].w); break; } } } cout<<(ans==INF?-1:ans)<<endl; } }}
0 0
- find the most comfortable road 1598 (并查集+枚举)
- hdu 1598 find the most comfortable road(并查集)
- hdu 1598 find the most comfortable road 并查集
- hdu 1598 find the most comfortable road(并查集+枚举图的各边)
- 【并查集+枚举】杭电 hdu 1598 find the most comfortable road
- hdu 1598 find the most comfortable road(枚举+并查集)
- hdu 1598 find the most comfortable road(枚举+并查集)
- HDU 1598 Find the most comfortable road 二分枚举答案 或 并查集
- hdu 1598 find the most comfortable road (并查集 + 枚举)
- Hdu 1598 find the most comfortable road 并查集&枚举
- HDU 1598 find the most comfortable road 【枚举+(并查集)最小生成树Kruskal】
- HDU 1598 find the most comfortable road (枚举+Kruskal) 并查集
- HDU 1598 find the most comfortable road(并查集+枚举)
- HDU 1598--find the most comfortable road【并查集 + 枚举】
- hdu 1598 find the most comfortable road(并查集+枚举)
- hdu 1598 find the most comfortable road (并查集+枚举下界)
- hdu 1598 find the most comfortable road (并查集+枚举)
- hdu 1598 find the most comfortable road 枚举+并查集
- 2015年第六届蓝桥杯C/C++程序设计本科B组决赛 居民集会(编程大题)
- JAVA_WEB经典面试题
- CCNU 校赛J---分桶法
- LeetCode|Flatten Nested List Iterator
- Android官方文档之Bound Services
- hdu 1598 find the most comfortable road 枚举+并查集
- 2015年第六届蓝桥杯C/C++程序设计本科B组决赛 模型染色(编程大题)
- Regularization正则化
- codefores 352 D. Robin Hood
- 用WKWebView不使用的UIWebView
- H5初体验
- RDD实战
- eclipse使用MyBatisGenerator自动生成相关文件出错:project does not exist
- ubuntu下mysql安装及基本操作