zoukankan      html  css  js  c++  java
  • poj2421Constructing Roads

    题目链接:http://poj.org/problem?id=2421

    还是模板题。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int maxn=110;
     6 int f[maxn];
     7 struct edge
     8 {
     9     int u,v,w;
    10     bool operator <(const edge &a)
    11     {
    12         return w<a.w;
    13     }
    14 }e[maxn*maxn];
    15 int n,m;
    16 int w,u,v;
    17 void init()
    18 {
    19     for(int i=1;i<=n;i++)
    20         f[i]=i;
    21 }
    22 int gf(int x)
    23 {
    24     return x==f[x]?x:f[x]=gf(f[x]);
    25 }
    26 void uni(int a,int b)
    27 {
    28     int pa=gf(a);
    29     int pb=gf(b);
    30     f[pb]=pa;
    31 }
    32 int main()
    33 {
    34     while(scanf("%d",&n)!=EOF)
    35     {
    36         init();
    37         int cnt=0;
    38         for(int i=1;i<=n;i++)
    39         for(int j=1;j<=n;j++){
    40             scanf("%d",&w);
    41             e[cnt].u=i;
    42             e[cnt].v=j;
    43             e[cnt].w=w;
    44             cnt++;
    45         }
    46         sort(e,e+cnt);
    47         scanf("%d",&m);
    48         for(int i=0;i<m;i++)
    49         {
    50             scanf("%d%d",&u,&v);
    51             uni(u,v);
    52         }
    53         int ans=0;
    54         for(int i=0;i<cnt;i++)
    55         {
    56             edge t=e[i];
    57             if(gf(t.u)!=gf(t.v))
    58             {
    59                 ans+=t.w;
    60                 uni(t.u,t.v);
    61             }
    62         }
    63         printf("%d
    ",ans);
    64 
    65     }
    66 }
  • 相关阅读:
    重构drf后的环境变量配置
    分离的前后台交互
    虚拟环境的搭建
    Python
    Python
    Python
    Python操作MongoDb数据库
    Python操作SQLite/MySQL/LMDB
    数据库-如何创建SQL Server身份验证用户
    Python
  • 原文地址:https://www.cnblogs.com/yijiull/p/6614114.html
Copyright © 2011-2022 走看看