find the most comfortable road

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3240    Accepted Submission(s): 1373

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
 
Author
ailyanlu
 
Source
 
Recommend
8600   |   We have carefully selected several similar problems for you:  2722 2962 1301 2923 2482 
 
 //421MS    244K    1294 B    G++
/* 并查集+贪心:
个人觉得这种方法会TLE..但是出于在做并查集专题..
(时间复杂度应该是O(m*m*q*lgn)了,应该用最短路做比较好。)
小变异并查集:
先将数据按速度排好序,然后一个个的搜,搜到结束为止,再从中找
最优解。 */
#include<stdio.h>
#include<stdlib.h>
#define inf 0x7ffffff
struct node{
int a,b,v;
}g[];
int set[];
int cmp(const void*a,const void*b)
{
return (*(node *)a).v-(*(node *)b).v;
}
int find(int x)
{
if(set[x]==x) return x;
return find(set[x]);
}
int main(void)
{
int n,m,q;
int a,b,s;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<m;i++)
scanf("%d%d%d",&g[i].a,&g[i].b,&g[i].v);
qsort(g,m,sizeof(g[]),cmp);
//for(int i=0;i<m;i++) printf("*%d\n",g[i].v);
scanf("%d",&q);
while(q--){
int min=inf;
int i,j;
scanf("%d%d",&a,&b);
for(i=;i<m;i++){
for(j=;j<=n;j++) set[j]=j;
for(j=i;j<m;j++){ //爆搜
int x=find(g[j].a);
int y=find(g[j].b);
if(x!=y) set[y]=x;
if(find(a)==find(b)){ //找到路线,速度直接相减即得舒适度
int temp=g[j].v-g[i].v;
if(temp<min) min=temp;
break; //优化
}
}
if(j==m) break; //优化
}
if(min==inf) puts("-1");
else printf("%d\n",min);
}
}
return ;
}

hdu 1598 find the most comfortable road (并查集)的更多相关文章

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

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

  2. hdu 1598 find the most comfortable road (并查集+枚举)

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

  3. hdu 1598 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 (MST)

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

  6. hdu 1598 find the most comfortable road(并查集+枚举)

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

  7. HDU - 1598 find the most comfortable road 【最小生成树】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1598 思路 用kruskal 算法 将边排序后 跑 kruskal 然后依次将最小边删除 再去跑 kr ...

  8. HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合

    Problem Description XX星有很多城市,城市之间通过一种奇怪的快速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...

  9. HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)

    一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...

随机推荐

  1. 实现高可用-Keepalived

    简介 Keepalived是HA Cluster(High Availability Cluster,高可用集群)的一个服务软件,用来防止单点故障. Keepalived采用VRRP(virtual ...

  2. javaScript 字符串与unicode码之间的相互转换,函数的封装

    在我们的开发过程中,有时在对数据进行储存的时候,我们需要将字符串转成unicode. 比如,在jsp开发时,前端使用页面间传值时,将传值参数先存入cookie中,然后在使用的时候,再从ookie中取出 ...

  3. h5移动端页面meta标签

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...

  4. [转]Centos7 内核从3.10升级到4.12过程

    [原文地址] http://blog.csdn.net/youshijifen/article/details/73472434 [摘要] 近期,国家互联网应急中心漏洞(CNCERT)公告中提到Lin ...

  5. Andrew Ng Machine Learning Coursera学习笔记

    课程记录笔记如下: 1.目前ML的应用 包括:数据挖掘database mining.邮件过滤email anti-spam.机器人autonomous robotics.计算生物学computati ...

  6. 转译符,re模块,random模块

    一, 转译符 1.python 中的转译符 正则表达式中的内容在Python中就是字符串 ' \n ' : \ 转移符赋予了这个n一个特殊意义,表示一个换行符 ' \ \ n' :  \ \  表示取 ...

  7. u-boot.bin生成过程分析

    ELF格式“u-boot”文件的生成规则如下,下面对应Makefile的执行过程分别分析各个依赖. $(obj)u-boot: depend version $(SUBDIRS) $(OBJS) $( ...

  8. 各种数据库分页语句整理以及Oracle数据库中的ROWNUM和ORDER BY的区别

    .oracle数据库分页 select * from (select a.*,rownum rc from 表名 where rownum<=endrow) a where a.rc>=s ...

  9. poj1050 dp动态规划

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  10. 暗影精灵3安装无线网卡驱动(ubuntu16.04)

    干货,无线网卡安装步骤: 1. 由于暗影精灵3的无线网卡较新,版本为Realtek Device b822,(查看命令为lspci | grep -i net,Ethernet controller代 ...