zoukankan      html  css  js  c++  java
  • hdu 1598 find the most comfortable road

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

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #define maxn 2000
     5 using namespace std;
     6 const int inf=1<<30;
     7 
     8 int f[maxn],n,m,q,a,b;
     9 struct node
    10 {
    11     int u,v,w;
    12     bool operator < (const node &a)const
    13     {
    14         return w<a.w;
    15     }
    16 }p[maxn];
    17 
    18 void inti()
    19 {
    20     for(int i=1; i<=n; i++)
    21     {
    22         f[i]=i;
    23     }
    24 }
    25 
    26 int find1(int x)
    27 {
    28     if(x==f[x]) return x;
    29     return f[x]=find1(f[x]);
    30 }
    31 
    32 void union1(int a,int b)
    33 {
    34     int fa=find1(a);
    35     int fb=find1(b);
    36     if(fa!=fb)
    37     {
    38         f[fa]=fb;
    39     }
    40 }
    41 
    42 int main()
    43 {
    44     while(scanf("%d%d",&n,&m)!=EOF)
    45     {
    46         for(int i=0; i<m; i++)
    47         {
    48             scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);
    49         }
    50         sort(p,p+m);
    51         scanf("%d",&q);
    52         while(q--)
    53         {
    54             scanf("%d%d",&a,&b);
    55             int min1=inf;
    56             int j;
    57             for(int i=0; i<m; i++)
    58             {
    59                 inti();
    60                 for(j=i; j<m; j++)
    61                 {
    62                     union1(p[j].u,p[j].v);
    63                     if(find1(a)==find1(b))
    64                     {
    65                         min1=min(min1,p[j].w-p[i].w);
    66                          break;
    67                     }
    68                 }
    69                 if(j==m) break;
    70             }
    71             if(min1==inf) printf("-1
    ");
    72             else
    73                 printf("%d
    ",min1);
    74         }
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    查找整数
    寒假作业3
    寒假作业2
    寒假作业1
    秋季学期总结
    对自己影响最深的三位老师
    自我介绍
    jquery学习笔记
    素材网站
    转:vim模式下报错E37: No write since last change (add ! to override)
  • 原文地址:https://www.cnblogs.com/fanminghui/p/3707748.html
Copyright © 2011-2022 走看看