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

y放代码在这里

Posted by 201030720525 at 2011-04-25 22:12:19 on Problem 1797
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxm 100005
#define maxn 1005
#define min(a,b) a<b?a:b
using namespace std;

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

int n,m;
int max,p[maxn];
int find(int x)
{
    return p[x]==x?x:p[x]=find(p[x]);
}

bool cmp(struct Edge a,struct Edge b)
{
    return a.w>b.w;
}

void init()
{
    for(int i=0;i<n;i++)
        p[i]=i;
}

int kruskal()
{
    int i;
    sort(edge,edge+m,cmp);
    int tot=0,ans=99999999;
    init();
    for(i=0;i<m&&tot<n-1;i++)
    {
        int x=find(edge[i].u),y=find(edge[i].v);
        if(x!=y)
        {
            ans=min(ans,edge[i].w);
            tot++;
            p[x]=y;
        }
    }
    return ans;
}

int main()
{
    int i,t,num=1;
    scanf("%d",&t);
    while(t--)
    {
        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("Scenario #%d:\n",num++);
        printf("%d",kruskal());
    }
    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