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

ac

Posted by 940111490 at 2015-08-13 17:58:25 on Problem 1751 and last updated at 2015-08-13 17:58:31
#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:
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