HDU-1598-find the most comfortable road(暴力+并查集)多看看,
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1598
题目思路:对于这个题目,可以先按速度的大小成小到大排序,
再成0 到 m ,把所有可以联通的道路全部暴搜一遍,一但联通,
两者的min=两者的速度差,依次成0 到 找 m 找,如果找到更最小的就取代min
最后的min一定是最小的,
看代码
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int father[222];
const int M=999999999;
int n,m;
struct node
{
int x,y,d;
} s[1111];
bool cmp(const node &a,const node &b)
{
return a.d<b.d;
}
int Find(int x)
{
if(x==father[x]) return x;
father[x]=Find(father[x]);
return father[x];
}
void Union(int x,int y)
{
x=Find(x);
y=Find(y);
if(x!=y)
{
father[x]=y;
}
}
int slove(int x,int y)
{
int min,max,i,j,k;
min=M;
for(i=0; i<m; i++)
{
for(j=1; j<=n; j++)
father[j]=j;
for(j=i; j<m; j++)
{
Union(s[j].x,s[j].y);
if(Find(x)==Find(y))//一旦两者联通,两者速度即相减
{
max=s[j].d-s[i].d;// 两者速度即相减。
if(max<min)
{
min=max;//取最小的差值。
break;
}
}
}
}
if(min==M) return -1;
else return min;
}
int main(void)
{
int i,j,k,l;
int x,y,q;
while(scanf("%d%d",&n,&m)==2)
{
for(i=0; i<m; i++)
scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].d);
sort(s,s+m,cmp);
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&x,&y);
printf("%d\n",slove(x,y));
}
}
return 0;
}
HDU-1598-find the most comfortable road(暴力+并查集)多看看,的更多相关文章
- hdu 1598 find the most comfortable road(并查集+枚举)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)
一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...
- hdu 1598 find the most comfortable road(并查集)
题意:略 分析:多询问问题,利用并查集加速.类似于kruskal对MST的构建:枚举最小的边,逐渐将更大的边加入集合,当查询的点在同一个集合,那么当前最小值,就是所加的最后一条边与第一条只差. 注意: ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- hdu 1598 find the most comfortable road (并查集+枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...
- hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1598 find the most comfortable road (MST)
find the most comfortable road Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 1598 find the most comfortable road (并查集)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1598 find the most comfortable road(最小生成树之Kruskal)
题目链接: 传送门 find the most comfortable road Time Limit: 1000MS Memory Limit: 32768 K Description XX ...
- HDU1598 find the most comfortable road 【并查集】+【枚举】
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
随机推荐
- 总结自己的Git常用命令
总结自己的Git常用命令 使用git也有一段时间了,把自己常用的命令用自己的描述记录起来,方便自己备忘也方便其他人参考. 目录: 最基本的命令: git clone 拷贝并跟踪远程的master分支. ...
- JAVA字符串编码转换常用类
无论是对程序的本地化还是国际化,都会涉及到字符编码的转换的问题.尤其在web应用中常常需要处理中文字符,这时就需要进行字符串的编码转换,将字符串编码转换为GBK或者GB2312.一.关键技术点: ...
- mr本地运行的几种模式
MR程序的几种提交运行模式 本地模型运行 1/在windows的eclipse里面直接运行main方法,就会将job提交给本地执行器localjobrunner执行 ----输入输出数据可以放在本地路 ...
- Jackson基础笔记
具体内容待完善......手抖,发错了! 一.基本使用 1. bean->jsonStr 2. jsonStr->bean 二.注解使用 三.复杂对象转换 四.其他细节 读取json文本.
- 用Date.ToString()输出中英文月份
DateTime.Now.ToString("dddd,dd MMMM,yyyy")//输出 星期三,30 一月,2008DateTime.Now.ToString(" ...
- Android Guts: Intro to Loopers and Handlers
One of the reasons I love Android API is because it contains so many useful little things. Many of t ...
- js 中调用 Object.prototype.toString()来检测对象的类型
1.使用toString()方法来检测对象类型 可以通过toString() 来获取每个对象的类型.为了每个对象都能通过 Object.prototype.toString() 来检测,需要以 Fun ...
- CodeForces 590B Chip 'n Dale Rescue Rangers
这题可以o(1)推出公式,也可以二分答案+验证. #include<iostream> #include<cstring> #include<cmath> #inc ...
- ICE BOX 配置,使用----第一篇
一 理论部分 (1) 为什么要使用icebox? icebox server代替了通常的server. icebox是为了方便集中管理多个ice服务而建立的. 它通过使用icebox服务器,把ice服 ...
- (中等) POJ 1436 Horizontally Visible Segments , 线段树+区间更新。
Description There is a number of disjoint vertical line segments in the plane. We say that two segme ...