zoukankan      html  css  js  c++  java
  • hust暑假集训2015 0713B Road System

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82557#problem/B

    WA了8次你敢信。。。

    觉得就是个最小生成树啊...

    如果不能构成一颗树,那么就not sure。。。

    需要注意n和m是反的。

    然后就是WA...

    WA到怀疑人生。。。

    被学长查代码才发现....

    原来当边数为0,我认为没有边数肯定是不连通的,所以都输出了Not sure。。。

    但是!

    如果只有一个点...即使不需要边,它本身就是联通的.....

    离散不好好听课的后果。。

    /*************************************************************************
        > File Name: code/2015summer/0713B.cpp
        > Author: 111qqz
        > Email: rkz2013@126.com
        > Created Time: 2015年07月13日 星期一 14时14分27秒
     ************************************************************************/
    
    #include<iostream>
    #include<iomanip>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<map>
    #include<set>
    #include<queue>
    #include<vector>
    #include<stack>
    using namespace std;
    #define REP(i, n) for (int i=0;i<int(n);++i)
    typedef long long LL;
    typedef unsigned long long ULL;
    const int N=1E4+5;
    int T,n,m;
    int f[N];
    struct node
    {
        int a,b,c;
    }r[N];
    bool cmp(node x,node y)
    {
        if (x.c<y.c) return true;
        return false;
    }
    void init()
    {
        for ( int i = 0 ; i < N ; i++)
          {
            f[i]=i;
            r[i].a=0;
            r[i].b=0;
            r[i].c=9999999;
          }
    }
    int root(int x)
    {
        if (x==f[x]) return x;
        else return f[x]=root(f[x]);
    }
    int main()
    {
        cin>>T;
        while (T--)
        {
            init();
            scanf("%d%d",&n,&m);
            for ( int i = 1 ; i <= n ; i++)
            {
              scanf("%d%d%d",&r[i].a,&r[i].b,&r[i].c);
            }
            sort(r+1,r+n+1,cmp);
            LL ans = 0;
            int cnt =0;
            bool flag=false;
            for (int i = 1 ; i <= n ; i++)
            {
              int x=root(r[i].a);
              int y=root(r[i].b);
              if (x!=y)
              {
                cnt++;
                ans = ans + r[i].c;
                f[x]=y;
              }
              if (cnt==m-1)
              {
                flag=true;
                break;
              }
            }
            if (flag)
            {
              cout<<ans<<endl;
            }
            else
            {
              if (m!=1)
              cout<<"I'm not sure."<<endl;
              else cout<<0<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    pch文件
    Info.plist常见的设置
    通知机制
    UITextField
    通过代码自定义cell(cell的高度不一致)
    Cell的重用原理
    UITableViewCell的contentView
    2019备考[嵌入式系统设计师]之“接口技术(上)”
    shell输入输出重定向问题
    [无私分享]最新网盘资源搜索站点
  • 原文地址:https://www.cnblogs.com/111qqz/p/4643078.html
Copyright © 2011-2022 走看看