hdu 1598 find the most comfortable road (并查集 + 枚举)
来源:互联网 发布:aris软件 编辑:程序博客网 时间:2024/05/16 07:23
题目:
链接:点击打开链接
思路:
对边排序,再枚举每条边,如果出现通路(findset(x) == findset(y))就结束。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define MAXN 220#define MAXM 1010#define MAX 100000000struct node{ int s,e,w;}edge[MAXM];int root[MAXN];int n,m,q;int cmp(node a,node b){ return a.w < b.w;}int findset(int x){ return root[x] == x ? x : root[x] = findset(root[x]);}void mergeset(int x,int y){ root[findset(x)] = findset(y);}void init(){ for(int i=1; i<=n; i++) root[i] = i;}int main(){ //freopen("input.txt","r",stdin); int x,y; int minn; while(scanf("%d%d",&n,&m) != EOF) { int i,j; for(i=0; i<m; i++) { scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].w); } sort(edge,edge+m,cmp); scanf("%d",&q); while(q--) { scanf("%d%d",&x,&y); minn = MAX; for(i=0; i<m; i++) { init(); for(j=i; j<m; j++) { mergeset(edge[j].s,edge[j].e); if(findset(x) == findset(y)) break; } if(j == m) break; if(minn > edge[j].w - edge[i].w) minn = edge[j].w - edge[i].w; } if(minn == MAX) printf("-1\n"); else printf("%d\n",minn); } } return 0;}
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 枚举+并查集
- Swift学习之十四:闭包(Closures)
- Jenkins发不出邮件
- 算法竞赛入门经典 第五章 因子和阶乘
- 随机数是骗人的,.Net、Java、C为我作证
- eclipse中导入android项目报错
- hdu 1598 find the most comfortable road (并查集 + 枚举)
- 网站测试流程,要求以及测试报告!
- HUNNU Contest 数组求和问题
- 还是畅通工程 (HDU 1233)
- 在ubuntu下使用deb安装的tomcat7+mysql
- Android Recovery:功能简介。Recovery模式介绍
- 链接,静态库,动态库
- 用JAVA实现"猜数字"游戏过程中的一点心得——编程时如何思考
- PLL的重要性