| ||||||||||
| 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 | |||||||||
Re:求用邻接矩阵做的PRIM算法的函数或程序,实在写不出了,谢谢In Reply To:求用邻接矩阵做的PRIM算法的函数或程序,实在写不出了,谢谢 Posted by:wangshicai100 at 2005-07-14 21:24:12 /*
Source
Problem Id:1751
Language:G++ Result:Accepted
*/
#include <stdio.h>
#define MAXN 750
int connect[MAXN+1][MAXN+1];
int len[MAXN+1][MAXN+1];
int point[MAXN+1][2];
int n,m;
int ok[MAXN+1];
int father[MAXN+1];
int shortRoad[MAXN+1];
void Prime()
{
int i,pre;
for(i=1;i<=n;i++)
{
shortRoad[i] = -1;
}
shortRoad[1] = 0;
ok[1] = 1;
pre = 1;
while(pre != 0)
{
ok[pre] = 1;
for(i=1;i<=n;i++)
{
if(ok[i]==0 && (shortRoad[i]<0 || shortRoad[i]>len[pre][i]))
{
shortRoad[i] = len[pre][i];
father[i] = pre;
}
}
pre = 0;
for(i=1;i<=n;i++)
{
if(ok[i]==0 && shortRoad[i]>=0 && (pre==0 || shortRoad[i]<shortRoad[pre]))
{
pre = i;
}
}
if(pre != 0)
{
if(connect[pre][father[pre]] == 0)
{
printf("%d %d\n",pre,father[pre]);
}
}
}
}
int main()
{
int i,j,a,b;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d %d",&point[i][0],&point[i][1]);
}
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d %d",&a,&b);
connect[a][b] = connect[b][a] = 1;
}
for(i=1;i<=n;i++)
{
len[i][i] = 0;
for(j=i+1;j<=n;j++)
{
if(connect[i][j] == 1)
{
len[i][j] = len[j][i] = 0;
}
else
{
len[i][j] = len[j][i] = (point[i][0]-point[j][0])*(point[i][0]-point[j][0])+
(point[i][1]-point[j][1])*(point[i][1]-point[j][1]);
}
}
}
Prime();
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator