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. Fence(codeforces 232D)

    题意: 对于给定的a[1..n],定义区间[s,t]和[x,y]"匹配"当且仅当下列条件同时满足:1. t-s=y-x,即长度相同.3. t<x或s>y,即两区间没有交 ...

  2. C结构体struct用法小结

    结构体和int,float等类型一样是一种常用的类型,它是由各种基本数据类型构成,通常包含有struct关键字,结构体名,结构体成员,结构体变量. 一.结构体定义 通常有3种定义方式,以例子方式表示: ...

  3. centos 7 安装git并配置ssh

    一.安装 1.查看是否安装git rpm -qa|grep git 有git加版本号就说明已经安装过了 2.安装git yum install git 3.查看git版本 git version 二. ...

  4. LeetCode OJ-- Container With Most Water

    https://oj.leetcode.com/problems/container-with-most-water/ 不同高度的柱子排一列,两个柱子可以组成一个容器,求最大容积. 最直观的方法就是暴 ...

  5. 查询/删除/建立DB2数据表的主键

    一.查询表主键. describe indexes for table <instancename>.<tablename> 例: describe indexes for t ...

  6. 复习FBV与CBV

    在学习DRF之前~我们要先复习一些知识点~~ FBV和CBV 学习Django的时候~我们已经学习过了CBV以及FBV~~我们来复习一下~~ 什么是FBV和CBV呢~~ FBV 基于函数的视图 CBV ...

  7. Codeforces Gym101063 C.Sleep Buddies (2016 USP-ICMC)

    C.Sleep Buddies It is nighttime in the Earth Colony on Mars and everyone is getting ready to sleep. ...

  8. Codeforces Gym101473 F.Triangles-前缀和 (2013-2014 ACM-ICPC Brazil Subregional Programming Contest)

    前缀和. 代码: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include< ...

  9. Codeforces 631B Print Check (思维)

    题目链接 Print Check 注意到行数加列数最大值只有几千,那么有效的操作数只有几千,那么把这些有效的操作求出来依次模拟就可以了. #include <bits/stdc++.h> ...

  10. 从壹开始 [ Ids4实战 ] 之四 ║ 用户数据管理 & 前后端授权联调

    前言 哈喽~~~ 大家周一好!夏天到了,大家舒服了没有,熟话说,打败你的不是天真,是天真热!