| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
小心是多个CASE的,附小弟一边泡妞一边AC的代码,哈哈#include <iostream>
#define INF 9999999
#define MAX_VERTEX_NUM 110
struct
{
int adjvex;
int lowcost;
}closeedge[MAX_VERTEX_NUM];
int N;
int G[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
int Prim(int u)
{
int res=0;
for(int j=0;j<N;j++)
if(j!=u)
{
closeedge[j].adjvex=u;
closeedge[j].lowcost=G[u][j];
}
closeedge[u].lowcost=0;
for(int j=1;j<N;j++)
{
int mini=-1,minv=INF;
for(int i=0;i<N;i++)
if(minv>closeedge[i].lowcost&&closeedge[i].lowcost!=0)
{
mini=i;
minv=closeedge[i].lowcost;
}
res+=minv;
closeedge[mini].lowcost=0;
for(int i=0;i<N;i++)
if(closeedge[i].lowcost&&closeedge[i].lowcost>G[mini][i])
{
closeedge[i].adjvex=mini;
closeedge[i].lowcost=G[mini][i];
}
}
return res;
}
int main()
{
//freopen("c:/aaa.txt","r",stdin);
while(scanf("%d",&N)!=EOF)
{
for(int r=0;r<N;r++)
for(int c=0;c<N;c++)
scanf("%d",&G[r][c]);
int res=Prim(0);
printf("%d\n",res);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator