hdu1690 Bus System floyed最短路

hdu1690 Bus System

 

 

floyd中3个循环的顺序弄反了 wa了一天 = =

 

正确顺序应该是先 中间节点 k ,然后才是 i , j。

 

 

 

#include<iostream>
using namespace std;

int INF = -1; 
__int64 mp[110][110],location[110],N,M,L1,L2,L3,L4,C1,C2,C3,C4;

int Price(__int64 x)
{
	if(x<=L1) return C1;
	if(x<=L2) return C2;
	if(x<=L3) return C3;
	if(x<=L4) return C4;
	else 	  return INF;
}

int main()
{
	int Test,T,n,i,j;
	scanf("%d",&Test);
	
	for(T=1;T<=Test;T++)
	{
		scanf("%I64d%I64d%I64d%I64d",&L1,&L2,&L3,&L4);      //距离区间
		scanf("%I64d%I64d%I64d%I64d",&C1,&C2,&C3,&C4);      //对应的价格
		scanf("%I64d%I64d",&N,&M);                		    //N个站 M次查询
		
		
		for(i=1;i<=N;i++)
			for(j=1;j<=N;j++)
				mp[i][j]=INF;
		
		
		for(i=1;i<=N;i++)
			scanf("%I64d",&location[i]);
		
		
		__int64 dist;
		for(i=1;i<=N;i++)
		{
			for(j=1;j<=N;j++)
			{
				if(i==j)
				{
					mp[i][j]=0;
					continue;
				}
				dist=location[i]-location[j];
				if(dist<0) dist*=-1;
				mp[i][j]=Price(dist);
			}
		}
		
		
		int k;
		for(k=1;k<=N;k++)
			for(i=1;i<=N;i++)
				for(j=1;j<=N;j++)
					if(mp[i][k]!=-1&&mp[k][j]!=-1)
						if(mp[i][k]+mp[k][j] < mp[i][j] || mp[i][j] == -1)
							mp[i][j]=mp[i][k]+mp[k][j];
						
		/*
		for(i=1;i<=N;i++)
		{
			for(j=1;j<=N;j++)
				printf("\t%I64d",mp[i][j]);
			printf("\n");
		}*/
		
		
		int s,t;
		printf("Case %d:\n",T);
		for(i=1;i<=M;i++)
		{
			scanf("%d%d",&s,&t);
			if(mp[s][t]==INF)
				printf("Station %d and station %d are not attainable.\n",s,t);
			else
				printf("The minimum cost between station %d and station %d is %I64d.\n",s,t,mp[s][t]);
		}
	}
	return 0;
}


/*
1
1 3 5 7 1 11 5 30
6 1
5 20 15 50 10 5




*/


 

你可能感兴趣的:(c,System)