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

这个输入竟然让我困惑了很久。。。。。瀑布汗。。

Posted by bingshen at 2010-08-22 14:53:43 on Problem 1251
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#define inf 99999999

using namespace std;

struct node
{
	int v1;
	int v2;
	int len;
};

int n;
node e[100];
int map[100][100];
int dis[100][100];

void init()
{
	int i,j;
	for(i=0;i<100;i++)
		for(j=0;j<100;j++)
		{
			dis[i][j]=inf;
		}
}

int prim()
{
	int i,j,leng,minl,min,vx,vy;
	int ret=0;
	for(i=1;i<=n-1;i++)
	{
		e[i].v1=1;
		e[i].v2=i+1;
		e[i].len=dis[1][i+1];
	}
	for(i=1;i<=n-1;i++)
	{
		min=-1;
		minl=inf;
		for(j=i;j<=n-1;j++)
		{
			if(e[j].len<minl)
			{
				minl=e[j].len;
				min=j;
			}
		}
		if(min==-1)
			break;
		ret=ret+minl;
		swap(e[min],e[i]);
		vx=e[i].v2;
		for(j=i+1;j<=n-1;j++)
		{
			vy=e[j].v2;
			leng=dis[vx][vy];
			if(leng<e[j].len)
			{
				e[j].len=leng;
				e[j].v1=vx;
			}
		}
	}
	return ret;
}

int main()
{
	int i,t,ans,j;
	char start,end,ch1;
	while(cin>>n&&n!=0)
	{
		init();
		for(i=0;i<n-1;i++)
		{
			cin>>ch1>>t;
			start=ch1-'A'+1;
			for(j=0;j<t;j++)
			{
				cin>>ch1;
				end=ch1-'A'+1;
///				cout<<(int)start<<" "<<(int)end<<endl;;
				cin>>dis[start][end];
				dis[end][start]=dis[start][end];
			}
		}
		ans=prim();
		printf("%d\n",ans);
	}
	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