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

Problem Description
XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对 Flycar的“舒适度”有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ), 但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。(SARS是双向的)。
 
Input
输入包括多个测试实例,每个实例包括: 第一行有2个正整数n (1<n<=200)和m (m<=1000),表示有N个城市和M条SARS。 接下来的行是三个正整数StartCity,EndCity,speed,表示从表面上看StartCity到EndCity,限速为speedSARS。speed<=1000000 然后是一个正整数Q(Q<11),表示寻路的个数。 接下来Q行每行有2个正整数Start,End, 表示寻路的起终点。
 
Output
每个寻路要求打印一行,仅输出一个非负整数表示最佳路线的舒适度最高速与最低速的差。如果起点和终点不能到达,那么输出-1。
 
Sample Input
4 4
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2
 
Sample Output
1
0
 

一道不错的并查集的题!!!

当然,我第一次看这道题的时候,第一个想法,果断应该是并查集啊。

思路:

我们先把这个边按权值排序,然后从第j(j从0到m)条边开始枚举,然后枚举k边(k从j到0),如果从k到j的边能使起点和终点连通的话,那么最快速度与最慢速度的差一定是edge[j].weight - edge[k].weight,这样枚举,就可以把最小差求出来了,由于题目数据比较小,所以这样的枚举是可以过掉的。

ps:http://acm.hdu.edu.cn/showproblem.php?pid=1598

 
#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)不错的并查集的更多相关文章

  1. CodeForces242D:Connected Components (不错的并查集)

    We already know of the large corporation where Polycarpus works as a system administrator. The compu ...

  2. HDU-1598 find the most comfortable road

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. HDU1598 find the most comfortable road 【并查集】+【枚举】

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. HDU 1598 find the most comfortable road(最小生成树之Kruskal)

    题目链接: 传送门 find the most comfortable road Time Limit: 1000MS     Memory Limit: 32768 K Description XX ...

  5. hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  7. HDU 1598 find the most comfortable road (MST)

    find the most comfortable road Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  8. find the most comfortable road(并差集,找差值最小的权值)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. HDU 1589 Find The Most Comfortable Road 最小生成树+枚举

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. ASP.NET MVC一次删除多笔记录 V2.0

    前一段时间Insus.NET有写一篇<ASP.NET MVC一次删除多笔记录>http://www.cnblogs.com/insus/p/6241186.html 可以前往去看看. 觉得 ...

  2. Android Dagger 2

    Dagger 2 依赖注入 1. 基本概念 最重要有四个概念,也是四个注解(annotation),Provide,Inject,Module,Component. Provide 是提供者,创建实例 ...

  3. 解决 canvas 绘图在高清屏中的模糊问题

    解决 canvas 绘图在高清屏中的模糊问题 为什么模糊 CSS 像素是一个抽象单位(1 px),浏览器根据某种规则将 css 像素转化为屏幕需要的实际像素值. 在高清屏之前,屏幕上显示一个像素点需要 ...

  4. MySQL 排名统计(常用功能函数)

    select actor_id,@curr_cnt:=cnt as cnt , ,@rank) as rank, @prev_cnt:=@curr_cnt as dummy from( select ...

  5. python 结巴分词简介以及操作

    中文分词库:结巴分词 文档地址:https://github.com/fxsjy/jieba 代码对 Python 2/3 均兼容 全自动安装:easy_install jieba 或者 pip in ...

  6. Flask 初印象

    Flask 出生于2010年,集中了其他python web框架的长处,定位于微小项目上. 特点 1 内置开发服务器和调试器 2 于Python单元测试功能无缝衔接 Flask框架提供了一个与Pyth ...

  7. 轮播图采用js、jquery实现无缝滚动和非无缝滚动的四种案例实现,兼容ie低版本浏览器

    项目源代码下载地址:轮播图 以下为项目实现效果:(由于gif太大,所以只上传一张图片,但效果完全能实现,经测试,在ie各版本浏览器及chrome,firefox等浏览器中均能实现效果,可以实现点击切换 ...

  8. CentOS7.x编译安装nginx,实现HTTP2

    网站使用HTTP2有助于网站加速及更安全,要配置HTTP2必须满足两个条件:①openssl的版本必须在1.0.2e及以上.②nginx的版本必须在1.9.5以上 一.准备工作  配置HTTP2之前需 ...

  9. Android_OnLowMemory和OnTrimMemory

    Android OnLowMemory和OnTrimMemory OnLowMemoryOnLowMemory是Android提供的API,在系统内存不足,所有后台程序(优先级为background的 ...

  10. Git 管理项目

    一个很小的HTML项目,使用.Git来记录和跟踪这个项目.包括以下内容: 创建版本库. 添加与修改文件. 创建新分支. 打标签并整理版本库. 克隆版本库. 创建版本库 Creating a Repos ...