find the most comfortable road

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4899    Accepted Submission(s): 2131

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 中文题目就不用说题意了
并查集,排序加枚举,下边是我理解大神代码猴,自己加的凝视。,唉,,仅仅怪自己太菜
2015,。7,,22
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int sc,ec,speed;
}a[1100];
int f[210];
int n,m; bool cmp(node a,node b){ return a.speed<b.speed; }
void init() { for(int i=0;i<210;i++) f[i]=i; }
int find(int k) { if(f[k]!=k) k=find(f[k]); return k; }
void merge(int a,int b){ a=find(a); b=find(b); if(a!=b) f[a]=b; }
int slove(int s,int e)
{
int i,j,ans;
init();
for(i=0;i<m;i++)//推断是否连通
{
merge(a[i].sc,a[i].ec);
}
if(find(s)!=find(e)) return -1;//假设不连通就返回-1
ans=0x3f3f3f;
for(i=0;i<m;i++)//以每条边为起始边。这条边权就是这条线的最小速度
{
init();
for(j=i;j<m;j++)//依次增加边。直到连通。ans存放最小的差值
{
if(a[j].speed-a[i].speed>=ans) continue;
merge(a[j].sc,a[j].ec);
if(find(s)==find(e))//假设起点和终点连通时 ,此时的a[j].speed就是这条路线的最大速度
{
if(ans>a[j].speed-a[i].speed)
ans=a[j].speed-a[i].speed;
}
}
}
return ans;
}
int main(){
int i,t,s,e;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a[i].sc,&a[i].ec,&a[i].speed);
}
sort(a,a+m,cmp);//按权值排序
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&s,&e);
printf("%d\n",slove(s,e));
}
}
return 0;
}

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(枚举+卡鲁斯卡尔最小生成树)

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

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

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

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

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

  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. Object.keys(obj)

    对象.keys 很明显是获得对象的key的一个数组 数组的key arr = ['a', 'b', 'c']; console.log(Object.keys(arr)); // console: [ ...

  2. jquery 实践操作:iframe 相关操作

    此篇记录关于HTML 的 iframe 元素 的相关记录 定义:iframe 元素会创建包含另外一个文档的内联框架(即行内框架). 常用的基本 iframe 设置(详细设置属性参考API:http:/ ...

  3. 最长k可重区间集(cogs 743)

    «问题描述:«编程任务:对于给定的开区间集合I和正整数k,计算开区间集合I的最长k可重区间集的长度.«数据输入:由文件interv.in提供输入数据.文件的第1 行有2 个正整数n和k,分别表示开区间 ...

  4. POJ1385 Lifting the Stone

    There are many secret openings in the floor which are covered by a big heavy stone. When the stone i ...

  5. linux 源代码目录结构

    Linux源代码目录树结构 (2008-04-21 09:14) 分类: Linux/Unix Linux用来支持各种体系结构的源代码包含大约4500个C语言程序,存放在270个左右的子目录下,总共大 ...

  6. iptables之centos6版本详解

    1 Linux防火墙概述 Linux防火墙实际指的是Linux下的Netfilter/Iptables.Netfilter/Iptables是2.4.x/2.6.x版本Linux内核集成的IP信息包过 ...

  7. LeetCode OJ--Copy List with Random Pointer **

    https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 灵活的指针链表应用. 每个节点有两个指针next,random,对本链表 ...

  8. nodejs后台启动

    可避免关闭窗口,程序就关闭,可在后台运行 安装forever包,一般用于服务器,调试环境可不安装 npm install forever -g 启动方式如图: 查询后台运行哪些程序 forever l ...

  9. Codeforces Gym101473 E.Dangerous Dive (2013-2014 ACM-ICPC Brazil Subregional Programming Contest)

    代码: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<cmat ...

  10. Codeforces 777C Alyona and Spreadsheet(思维)

    题目链接 Alyona and Spreadsheet 记a[i][j]为读入的矩阵,c[i][j]为满足a[i][j],a[i - 1][j], a[i - 2][j],......,a[k][j] ...