zoukankan      html  css  js  c++  java
  • HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合

    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

    # include<iostream>
    # include<cstdio>
    # include<algorithm>
    using namespace std;
    const int maxn=1000+5;
    struct node{
    int st;
    int ed;
    int fu;
    }road[maxn];
    int p[maxn];
    int st,ed;
    int miin;
    int inf=1000000+5;
    bool cmp(node x,node y)
    {
        return x.fu<y.fu;
    }
    int find(int x)
    {
        return p[x]== x ?

    x:p[x]=find(p[x]); } int main() { int n,m; while(cin>>n>>m) { for(int i=1;i<=m;i++) scanf("%d%d%d",&road[i].st,&road[i].ed,&road[i].fu); sort(road+1,road+m+1,cmp); int tot; cin>>tot; while(tot--) { miin=inf; scanf("%d%d",&st,&ed); for(int i=1;i<=m;i++) { for(int i=1;i<=n;i++) p[i]=i; for(int j=i;j<=m;j++) { int x=find(road[j].st); int y=find(road[j].ed); if(x!=y) p[x]=y; if(find(st)==find(ed)) { int sum=road[j].fu-road[i].fu; if(sum<miin) miin=sum; break; } } } if(miin==inf) printf("-1 "); else printf("%d ",miin); } } return 0; }


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    Android 2.2 r1 API 中文文档系列(11) —— RadioButton
    Android API 中文 (15) —— GridView
    Android 中文 API (16) —— AnalogClock
    Android2.2 API 中文文档系列(7) —— ImageButton
    Android2.2 API 中文文档系列(6) —— ImageView
    Android 2.2 r1 API 中文文档系列(12) —— Button
    Android2.2 API 中文文档系列(8) —— QuickContactBadge
    [Android1.5]TextView跑马灯效果
    [Android1.5]ActivityManager: [1] Killed am start n
    Android API 中文(14) —— ViewStub
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4754986.html
Copyright © 2011-2022 走看看