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

大牛,帮帮忙,看看这个程序,wrong了无数此了,不明白

Posted by DarkKnight at 2006-04-15 10:50:20 on Problem 2075
#include <stdio.h>
#include <string.h>
float a;
char s[1000][21];
int n,m;
float p[1000][1000]={0};
struct edge
{
	int i, j;
	float w;
};
edge e[1000];
int main()
{
	int i,k;
	char buf1[30], buf2[30];
	float tmp;
	int x,y;
	scanf("%f",&a);
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%s",s[i]);	
	}

	scanf("%d",&m);
	for(i=0;i<m;i++)
	{
		scanf("%s %s %f",buf1, buf2, &tmp);
		x=0;
		y=0;
		for(k=0;k<n;k++)
		{	
			if(x&&y)
				break;
			if(strcmp(s[k], buf1) == 0)
				x=k;
			else if(strcmp(s[k], buf2) == 0)
				y=k;
		}
		p[x][y]=tmp;
		p[y][x]=tmp;
	}

	for(i=0;i<n-1;i++)
	{
		e[i].i=0;
		e[i].j=i+1;
		e[i].w=p[0][i+1];
	}
	float min,max=32767;
	int m,v;
	edge t1;
	for(i=0;i<n-1;i++)
	{
		min=max;
		m=-1;
		for(k=i;k<n-1;k++)
		{
			if(e[k].w>0 && e[k].w < min)
			{
				min=e[k].w;
				m=k;
			}
		}
		if(m==-1)
		{
			break;
			min=a+100;
		}
		t1=e[m];e[m]=e[i];e[i]=t1;
		v=e[i].j;
		for(k=i+1;k<n-1;k++)
		{
			min=p[v][e[k].j];
			if(min>0 && min < e[k].w)
			{
				e[k].w=min;
				e[k].i=v;
			}
		}
	}
	min=0;
	for(i=0;i<n-1;i++)
	{
		min+=e[i].w;
	}
	if(min<=a)
	{
		printf("Need %.1f miles of cable\n",min);
	}
	else{
		printf("Not enough cable\n");
	}
	
	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