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. 在.net中修改Webbrowser控件的IE版本

    根据32位.64位系统来分别修改对应的注册表路径的键值对,不需要重启程序. /// <summary> /// 修改Webbrowser控件模拟的IE版本 /// </summary ...

  2. @JsonInclude、@JsonFormat、@DateTimeFormat注解的使用

    @JsonInclude(value=Include.NON_NULL) :用在实体类的方法类的头上  作用是实体类的参数查询到的为null的不显示 @DateTimeFormat:用于接收 前端传的 ...

  3. Django中安装搜索引擎方法。

    全文检索 全文检索不同于特定字段的模糊查询,使用全文检索的效率更高,并且能够对于中文进行分词处理. haystack:全文检索的框架,支持whoosh.solr.Xapian.Elasticsearc ...

  4. Windows上安装tensorflow 详细教程

    原博客转载自:https://www.cnblogs.com/lvsling/p/8672404.html 一, 前言:本次安装tensorflow是基于Python的,安装Python的过程不做说明 ...

  5. 56.storm 之 hello world (集群模式)

    回顾 在上一小节,我们在PWTopology1 这一个java类中注解掉了集群模式,使用本地模式大概了解一下storm的工作流程.这一节我们注解掉本地模式相关的代码,放开集群模式相关代码,并且将项目打 ...

  6. 题外话 -- windows10系统C盘空间变大 CPU莫名跑满

    场景描述: 安装windows10一段时间了,发现C盘空间越来越小 CPU有时候,莫名其妙的跑满,造成操作卡顿. 如何处理参考: windows10 C盘空间清理:https://jingyan.ba ...

  7. c++获取随机数

    方法一: 使用 rand 函数可以获取,如下. 随机数大小是在0到RAND_MAX,值为2147483647,它是在stdlib中定义的,如果我们希望在某个范围内,可以使用 % 结合 / 来实现. 但 ...

  8. Zookeeper--0200--安装与集群搭建、常用命令、客户端工具

    看这里,http://www.cnblogs.com/lihaoyang/p/8358153.html 1,先使用可视化客户端软件 ZooInspector 连接上集群中的一个节点,看下zk的结构: ...

  9. php使用 utf8_encode 来将特殊字符转成 utf8

    如果在接受 $_POST 或 $_GET 时发生类似的错误报告:SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xF6te ...

  10. Spring总结 2.装配bean

    本随笔内容要点如下: bean的作用域 占位符 一.bean的作用域 在默认情况下,Spring管理的bean的单例的.也就是说,无论注入多少次,都是同一个bean对象.一般情况下,单例模式是足以应付 ...