题意:给出一幅无向图,每次询问给出起点和终点,求起点到终点权值差最小的一条路,输出权值差;

思路:边按权值排序,利用并查集枚举每次能使起点终点联通的情况下权值差,并每次更新最小值;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int fa[500010],n,m;
struct node
{int u,v,val;friend bool operator<(const node &a,const node &b){return a.val<b.val;}
}arr[500010];
void init()
{for(int i=0;i<n;i++){fa[i]=i;}
}
int fin(int x)
{if(fa[x]==x) return x;return fa[x]=fin(fa[x]);
}
void combine(int a,int b)
{int t1=fin(a);int t2=fin(b);if(t1!=t2)fa[t2]=t1;
}
int main()
{int i,j,k,a,b,q,ans,flag;while(scanf("%d%d",&n,&m)!=EOF){for(i=0;i<m;i++)scanf("%d%d%d",&arr[i].u,&arr[i].v,&arr[i].val);sort(arr,arr+m);//按速度从小到大排序scanf("%d",&q);for(i=0;i<q;i++){scanf("%d%d",&a,&b);ans=INF;for(j=0;j<m;j++)//枚举恰能使两点联通的边数
            {init();for(k=j;k>=0;k--){combine(arr[k].u,arr[k].v);if(fin(a)==fin(b)){ans=min(ans,arr[j].val-arr[k].val);//联通时,求最小差值
                    }}}if(ans==INF) printf("-1\n");else printf("%d\n",ans);}}return 0;
}

转载于:https://www.cnblogs.com/dashuzhilin/p/4418271.html

hdu 1598 find the most comfortable road相关推荐

  1. hdu 1598 find the most comfortable road (并查集+枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...

  2. HDOJ 1598 find the most comfortable road(并查集+贪心)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. HDU1598 find the most comfortable road【枚举+并查集+最大生成树】

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  4. ACM--steps--dyx--5.1.4--find the most comfortable road

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  5. 图论500题 ---- (枚举+并查集)求图上路径权值差值最小 HDU find the most comfortable road

    题目链接 解题思路: 对于两个点的路径假设我们把边按照边权进行排序那么,再把边依次加入图中判断两点的联通性,那么这个边就是两个点联通的最小的最大值,但是最大的最小值怎么找? 因为我们是把边权按照从小到 ...

  6. hdu 1598(最小生成树变形)

    解题思路:这道题我一开始的思路是动规,dp_max[i][j]表示i-j的最大边,dp_min[i][j]表示i-j的最小边,可是这样会有问题,有可能最大边与最小边不在同一条路径上,这样就很麻烦了. ...

  7. find the most comfortable road

    XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Spee ...

  8. Find the most comfortable road(HDU-1598)

    Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶 ...

  9. HDU - 1598

    XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super AirRoam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed ...

最新文章

  1. 使用Application.GetResourceStream从XAP安装包加载任意资源
  2. JavaScript text highlighting JQuery plugin
  3. C++中了类继承和调用父类的构造函数方法
  4. 计算1+2+……+N的和。
  5. equals()方法
  6. 资源下载】512页《神经网络与深度学习》(附下载链接)
  7. HDU4536+DFS
  8. 删除dedecms友情链接中li标签的方法
  9. Windows常用运行库(VC++、DirectX、.NET)
  10. CSOL控制台与FPS优化命令大全
  11. C语言程序设计-鸡兔同笼问题
  12. Android性能优化 _ 大图做帧动画卡?优化帧动画之 SurfaceView滑动窗口式帧复用
  13. 【重识云原生】计算第2.2节——主流虚拟化技术之VMare ESXi
  14. Unity3D——加入剑痕效果(PocketRPG Trail插件)
  15. 了解JavaScript中的Promise
  16. Codejock Suite Crack,工作区更易于管理
  17. MAVLink协议详解
  18. 怎么查找计算机的内存卡,怎么查看电脑内存卡型号
  19. 基于python的校园寝室管理系统设计与实现
  20. 遥感学习笔记(二)——地物反射波谱特征

热门文章

  1. 祝51CTO 生日快乐
  2. Upgrade after a crash is not supported. The redo log was created with Maria的解决办法
  3. mac开启sshserver
  4. mysql 唯一索引_MySQL学会用索引,让你数据库的查询速度起飞
  5. Python内置函数之输入输出函数
  6. Python+selenium+firefox模拟登录微博并爬取数据(1
  7. 8道Python基础面试练习题
  8. pythonpil库过滤图像contour_快乐python 零基础也能P图 —— PIL库
  9. oracle 字符集 自造字,循序渐进Oracle:自定义字符集的使用、更改字符集的内部方式及字符集更改的案例...
  10. 防抖函数Debounce实现