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

Re:坑爹,需要考虑重边!!!

Posted by 1871168524 at 2014-11-13 23:27:11 on Problem 2377
In Reply To:坑爹,需要考虑重边!!! Posted by:1871168524 at 2014-11-13 23:24:39
> 坑爹,需要考虑重边!!!
代码如下:

#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
int map[1005][1005],visit[1005],dis[1005],n;
void prim()
{
	int length=0,point=1,max,i,j;
    for(i=1;i<=n;i++)
       dis[i]=map[point][i];
    memset(visit,0,sizeof(visit));
    visit[point]=1;
    for(i=1;i<n;i++)
    {
    	max=-INF;
    	for(j=1;j<=n;j++)
    	  if(!visit[j]&&dis[j]>max)
    	  {
    	  	max=dis[j];
    	  	point=j;
    	  }
    	if(max==-INF) 
		{
		   printf("-1\n");
		   return ;
	    }
    	length+=max;
    	visit[point]=1;
    	for(j=1;j<=n;j++)
    	  if(!visit[j]&&dis[j]<map[point][j])
    	     dis[j]=map[point][j];
    }
    printf("%d\n",length);
}
int main()
{
	int m,i,j,p,q,c;
	while(~scanf("%d%d",&n,&m))
	{
		for(i=1;i<=n;i++)
		  for(j=1;j<=n;j++)
		    map[i][j]=-INF;
		while(m--)
		{
			scanf("%d%d%d",&p,&q,&c);
			if(map[p][q]==-INF) map[p][q]=map[q][p]=c;
			else if(map[p][q]<c) map[p][q]=map[q][p]=c;
		}
		prim();
	}
	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