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

弱渣的第30题 贴代码

Posted by lyz963254 at 2014-08-15 17:36:08
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <stack>
using namespace std;

const int M = 1000 + 50;

int map[M][M];
int closest[M],lowcost[M];

bool s[M];

int i,j;
int n;

int  prim()
{
    s[1] = true ;
    for (i=1;i<=n;i++)
    {
        lowcost[i] = map[1][i];
        closest[i] = 1;
        s[i] = false;

    }

    for (i=1;i<=n;i++)
    {
        int temp = M;
         int t = 1;
         for (j=1;j<=n;j++)
         {
             if(!s[j] && lowcost[j]<temp  )
             {
                  temp = lowcost[j];
                  t = j;
             }
         }
         s[t] = true;
         for (j=1;j<=n;j++)
         {
             if ((!s[j]) && map[t][j] < lowcost[j] )
                 {
                     lowcost[j] = map[t][j];
                        closest[j] = t;
                 }
         }
    }
    int min = 0;
    for (i=1;i<=n;i++)
        min += lowcost[i];

    return min;
}

int main ()
{
  while (scanf("%d",&n) != EOF)
  {
       int m,a,b;
       int k = 0;

      memset(s,0,sizeof(s));

      for (i=1;i<=n;i++)
      {

          for (j=1;j<=n;j++)
            scanf("%d",&map[i][j]);

      }
      scanf("%d",&m);
      while (m--)
      {
          scanf("%d%d",&a,&b);
          map[a][b] = map [b][a] = 0; // 表示a,b已经连通。
      }
      k = prim() ;
      printf("%d",k);
    }
    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