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

double 在HDU可以过啊 精度也考虑了 修改成int 完全正确 过了 看来double得少用啊

Posted by helihui at 2010-05-18 22:54:43 on Problem 2421
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define eps 1e-10

#define maxn 110 //总最多顶点个数
#define inf 9999999 //无穷大
double map[maxn][maxn],disk[maxn];//顶点之间的关系-map邻接矩阵

double prim(int n,int s)
{
    int visit[maxn],i,w,j;
    double min=inf,sum=0;
    memset(visit,0,sizeof(visit));
    visit[s]=1;
    for(i=0;i<n;i++)
        disk[i]=map[0][i];
    for(i=1;i<n;i++)
    {
        min=inf;w=-1;
        for( j=1;j<n;j++)
        {
            if(!visit[j]&&(min-disk[j]>=eps))
            {min=disk[j];w=j;}
            
        }
        if(w==-1)return -1;
        visit[w]=1;
        sum+=min;
        for(j=1;j<n;j++)
        {
            if(!visit[j]&&(disk[j]-map[w][j]>=eps))
                disk[j]=map[w][j];
        }
    }
    return sum;
}
double prim_solve(int n,int s)//使用prim 函数
{
	double sum=0;
	
	sum=prim(n,s);
	return sum;
	
}

int main()
{
	int n,i,j,q;
	int a,b;
	double sum,x;
	while(scanf("%d",&n)!=EOF)
	{sum=0;
		//init();
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
			{
				scanf("%lf",&x);
					map[i][j]=x;
				
			}
		scanf("%d",&q);
		for(i=0;i<q;i++)
		{
			scanf("%d%d",&a,&b);
			map[a-1][b-1]=map[b-1][a-1]=0;
		}
		sum = prim( n, 0);
		printf("%.0lf\n",sum);
		
	}
	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