hdu 1598 find the most comfortable road
来源:互联网 发布:《大数据时代》电子书 编辑:程序博客网 时间:2024/06/07 12:42
路径从小到大排序,然后开始枚举边,构建最小生成树,如果包含了要求的起点和终点,即 father【u】==father【v】 ,求得此时的差值,取个最小值就是了
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<iostream>#define inf 999999999using namespace std;struct edge{int u,v,w;}e[1100];int father[500000];int n,m;bool cmp(const edge &a,const edge &b){return a.w<b.w;};int findroot(int p){if(father[p]!=p)father[p]=findroot(father[p]);return father[p];}void unionset(int p,int q){father[q]=p;}int main(){while(scanf("%d%d",&n,&m)!=EOF){int i,j;for(i=0;i<m;i++){scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);}sort(e,e+m,cmp);int q;scanf("%d",&q);while(q--){int u,v;int ans=inf;scanf("%d%d",&u,&v);for(i=0;i<m;i++){for(j=0;j<=n;j++)father[j]=j;for(j=i;j<m;j++){int a=findroot(e[j].u);int b=findroot(e[j].v);if(a!=b)father[b]=a;if(findroot(u)==findroot(v)){ans=min(ans,e[j].w-e[i].w);}}}if(ans==inf)printf("-1\n");else printf("%d\n",ans);}}return 0;}
阅读全文
0 0
- find the most comfortable road hdu 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
- 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
- 最常用的四种大数据分析方法
- 编辑距离
- P和NP问题
- Java学习笔记-ArrayList(2)和LinkedList
- DELPHI XE 调用 DELPHI 7 写的DLL的问题
- hdu 1598 find the most comfortable road
- 代价函数总结
- Python中关于str()函数和repr()函数的那些事
- 构建Linux驱动开发环境全过程(X210bv3)
- 基于@AspectJ和Schema的AOP(精通Spring+4.x++企业应用开发实战 第八章)
- 博客竟然被黑了
- 智力题:烧一根不均匀的绳子,从头烧到尾是要1个小时.现在有若干条材质相同的绳子 问如何用烧绳的方法来计时一个小时15分钟.
- 适配IE浏览器的那些坑
- Burp Suite中proxy的简单用法