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. nginx-2.nginx是什么

    Nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器: Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以 ...

  2. “全栈2019”Java多线程第三十二章:显式锁Lock等待唤醒机制详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  3. Dubbo原理实现之使用Javassist字节码结束构建代理对象

    JavassistProxyFactory利用自己吗技术构建代理对象的实现如下: public <T> T getProxy(Invoker<T> invoker, Class ...

  4. Python语法基础练习

  5. IntelliJ IDEA中Debug的使用技巧

    当运行结果跟我们设想的不一致时,我们就可以用debug进行代码调试,下面是我在日常开发中对debug的一些小结 (一)基本介绍 本篇文章是基于IntelliJ IDEA2018.3.2版本,Debug ...

  6. windows 64位下 Octave 不能画图的解决办法

    如果不能画图,可能需要更改图形工具包. 1.首先,查看当前的工具包.在Octave命令行中键入 graphics_toolkit,结果如下: >> graphics_toolkit    ...

  7. 线程中的同步辅助类Semaphore

    同步辅助类  线程池  并发集合类 都是在线程同步的基础上增加了一些同步的东西,在线程同步的基础上更好的实现线程同步.实现的效率更高,更方便而已. 多线程并不是很难 需要你把代码写出来...然后分析运 ...

  8. Apater适配器模式(结构型模式)

    1.概要 适配:即在不改变原有实现的基础上,将原先不适合的接口转换成适合的接口. what is Apater?适配,这个概念在生活中无处不在,比如你的iphone 4手机充电器坏了,这是时候只有一个 ...

  9. Anaconda 科学计算环境与包的管理

    相信大多数 python 的初学者们都曾为开发环境问题折腾了很久,包管理和 python 不同版本的问题,特别是 window 环境安装个 scrapy 各种报错 ,使用 Anaconda 可以很好的 ...

  10. android app性能优化大汇总

    这里根据网络上各位大神已经总结的知识内容做一个大汇总,作为记录,方便后续“温故知新”. 性能指标: (1)使用流畅度:  图片处理器每秒刷新的帧数(FPS),可用来指示页面是否平滑的渲染.高的帧率可以 ...