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 |
double 在HDU可以过啊 精度也考虑了 修改成int 完全正确 过了 看来double得少用啊#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator