zoukankan      html  css  js  c++  java
  • find the most comfortable road(并差集,找差值最小的权值)

    find the most comfortable road

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


    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
    题解:错了好多次啊。。。
    代码:
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 using namespace std;
     7 const int INF=0x7f7f7f7f;
     8 const int MAXN=210;
     9 const int MAXM=1010;
    10 int pre[MAXN];
    11 struct Node{
    12     int u,v,d;
    13 };
    14 Node dt[MAXM];
    15 int cmp(Node a,Node b){
    16     return a.d<b.d;
    17 }
    18 int find(int x){
    19     int r=x;
    20     while(r!=pre[r])r=pre[r];
    21 //    pre[x]=r;
    22     return r;
    23 }
    24 int main(){
    25     int n,m;
    26     while(~scanf("%d%d",&n,&m)){
    27         for(int i=0;i<m;i++){
    28             scanf("%d%d%d",&dt[i].u,&dt[i].v,&dt[i].d);
    29         }
    30         sort(dt,dt+m,cmp);
    31         int Q,s,e,f1,f2;
    32         scanf("%d",&Q);
    33         while(Q--){
    34             int ans=INF;//这个竟然被我写到外边 
    35             scanf("%d%d",&s,&e);
    36             for(int j=0;j<m;j++){
    37                 for(int i=1;i<=n;i++)pre[i]=i;
    38             for(int i=j;i<m;i++){
    39                 int u=dt[i].u,v=dt[i].v,d=dt[i].d;
    40                 f1=find(u);f2=find(v);
    41                 if(f1!=f2)pre[f2]=f1;
    42                 if(find(s)==find(e)){
    43                     ans=min(ans,d-dt[j].d);
    44                     break;
    45                 }
    46             }
    47         }
    48         if(ans==INF)puts("-1");
    49         else printf("%d
    ",ans);
    50         }
    51     }
    52     return 0;
    53 }
     
  • 相关阅读:
    JS进阶篇--函数防抖(debounce)
    vue注册全局组件分页
    vue下拉框清空
    vue中的watch监听
    五:request和response的使用
    四:servlet最终形态
    三:登录功能实现,servlet
    二:Tomcat与登录
    一:JavaWeb
    Linux安装JAVA并且配置环境
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4905686.html
Copyright © 2011-2022 走看看