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 tonylane at 2007-09-04 16:13:54 on Problem 1062
想了很久,还是WA,大家能帮忙看一下吗?
#include <stdio.h>
//单源最短路径,dijkstra算法,邻接阵形式,复杂度O(n^2)
//求出源s到所有点的最短路经,传入图的顶点数n,(有向)邻接矩阵mat
//返回到各点最短距离min[]
//可更改路权类型,但必须非负!
#define MAXN 100
#define inf 10001

struct thing
{
	int p, l, x;
}a[100];

void dijkstra(int n,int mat[][MAXN],int s,int* min){
	int v[MAXN],i,j,k;
	for (i=0;i<n;i++)
		min[i]=inf,v[i]=0;
	for (min[s]=0,j=0;j<n;j++){
		for (k=-1,i=0;i<n;i++)
			if (!v[i]&&(k==-1||min[i]<min[k]))
				k=i;
		for (v[k]=1,i=0;i<n;i++)
			if (!v[i]&&min[k]+mat[k][i]<min[i])
			{
				min[i]=min[k]+mat[k][i];
			}
	}
	//在已求最短距离上加上节点的价值
	for(i=0; i<n; i++)
		min[i] += a[i].p;
}

int main(void)
{
	int M=0, N=0, i=0, j=0;
	int t[100], v[100];
	int price[100][100];
	scanf("%d%d", &M, &N);
	int *min = new int[N];

	for(i=0; i<N; i++)
		for(j=0; j<N; j++)
			price[i][j] = inf;

	for(i=0; i<N; i++)
	{
		scanf("%d%d%d", &a[i].p, &a[i].l, &a[i].x);
		for(j=0; j<a[i].x; j++)
		{
			scanf("%d%d", &t[j], &v[j]);
			price[i][t[j]-1] = v[j];
		}
	}

	//求最大等级的位置
	int MaxLev=0, index=0;
	for(i=0; i<N; i++)
	{
		if(a[i].l > MaxLev)
		{
			MaxLev = a[i].l;
			index = i;
		}
	}

	//将超过M的去掉,用inf表示去掉
	for(j=0; j<N; j++)
	{
		if((a[index].l-a[j].l) > M)
		{
			for(int k=0; k<N; k++)
			{
		    	price[k][j] = inf;
		    	price[j][k] = inf;
			}
		}
	}

	dijkstra(N, price, 0, min);

	int Min = 10001;
	for(i=0; i<N; i++)
		if(min[i] < Min)
			Min = min[i];

	printf("%d", Min);
	delete []min;
	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