zoukankan      html  css  js  c++  java
  • 【luogu P1111 公路修建】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1111

    考察并查集,运用kruskal的思想很好做。注意几个小问题即可。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 int n,m,fa[1000010];
     6 int ans=0,cnt=0;
     7 struct node{
     8     int x,y,t; 
     9 }s[1000010];
    10 int cmp(node a,node b)
    11 {
    12     return a.t<b.t;
    13 }
    14 int find(int x)
    15 {
    16     return fa[x]==x?x:fa[x]=find(fa[x]);
    17 }
    18 int main()
    19 {
    20     scanf("%d%d",&n,&m);
    21     for(int i=1;i<=n;i++)
    22     fa[i]=i;
    23 
    24     for(int i=1;i<=m;i++)
    25     scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].t);
    26     sort(s+1,s+1+m,cmp);
    27 
    28     for(int i=1;i<=m;i++)
    29     {
    30         int xx=find(s[i].x);
    31         int yy=find(s[i].y);
    32         if(xx!=yy)
    33         {
    34             fa[yy]=xx;
    35             cnt++;
    36         }
    37         if(cnt==n-1)//注意是点数-1,点数-1条边生成的图是树
    38         {
    39             cout<<s[i].t;//因为已经是排好序的,所以直接是当前的边序号
    40             return 0;
    41         }
    42     }
    43 
    44     cout<<-1;
    45     return 0;
    46 
    47 }

    隐约雷鸣,阴霾天空,但盼风雨来,能留你在此。

    隐约雷鸣,阴霾天空,即使天无雨,我亦留此地。

  • 相关阅读:
    WCF简单使用(分别部署在控制台和IIS上)
    WCF系列教程之WCF服务配置工具
    053547
    053546
    053545
    053544
    053543
    053542
    053541
    053540
  • 原文地址:https://www.cnblogs.com/MisakaAzusa/p/8469910.html
Copyright © 2011-2022 走看看