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 ...
随机推荐
- ASP.NET MVC一次删除多笔记录 V2.0
前一段时间Insus.NET有写一篇<ASP.NET MVC一次删除多笔记录>http://www.cnblogs.com/insus/p/6241186.html 可以前往去看看. 觉得 ...
- Android Dagger 2
Dagger 2 依赖注入 1. 基本概念 最重要有四个概念,也是四个注解(annotation),Provide,Inject,Module,Component. Provide 是提供者,创建实例 ...
- 解决 canvas 绘图在高清屏中的模糊问题
解决 canvas 绘图在高清屏中的模糊问题 为什么模糊 CSS 像素是一个抽象单位(1 px),浏览器根据某种规则将 css 像素转化为屏幕需要的实际像素值. 在高清屏之前,屏幕上显示一个像素点需要 ...
- MySQL 排名统计(常用功能函数)
select actor_id,@curr_cnt:=cnt as cnt , ,@rank) as rank, @prev_cnt:=@curr_cnt as dummy from( select ...
- python 结巴分词简介以及操作
中文分词库:结巴分词 文档地址:https://github.com/fxsjy/jieba 代码对 Python 2/3 均兼容 全自动安装:easy_install jieba 或者 pip in ...
- Flask 初印象
Flask 出生于2010年,集中了其他python web框架的长处,定位于微小项目上. 特点 1 内置开发服务器和调试器 2 于Python单元测试功能无缝衔接 Flask框架提供了一个与Pyth ...
- 轮播图采用js、jquery实现无缝滚动和非无缝滚动的四种案例实现,兼容ie低版本浏览器
项目源代码下载地址:轮播图 以下为项目实现效果:(由于gif太大,所以只上传一张图片,但效果完全能实现,经测试,在ie各版本浏览器及chrome,firefox等浏览器中均能实现效果,可以实现点击切换 ...
- CentOS7.x编译安装nginx,实现HTTP2
网站使用HTTP2有助于网站加速及更安全,要配置HTTP2必须满足两个条件:①openssl的版本必须在1.0.2e及以上.②nginx的版本必须在1.9.5以上 一.准备工作 配置HTTP2之前需 ...
- Android_OnLowMemory和OnTrimMemory
Android OnLowMemory和OnTrimMemory OnLowMemoryOnLowMemory是Android提供的API,在系统内存不足,所有后台程序(优先级为background的 ...
- Git 管理项目
一个很小的HTML项目,使用.Git来记录和跟踪这个项目.包括以下内容: 创建版本库. 添加与修改文件. 创建新分支. 打标签并整理版本库. 克隆版本库. 创建版本库 Creating a Repos ...