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 xx315 at 2015-05-20 15:15:37 on Problem 1258
就是赤裸裸的模版题啊
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0xfffff
#define exp 1e-9
int p[510],n,m,s,flag;
int lenth,ans;
struct e
{
    int a,b;
    int c;
}path[25000];
int cmp(e a,e b)
{
    return a.c<b.c;
}
int find(int k)
{
    while(p[k]!=k)
    {
        k=p[k];
    }
    return k;
}

void kruskal(int a,int b,int c)
{
    int f1=find(a);
    int f2=find(b);
    if(f1!=f2)
    {
        ans+=c;

        p[f1]=f2;
    }
}
void inti()
{
    for(int i=1;i<=n;i++)
        p[i]=i;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {   memset(path,0,sizeof(path));
        int num=1,te;
        inti();
        ans=0;

        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&te);
                if(i<j){
                path[num].a=i;
                path[num].b=j;
                path[num].c=te;
              //  printf("%d %d %d\n",i,j,te);
                num++;}
            }


        sort(path+1,path+num+1,cmp);

        for(int i=1;i<=num;i++)
        kruskal(path[i].a,path[i].b,path[i].c);
            printf("%d\n",ans);
    }
}

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