find the most comfortable road(hdu1598)不错的并查集
find the most comfortable road
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2791 Accepted Submission(s): 1191
一道不错的并查集的题!!!
当然,我第一次看这道题的时候,第一个想法,果断应该是并查集啊。
思路:
我们先把这个边按权值排序,然后从第j(j从0到m)条边开始枚举,然后枚举k边(k从j到0),如果从k到j的边能使起点和终点连通的话,那么最快速度与最慢速度的差一定是edge[j].weight - edge[k].weight,这样枚举,就可以把最小差求出来了,由于题目数据比较小,所以这样的枚举是可以过掉的。
#include<cstdio>
#include<cstdlib>
#define MAX 1000001
#define min(a,b) (a)<(b)?(a):(b) struct Edge
{
int from;
int to;
int weight;
}edge[]; int n,m;
int father[]; int find(int x)
{
return x == father[x] ? x : father[x] = find(father[x]);
} void Union(int a,int b)
{
if(a!=b)
father[b]=a;
} int cmp(const void* a,const void* b)
{
return ((Edge*)a)->weight - ((Edge*)b)->weight;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int i,j,k,t;
for(i=;i<m;i++)
scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].weight);
qsort(edge,m,sizeof(Edge),cmp);
int q;
scanf("%d",&q);
for(i=;i<q;i++)
{
int from,to;
scanf("%d%d",&from,&to);
int ans = MAX;
for(j=;j<m;j++)
{
for(t=;t<m;t++)
father[t]=t;
for(k=j;k>=;k--)
{
Union(find(edge[k].from),find(edge[k].to));
if(find(from) == find(to))
{
ans = min(ans,edge[j].weight - edge[k].weight);
}
}
}
if(ans!=MAX)
printf("%d\n",ans);
else
printf("-1\n");
}
}
return ;
}
不知道下面这个怎么错了,求解啊!!!
#include<cstdio>
#include<iostream>
#include<cstdlib>
#define MAX 1000005
using namespace std;
#define min(a,b) (a)<(b)?(a):(b) struct Edge
{
int from;
int to;
int weight;
}edge[]; int n,m;
int father[]; int find(int x)
{
while(x!=father[x])
x=father[x];
return x;
} void Union(int a,int b)
{
if(a!=b)
father[b]=a;
} int cmp(const void* a,const void* b)
{
return ((Edge*)a)->weight - ((Edge*)b)->weight;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int i,j,k,t;
for(i=;i<m;i++)
scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].weight);
qsort(edge,m,sizeof(Edge),cmp);
int q;
scanf("%d",&q);
for(i=;i<q;i++)
{
int from,to;
scanf("%d%d",&from,&to);
int ans = MAX;
for(j=;j<m;j++)
{ for(t=;t<m;t++)
father[t]=t;
for(k=j;k>=;k--)
{
Union(find(edge[k].from),find(edge[k].to));
if(find(from) == find(to))
{
ans = min(ans,edge[j].weight - edge[k].weight);
}
}
}
if(ans!=MAX)
printf("%d\n",ans);
else
printf("-1\n");
}
}
return ;
}
不解a!
find the most comfortable road(hdu1598)不错的并查集的更多相关文章
- CodeForces242D:Connected Components (不错的并查集)
We already know of the large corporation where Polycarpus works as a system administrator. The compu ...
- HDU-1598 find the most comfortable road
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU1598 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 ...
- 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 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- HDU 1598 find the most comfortable road (MST)
find the most comfortable road Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- find the most comfortable road(并差集,找差值最小的权值)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1589 Find The Most Comfortable Road 最小生成树+枚举
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
随机推荐
- nginx-2.nginx是什么
Nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器: Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以 ...
- “全栈2019”Java多线程第三十二章:显式锁Lock等待唤醒机制详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- Dubbo原理实现之使用Javassist字节码结束构建代理对象
JavassistProxyFactory利用自己吗技术构建代理对象的实现如下: public <T> T getProxy(Invoker<T> invoker, Class ...
- Python语法基础练习
- IntelliJ IDEA中Debug的使用技巧
当运行结果跟我们设想的不一致时,我们就可以用debug进行代码调试,下面是我在日常开发中对debug的一些小结 (一)基本介绍 本篇文章是基于IntelliJ IDEA2018.3.2版本,Debug ...
- windows 64位下 Octave 不能画图的解决办法
如果不能画图,可能需要更改图形工具包. 1.首先,查看当前的工具包.在Octave命令行中键入 graphics_toolkit,结果如下: >> graphics_toolkit ...
- 线程中的同步辅助类Semaphore
同步辅助类 线程池 并发集合类 都是在线程同步的基础上增加了一些同步的东西,在线程同步的基础上更好的实现线程同步.实现的效率更高,更方便而已. 多线程并不是很难 需要你把代码写出来...然后分析运 ...
- Apater适配器模式(结构型模式)
1.概要 适配:即在不改变原有实现的基础上,将原先不适合的接口转换成适合的接口. what is Apater?适配,这个概念在生活中无处不在,比如你的iphone 4手机充电器坏了,这是时候只有一个 ...
- Anaconda 科学计算环境与包的管理
相信大多数 python 的初学者们都曾为开发环境问题折腾了很久,包管理和 python 不同版本的问题,特别是 window 环境安装个 scrapy 各种报错 ,使用 Anaconda 可以很好的 ...
- android app性能优化大汇总
这里根据网络上各位大神已经总结的知识内容做一个大汇总,作为记录,方便后续“温故知新”. 性能指标: (1)使用流畅度: 图片处理器每秒刷新的帧数(FPS),可用来指示页面是否平滑的渲染.高的帧率可以 ...