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

WA,请大家帮忙看一下。

Posted by robertbean at 2012-02-21 14:32:08 on Problem 2263
#include <iostream>
using namespace std;

int best;
int n,r;
int main(void)
{
	
	char cities[200][31];	 //城市名字
	int cityNum = 0;		 //已输入城市的个数
	int map[200][199];       //记载城间路上的最大载重限制

	char cityA[31];
	char cityB[31];
	int dis=0;

	int GetID(const char*, char [200][31], int&);     

	int count = 1;
	while (1)
	{
		cin >> n >> r;
		if (n == 0 && r == 0)
			break;
		if (count-1)
			cout << endl << endl;
		memset(cities, ' ', sizeof(cities));
		memset(map, -1, sizeof(map));
		cityNum = 0;
	
		for (int i=0; i<r; i++)
		{
			cin >> cityA >> cityB >> dis;
			int idA = GetID(cityA, cities, cityNum);          //将城市名字符串对应成数字
			int idB = GetID(cityB, cities, cityNum);
			map[idA][idB] = dis;  //记录 权值。
			map[idB][idA] = dis;
		}

		cin >> cityA >> cityB;
		int idA = GetID(cityA, cities, cityNum);
		int idB = GetID(cityB, cities, cityNum);
		if (idA == idB)
		{
			best = 10000;
		}
		else
		{
			best = 0;
			void Heavy(int, int, int[][199], int);
			Heavy(idA, idB, map, 20000);   //计算最大载重量
		}

		cout<<"Scenario #"<<count++<<endl<<best<<" tons";
	}

	return 0;
}

//dfs.
void Heavy(int idA, int idB, int map[][199], int weight)
{
	if (map[idA][idB] > 0)
	{
		best = (map[idA][idB] < weight ? map[idA][idB] : weight);
		return;
	}
	for (int i=0; i<n; i++)
	{
		if (map[idA][i] < 0)
			continue;
		int tWei = weight;
		if (tWei > map[idA][i])
			tWei = map[idA][i];
		int tt = map[idA][i];
		map[idA][i] = map[i][idA] = -1;
		Heavy(i, idB, map, tWei);
		map[idA][i] = map[i][idA] = tt;
	}
	return;
}

//将cName对应成相应数字。 即: 若该名字未在cities容器中,则已输入城市数量cityNum为该城市id,cityNum++
int GetID(const char *cName, char cities[200][31], int &cityNum)
{
	for (int i=0; i < cityNum; i++)
	{
		for (int j=0; j<31; j++)
		{
			if (cName[j] != cities[i][j])
			{
				break;
			}
			if (cName[j] == '\0')
			{
				return i;
			}
		}
	}
	cityNum;
	for (int j=0; j<31; j++)
	{
		cities[cityNum][j] = cName[j];
		if (cName[j] == '\0')
			break;
	}
	return cityNum++;
}

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