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

能过阿 94ms kruskal+并差集 第一次用kruskal过题,纪念

Posted by 201030720525 at 2011-04-21 21:42:38 on Problem 1861 and last updated at 2011-04-21 21:45:07
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 1005
#define maxm 15005
int n,m;
int p[maxn];
int num=0;
int path[maxm];

struct edge
{
    int u,v,w;
}edge[maxm];

bool cmp(struct edge p,struct edge q)
{
    if(p.w!=q.w)
        return p.w<=q.w;
    if(p.u!=q.u)
        return p.u<q.u;
    return p.v<=q.v;
}

int find(int x)
{
    return p[x]==x?x:p[x]=find(p[x]);
}

int kruskal()
{
    int i;
    int max=0;
    for(i=0;i<n;i++)
        p[i]=i;
    sort(edge,edge+m,cmp);
    for(i=0;i<m;i++)
    {
        if(num==n-1)
            break;
        int x=find(edge[i].u),y=find(edge[i].v);
        if(x!=y)
        {
            p[x]=y;
            if(max<edge[i].w)
                max=edge[i].w;
            path[num++]=i;
        }
    }
    return max;
}

int main()
{
    int i,j;
    scanf("%d%d",&n,&m);
    for(i=0;i<m;i++)
        scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
    printf("%d\n",kruskal());
    printf("%d\n",num);
    for(i=0;i<num;i++)
        printf("%d %d\n",edge[path[i]].u,edge[path[i]].v);
    return 0;
}



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