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 ...
随机推荐
- python WEB接口自动化测试之requests库详解
由于web接口自动化测试需要用到python的第三方库--requests库,运用requests库可以模拟发送http请求,再结合unittest测试框架,就能完成web接口自动化测试. 所以笔者今 ...
- web容器启动顺序
web容器启动顺序: 第一:context-param 第二:Listerer 第三:Filter 第四:servlet
- 计算机学院大学生程序设计竞赛(2015’12) 1002 Polygon
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #inclu ...
- SpringMVC文件上传报错org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest
错误信息: java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to or ...
- [算法] trie树实现
小写字母的字典树 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 1 ...
- JavaScript 逗号表达式
逗号表达式的一般形式是:表达式1,表达式2,表达式3……表达式n 逗号表达式的求解过程是:先计算表达式1的值,再计算表达式2的值,……一直计算到表达式n的值.最后整个逗号表达式的值是表达式n的值. ...
- BP神经网络的Java实现
http://fantasticinblur.iteye.com/blog/1465497
- margin 相关 bug 系列
原文地址:margin系列之bug巡演 by @doyoe IE6浮动双倍margin bug 这当是IE6最为经典的bug之一. 触发方式 元素被设置浮动 元素在与浮动一致的方向上设置margin值 ...
- MediaScanner
http://blog.csdn.net/hellofeiya/article/details/8255898 http://www.cnblogs.com/halzhang/archive/2011 ...
- 控制流之if
if语句if语句用来检验一个条件, 如果 条件为真,我们运行一块语句(称为 if-块 ), 否则 我们处理另外一块语句(称为 else-块 ). else 从句是可选的. ~~~~~~~~~~~~~~ ...