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 flywithoutwing at 2008-05-18 20:40:38 on Problem 1062
思路:先找出酋长即物品(1)到每个物品的最小花费,然后有每个物品返回到物品1,若这条路径上的最高等级与最小等级的查大于M则舍弃这条路径,找出符合条件的最小值?
代码如下:

#include<iostream>

using namespace std;

struct Goods
{
	int price,rank,num;
	Goods(int p = 0,int r = 0,int  n = 0):price(p),rank(r),num(n){}

} goods[200];


int ABS(int num)
{
	return num > 0?num:-num;
}
int map[200][200];
int father[200];
int dis[200];
bool visited[200];


int main()
{
	int M,N,i,j,replace,price,k;
	cin >> M >> N;
	fill_n(visited,N + 1,false);
	fill_n(father,N + 1,0);
	for(i = 0; i <= N;i++)
		fill_n(map[i],N + 1,100000000);
	for(i = 1; i <= N;i++)
	{
		cin >> goods[i].price >> goods[i].rank >> goods[i].num;
		for(j = 0; j < goods[i].num;j++)
		{
            cin >> replace >> price;
			map[i][replace] = price;
		}
	}
	for(i = 1; i <= N;i++)
		dis[i] = map[1][i];

	dis[1] = 0;
	visited[1] = true,father[1] = -1;
	for(i = 1; i < N;i++)
	{
		int mindis = 10000000,v = 0;
		for(j = 1; j <= N;j++)
			if(!visited[j]&&mindis > dis[j])
			{
				mindis = dis[j];
				v = j;
			}
		visited[v] = true;
		for(k = 1; k <= N;k++)
			if(!visited[k]&&dis[k] > dis[v] + map[v][k])
			{
				father[k] =v;
				dis[k] = dis[v] + map[v][k];
			}
	}
	int res = goods[1].price;
	for(k = 2;k <= N;k++)
	{
		int edge = 1,maxrank = goods[k].rank,minrank = goods[k].rank;
		int u = k;
		while(father[u] != -1&&ABS(maxrank - minrank) <= M)
		{
			if(maxrank < goods[u].rank)
				maxrank = goods[u].rank;
			if(minrank > goods[u].rank)
				minrank = goods[u].rank;		
			edge++;
			if(father[u] == 0)
				break;
			u = father[u];
		}
		if(/*edge >= goods[1].num&&*/ABS(maxrank - minrank) <= M)
			if(res > dis[k])
				res = dis[k] + goods[k].price;
	}
	cout << res << endl;
    system("pause");
	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