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 |
ac#include<stdio.h> #include<string.h> #include<math.h> #define M 800 #define INF 0x3f3f3f using namespace std; int n; double map[M][M]; int zhedouxing[M]; bool vis[M]; double dis[M]; struct node { int x; int y; }num[M]; node num2[M]; int av=0; void prim() { int i,j,k; memset(vis,false,sizeof(vis)); vis[1]=true; for(i=1;i<=n;i++) { dis[i]=map[1][i]; zhedouxing[i]=1; } for(i=1;i<n;i++) { double min=INF; for(j=1;j<=n;j++) { if(!vis[j]&&dis[j]<=min) { min=dis[j]; k=j; } } if(min!=0) { num2[av].x=zhedouxing[k]; num2[av].y=k; av++; } vis[k]=true; for(j=1;j<=n;j++) { if(!vis[j]&&dis[j]>map[k][j]) { dis[j]=map[k][j]; zhedouxing[j]=k; } } } } double len(int x1,int y1,int x2,int y2) { return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&num[i].x,&num[i].y); } int m; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { map[i][j]=len(num[i].x,num[i].y,num[j].x,num[j].y); } } scanf("%d",&m); for(int j=1;j<=m;j++) { int v,u; scanf("%d%d",&v,&u);//61 73578349 map[v][u]=0; map[u][v]=0; } prim(); for(int i=0;i<av;i++) { printf("%d %d\n",num2[i].x,num2[i].y); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator