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

G++ 就AC 。。。C++ 就RE。。。Kruskal……

Posted by dongshimou at 2014-07-04 16:44:37 on Problem 1258
搞不懂了。数组都开到1001.边都开到500*1001 了。


求路过大神看看,谢谢。


#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
using namespace std;
int n;
struct lx
{
    int u,v,len;
} l[500*1001];
int fa[1001];
bool cmp(lx a,lx b)
{
    return a.len<b.len;
}
int father(int x)
{
    if(x!=fa[x])
        return fa[x]=father(fa[x]);
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<=n; i++)
            fa[i]=i;
        int cot=0;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                int len;
                scanf("%d",&len);
                if(i==j)continue;
                l[cot].u=i;
                l[cot].v=j;
                l[cot++].len=len;
            }
        sort(l,l+cot,cmp);
        int ans=0;

        for(int i=0; i<cot; i++)
        {
            int fu=father(l[i].u);
            int fv=father(l[i].v);
            if(fu==fv)continue;

            fa[fv]=fu;
            ans+=l[i].len;
        }
        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