zoukankan      html  css  js  c++  java
  • 并查集

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

    https://zh.wikipedia.org/wiki/%E5%B9%B6%E6%9F%A5%E9%9B%86

    http://www.cnblogs.com/nanke/archive/2012/02/13/2350008.html

    想不到并查集这样用

    对于题目中的路,按速度进行排序,这样,如果从第 i 条路到第 j 条路之间的所有路能够让 i 和 j 连通,那么,这就存在一条路,且这条路的舒适度就是 两者的差值。  这样,只要枚举从每一条路开始,向前找到可以使得 起点和终点连通的路的舒适度,就可以找到答案了

    代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <math.h>
     4 #include <limits.h> 
     5 #include <algorithm>
     6 #include <iostream>
     7 #include <ctype.h>
     8 #include <iomanip>
     9 #include <queue>
    10 #include <map>
    11 #include <stdlib.h>
    12 using namespace std;
    13 
    14 int f[201],n,m;
    15 struct lu
    16 {
    17     int startcity,endcity,speed;
    18 }num[1010];
    19 
    20 bool cmp(lu a,lu b)
    21 {
    22     return a.speed < b.speed;
    23 }
    24 
    25 void init()
    26 {
    27     for(int i=0;i<=n;i++)
    28         f[i]=i;
    29 }
    30 
    31 int find(int x)
    32 {
    33     if(x==f[x])
    34         return f[x];
    35     f[x]=find(f[x]);
    36     return f[x];
    37 }
    38 
    39 void Union(int x,int y)
    40 {
    41     int a=find(x);
    42     int b=find(y);
    43     if(a==b)
    44         return; 
    45     f[a]=b;
    46 }
    47 
    48 int main()
    49 {
    50     int p;
    51     while(scanf("%d %d",&n,&m)==2){
    52         for(int i=0;i<m;i++)
    53             scanf("%d %d %d",&num[i].startcity,&num[i].endcity,&num[i].speed);
    54         sort(num,num+m,cmp);
    55         scanf("%d",&p);
    56         while(p--){
    57             int x,y;
    58             int ans=INT_MAX;
    59             scanf("%d %d",&x,&y);
    60             for(int i=0;i<m;i++){
    61                 init();
    62                 int flag=0,k=0;
    63                 for(int j=i;j<m;j++){
    64                     Union(num[j].startcity,num[j].endcity);
    65                     if(find(x)==find(y)){
    66                         flag=1;
    67                         k=j;
    68                         break;
    69                     }
    70                 }
    71                 if(!flag)
    72                     break;
    73                 ans=min(ans,num[k].speed-num[i].speed);
    74             }
    75             if(ans==INT_MAX)
    76                 printf("-1
    ");
    77             else
    78                 printf("%d
    ",ans);
    79         }
    80     }
    81     return 0;
    82 }

     PS:#include <limits.h> 
    這一行所載入的表頭檔為一些關於整數值檔案極限大小的定義, 
    此表頭檔裡有一行 
    #define INT_MIN(-INT_MAX-1) 
    意思就是定義了INT_MIN這個字的範圍為(-INT_MAX-1) 
    而INT_MAX的範圍則在前一行被定義為 2147483647 
    #define INT_MAX2147483647 
    故 你的INT_MAX在這裡的時候的數值是 (-2147483647-1) 
    就是"-2147483648"啦! 
    那你輸入任何數跟他比較的話,他一定是最小的, 

  • 相关阅读:
    Mac OS X下Maven的安装与配置
    [MAC Eclipse] Eclipse for MAC 中文乱码的解决办法
    The type javax.servlet.http.HttpServletRequest cannot be resolved.
    IOS基础:深入理解Objective-c中@class的含义
    NSJSONSerialization-JSON数据与NSDictionary和NSArray之间的转化
    真机测试时的错误:No matching provisioning profiles found
    转帖Jmeter中的几个重要测试指标释义
    Spring集成log4j日志管理
    Log4J日志配置详解
    使用Redis的理由
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/4850654.html
Copyright © 2011-2022 走看看