hdoj 1598 find the most comfortable road(并查集)
来源:互联网 发布:网络独家代理合同 编辑:程序博客网 时间:2024/04/29 09:28
【题目大意】:XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对 Flycar的“舒适度”有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ),
但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。(SARS是双向的)。
【解题思路】:并查集...对边权按从小到大进行一次排序,然后按照从小到大枚举最小边的并按照从小到大的顺序往加边,当被询问的两个点属于同一个集合的时候,势必加入的边是最大边,第一题加入的边是最小边,然后比较求最后答案即可。
【代码】:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>#include <cmath>#include <string>#include <cctype>#include <map>#include <iomanip> using namespace std; #define eps 1e-8#define pi acos(-1.0)#define inf 1<<30#define linf 1LL<<60#define pb push_back#define lc(x) (x << 1)#define rc(x) (x << 1 | 1)#define lowbit(x) (x & (-x))#define ll long longstruct Edge{ int u,v,w; Edge() {} Edge(int a,int b,int c) { u=a,v=b,w=c; }}edge[1100];int set[300];int n,m,ans;bool cmp(const Edge &a,const Edge &b){ return a.w<b.w;}void init(){ memset(set,-1,sizeof(set));}int findSet(int x){ if (set[x]==-1) return x; return set[x]=findSet(set[x]);}void unionSet(int x,int y){ int fx=findSet(x); int fy=findSet(y); if (fx!=fy) set[fx]=fy;}int main() { while (~scanf("%d%d",&n,&m)){ int a,b,c; for (int i=0; i<m; i++){ scanf("%d%d%d",&a,&b,&c); edge[i]=Edge(a,b,c); } sort(edge,edge+m,cmp); int t; scanf("%d",&t); for (int i=0; i<t; i++){ scanf("%d%d",&a,&b); ans=inf; for (int j=0; j<m; j++){ init(); int l; for (l=j; l<m; l++){ unionSet(edge[l].u,edge[l].v); if (findSet(a)==findSet(b)) break; } if (l==m) break; if (edge[l].w-edge[j].w<ans) ans=edge[l].w-edge[j].w; } if (ans==inf) printf("-1\n"); else printf("%d\n",ans); } } return 0;}
- hdoj 1598 find the most comfortable road(并查集)
- HDOJ 1598 find the most comfortable road 并查集
- hdu 1598 find the most comfortable road(并查集)
- hdu 1598 find the most comfortable road 并查集
- find the most comfortable road 1598 (并查集+枚举)
- HDOJ 题目1598 find the most comfortable road(并查集,枚举)
- hdoj preoblem 1598 find the most comfortable road (并查集&&动态规划)
- hdoj 1598 find the most comfortable road 【并查集】+【暴力枚举】
- HDOJ 1598 find the most comfortable road(并查集+枚举,贪心)
- HDOJ 1598 find the most comfortable road(并查集+贪心)
- hdoj 1598 find the most comfortable road【并查集&&暴力枚举】
- HDOJ--1598--find the most comfortable road(并查集+枚举)
- hdoj 1598 find the most comfortable road 【并查集+贪心】
- (并查集+枚举)find the most comfortable road -- HDOJ
- hdu1598 find the most comfortable road 并查集
- 【并查集】find the most comfortable road
- 【hdu】 find the most comfortable road (并查集)
- find the most comfortable road(并查集)
- c程序员,必须记口诀.
- AsyncTask的用法
- Android 属性系统 Property service 设定分析
- c# 线程池 多线程
- mysql中的临时表用在什么地方比较好
- hdoj 1598 find the most comfortable road(并查集)
- MySQL 5.1 mysqldump导出备份(转)
- c# 多线程下载文件
- windows和centos的目录互访
- Qt键盘事件
- 工作问题。面试
- 学习的第十天
- c#委托 的学习 - 写的特别清楚一学就会
- C++虚指针实现及效率