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 h8214111 at 2014-02-16 00:06:40 on Problem 1258
#include<cstdio>
#include<cstring>
#define INF 100000000
#define maxn 105

int n,sum;
int map[maxn][maxn];
int used[maxn];
int dis[maxn];

void Prim()
{
    int i, j, k, min;
    memset(used, 0, sizeof(used));
    used[1] = 1;
    for(i=1; i<=n; i++)
        dis[i] = map[1][i];
    for(i=1; i<n; i++)
    {
        min=INF;
        for(j=1; j<=n; j++)
        {
            if(!used[j] && dis[j]<min)
            {
                min=dis[j];
                k=j;
            }
        }
        used[k] = 1;
        sum+=min;
        for(j=1; j<=n; j++)
        {
            if(!used[j] && dis[j]>map[k][j])
            {
                dis[j]=map[k][j];
            }
        }
    }
}

int main()
{
    int i, j;
    while(scanf("%d",&n)!=EOF)
    {
        sum = 0;
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                scanf("%d", &map[i][j]);
            }
        }
        Prim();
        printf("%d\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