PKU ACM 2485 Highways http://acm.pku.edu.cn/JudgeOnline/problem?id=2485

一次性ac,算法-----prim,书中的讲解不太详细而且有错误,希望有困难的acmer看看我的文章prim;

我的代码虽然说不是很精湛但也能勉强ac,有兴趣的可以看一下:

#include <iostream>
using namespace std;
const int Max=65536;
int min_edge (int a[],int n)
{
int min=Max;
int index;
for (int i=0;i<n;i++)
{
    if (a[i]!=0&&a[i]<min)
{
min =a[i];
    index=i;
}
}
return index;
}
int main ()
{
int m;
int u=0;
int a[500][500]={0};
cin >>m;
int i=0;
while (i!=m)
{
   int n;
   int max=0;   
   int lowcost[501];
   int closet [501];
   cin >>n;
   for (int j=0;j< n;j++)
    for (int k=0;k< n;k++)
    { 
     int p; 
     scanf("%d",&p);
if (p==0&&j!=k)
{
   a[j][k]=Max;
}
else a[j][k]=p;
    }
for (int p=0;p<n;p++)
{
   lowcost[p]=a[0][p];
   closet [p]=0;
}
closet[0]=0;
   for (int h=1;h<n;h++)
   {
    int index=min_edge(lowcost,n);
    if (lowcost[index]>max)
    {
     max = lowcost[index];
    }
    lowcost[index]=0;
    for (int q=1;q<n;q++)
    {
     if (a[index][q]!=0&&lowcost[q]>a[index][q])
     {
      lowcost[q]=a[index][q];
         closet[q]=index;
     }
    }
   }
   cout<<max<<endl;
   i++;
}
return 0;
}

posted on 2011-05-06 19:19  _Clarence  阅读(131)  评论(0编辑  收藏  举报

导航