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 ...
随机推荐
- WINCE下进程间通信(二)
WINCE下进程间通信(二) 接着前面的文章<WINCE下进程间通信(一)>,现在介绍进程间通信的另一种方法. 三.管道(消息队列) WINCE并不支持类似于PC机上匿名管道.命名管道的通 ...
- 可信执行环境(TEE)介绍
可信执行环境(TEE)是Global Platform(GP)提出的概念.针对移动设备的开放环境,安全问题也越来越受到关注,不仅仅是终端用户,还包括服务提供者,移动运营商,以及芯片厂商.TEE是与设备 ...
- Kubernetes 1.4 部署
k8s 1.4 新版本部署 测试环境: node-: 10.6.0.140 node-: 10.6.0.187 node-: 10.6.0.188 kubernetes 集群,包含 master 节点 ...
- git archive
git archive --format zip --output ../g.zip 3.4.2 git archive --format=tar \ --remote=ssh://remote_se ...
- html 自定义标签的作用
比如早期的时候,如果浏览器不支持 nav 这个标签的话, style标签中的样式 nav {color: yellow}会不起作用,字体不会变黄. 怎么处理兼容性呢? 用 javascript. 加上 ...
- System.InvalidCastException: 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。
报错:System.InvalidCastException: 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接 ...
- BNU OJ 50998 BQG's Messy Code
#include <cstdio> #define _(l) int l #define ___(l,L) for (_(o)=0,x=l o*2;o<x;o++)O= L o; # ...
- nginx 内存池分析
最近nginx的源码刚好研究到内存池,这儿就看下nginx内存池的相关的东西. 一,为什么要使用内存池 大多数的解释不外乎提升程序的处理性能及减小内存中的碎片,对于性能优化这点主要体现在: (1)系统 ...
- ZBUS = MQ + RPC
http://git.oschina.net/rushmore/zbus http://my.oschina.net/sbz/blog Readme.md 18.02 KB ZBUS = MQ + ...
- 解决SSh连接过慢
ssh 连接的时候特别慢 解决方法: 修改配置文件 /etc/ssh/sshd_config GSSAPIAuthentication yes --->no 81行左右 #UseDNS yes ...