HDU 1598 find the most comfortable road (枚举+Kruskal)

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1598

题目:

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

分析:

贪心的kruskal最小生成树算法思路, 把路径按照从小到大排序, 然后从小到大依次枚举“最小速度”, 再寻找目标起点到目标终点的路径中的“最大速度”,为了使得它们的差更小,就要使得“最大速度”尽量的小。

本栏目更多精彩内容:http://www.bianceng.cn/Programming/sjjg/

那么为了让Start和End有路径,只需要按照kruskal算法从"最小路径"开始构造生成树,一旦发现Start和End有连接了,那么就表示已经构成满足条件的那个路径。由于路径已经排序好,那么此时所枚举“起点”和“满足条件之点”之差便是最大速度与最小速度之差。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 210
#define INF 2147483646
using namespace std;  

int f[MAXN], rank[MAXN], n, m, pos;  

struct Edge{
    int u,v,val;
    friend bool operator<(const Edge&a,const Edge&b){
        return a.val < b.val;
    }
}arr[MAXN*MAXN];  

void init(){
    for(int i=0; i<MAXN; ++i)
        f[i]=i,rank[i]=0;
}
int find(int x){
    int i, j=x;
    while(j!=f[j]) j=f[j];
    while(x!=j){
        i=f[x]; f[x]=j; x=i;
    }
    return j;
}
void Union(int x, int y){
    int a=find(x), b=find(y);
    if(a==b)return;
    if(rank[a]>rank[b])
        f[b]=a;
    else{
        if(rank[a]==rank[b])
            ++rank[b];
        f[a]=b;
    }
}  

int main(){
    int u,v,speed,Q;
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=0; i<m; ++i)
            scanf("%d%d%d",&arr[i].u,&arr[i].v,&arr[i].val);
        sort(arr,arr+m);
        scanf("%d",&Q);
        for(int i=0; i<Q; ++i){
            scanf("%d%d",&u,&v);
            int ans=INF;
            for(int j=0; j<m; ++j){
                init();
                for(int k=j; k<m; ++k){
                    Union(arr[k].u,arr[k].v);
                    if(find(u)==find(v)){
                        ans = min(ans, arr[k].val-arr[j].val);
                        break;
                    }
                }
            }
            if(ans==INF)printf("-1\n");
            else printf("%d\n",ans);
        }
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索实例
, hdu1716 排序 oj
, 速度
, 整数
, 限速
, 枚举description
城市
hdu1598、kruskal算法、kruskal wallis检验、kruskal、kruskal wallis test,以便于您获取更多的相关知识。

时间: 2024-08-29 09:26:54

HDU 1598 find the most comfortable road (枚举+Kruskal)的相关文章

HDU 1595 find the longest of the shortest(枚举,最短路)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1595 题目: find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 667    Accepted Submission(s): 220 Problem Description Ma

HDU 4081 Qin Shi Huang&#039;s National Road System (次小生成树算法)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=4081 题目: Problem Description During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China ---- they were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the

HDU 2363 Cycling:二分+枚举+限制最短路,好题

链接: http://acm.hdu.edu.cn/showproblem.php?pid=2363 题目大意: 小明从家里走到学校去考试, 路上有各个交叉点,它们有自己的海拔高度. 小明从家里走到学校的路上,必然会经过不同的交叉点,因此会不断的走上走下,忐忐忑忑,这让他很不安,会影响他考试的发挥.因此,他要选择一条起伏最小的路去学校.所谓的"起伏最小",是指这条路上海拔最高的点与海拔最低的点的差值最小. 在起伏最小的前提下,还要求出路程距离最短. 分析与总结: 这题让我想起以前做过的

HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)

链接: HDU : http://acm.hdu.edu.cn/showproblem.php?pid=2489 POJ  : http://poj.org/problem?id=3925 题目: Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a comp

HDU 4353 枚举

题意:给出n个点,为商人要购买的点,m个点为金矿的位置.问如何使够买三个点或三个以上的点围成的多边形面积与多边形内金矿的数量的比值最小. 这题很容易想到比值最小的肯定是三角形和在三角形内的点的数量想比.虽然我没想到.然后很容易想到四重循环来找最小的比值但是会超时,所以需要预处理一下,先把两组点按照x轴排序,枚举两个n点,针对于每组点组成的线段选线段正上方的m点,存入数组中.然后再进行n^3循环枚举3个n内的点,长线段上的m点数-两条短线段的m点数的绝对值就是三角形内的点数.为什么是绝对值,因为长

hdu 1077 Catching Fish 计算几何+暴力枚举

   简单的暴力枚举,枚举两个点在圆上,用向量法求下圆心.复杂度o(n^3),但数据量只有300 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <c

HDU 4380 预处理枚举

题意:给出n个房子m个矿问从n个房子选三个组成的三角形内部矿数为奇数有多少种选法. 先预处理一下每条线段正上方有多少个点,然后在枚举三条线段就可以了. #include <iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; struct point { long long x,y; }; int

HDU 3823 暴力枚举

题意:给出A,B, 找出一个最小的m,使A+m,B+m为连续的两个素数. 枚举2000W以内的素数暴力找. #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define maxn 21000000 bool isprime[maxn]; long long prime[maxn],nprime; void getp

HDU 4569 长沙E题 枚举

题意:给你函数 f(x) = anxn +...+ a1x +a0 最多N就4位,输入任意一个x使f(x)%(prime*prime)=0. 这题枚举就可以,首先如果满足f(x)%(prime*prime)=0必须要满足f(x)%prime=0这个条件. 那么应该先找到一个x满足f(x)%prime=0,然后在(x-prime*prime)区间内x+=prime(保证f(x)%prime=0总成立),如果有f(x)%(prime*prime)=0那么就输出. 找出一个x在(0-prime)区间内