Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴一波我的最小生成树做法,顺便提醒一下后来者,请忽略题目中那个空行。。。

Posted by Vibe at 2018-08-14 11:23:36 on Problem 3723
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long long sum;
int root[20005];
int Find(int x)
{
    if (x!=root[x])
        root[x]=Find(root[x]);
    return root[x];
}
void Merger(int a,int b)
{
    int r1=Find(a);
    int r2=Find(b);
    if (r1!=r2)
        root[r1]=r2;
}
struct edge
{
    int a,b,c;
    bool operator < (edge t) const
    {
        return c<t.c;
    }

} rr[50005];
int main()
{
    int n,m,r,x,y,d;
    int t;
    cin>>t;
    while(t--)
    {
        //printf("\n");      此处被坑
        scanf("%d%d%d",&n,&m,&r);
        sum=0;
        for (int i=0; i<n+m; ++i)
            root[i]=i;
        for (int i=0; i<r; ++i)
        {
            scanf("%d%d%d",&x,&y,&d);
            rr[i].a=x,rr[i].b=y+n,rr[i].c=10000-d;
        }
        sort(rr,rr+r);
//        for (int i=0;i<r;++i)
//            cout<<"##"<<rr[i].a<<' '<<rr[i].b<<' '<<rr[i].c<<endl;
        int cnt=0;
        for (int i=0; i<r; ++i)
        {
            if (Find(rr[i].a)!=Find(rr[i].b))
            {
//                cout<<"++++"<<rr[i].a<<' '<<rr[i].b<<endl;
                cnt++;
                sum+=rr[i].c;
                Merger(rr[i].a,rr[i].b);
                if (cnt==m+n-1)
                    break;
            }
        }
//        for (int i=0;i<m+n;++i)
//            cout<<"i="<<i<<' '<<"root[i]="<<root[i]<<endl;
        for (int i=0; i<m+n; ++i)
            if (root[i]==i)
                sum+=10000;
        printf("%lld\n",sum);
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator