1. 程式人生 > >PAT甲級 1003.Emergency(25) 題目翻譯與答案

PAT甲級 1003.Emergency(25) 題目翻譯與答案

題目來源自PAT網站  https://www.patest.cn/

題目描述:

1003. Emergency (25)

As an emergencyrescue team leader of a city, you are given a special map of your country. Themap shows several scattered cities connected by some roads. Amount of rescueteams in each city and the length of each road between any pair of cities aremarked on the map. When there is an emergency call to you from some other city,your job is to lead your men to the place as quickly as possible, and at themean time, call up as many hands on the way as possible.

Input

Each input filecontains one test case. For each test case, the first line contains 4 positiveintegers: N (<= 500) - the number of cities (and the cities are numberedfrom 0 to N-1), M - the number of roads, C1 and C2 - the cities that you arecurrently in and that you must save, respectively. The next line contains Nintegers, where the i-th integer is the number of rescue teams in the i-thcity. Then M lines follow, each describes a road with three integers c1, c2 andL, which are the pair of cities connected by a road and the length of thatroad, respectively. It is guaranteed that there exists at least one path fromC1 to C2.

Output

For each test case,print in one line two numbers: the number of different shortest paths betweenC1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there isno extra space allowed at the end of a line.

SampleInput

5 6 0 2

1 2 1 5 3

0 1 1

0 2 2

0 3 1

1 2 1

2 4 1

3 4 1

SampleOutput

2 4

時間限制

400 ms

記憶體限制

65536 kB

程式碼長度限制

16000 B

判題程式

Standard

作者

CHEN, Yue

題目翻譯:

1003.突發事件(25)

作為一個城市緊急援救隊的指揮者,你得到了一個國家的特殊地圖。地圖上分散著幾座城市,城市間用道路連線著。每個城市援救隊的數量以及兩座城市之間每條道路的長度已經在地圖上標出。當某些城市發生了突發事件,需要你的幫助時,你的工作是帶領你的隊伍儘快的趕到事發現場,與此同時,召集儘可能多的在路上的隊伍。

輸入

每個輸入檔案包含一個測試例項。每個例項的第一行有四個正整數:N(<= 500)是城市的個數(城市的編號從0到N-1),M是道路的個數,C1和C2分別是你現在所在的城市以及你必須去救援的城市。下一行有N個整數,第i個整數是第i個城市中救援隊的數量。然後下面有M行,每行表示一條道路。每一行有三個整數c1,c2和L,分別表示道路連線的兩個城市以及道路的長度。保證C1到C2之間存在至少一條路徑。

輸出

對於每個測試例項,在一行中輸出兩個數字:C1和C2之間不同的最短路徑的個數,你能聚集起來的最多的救援隊數量。

一行中的所有數字必須被一個空格分隔開,在每行的結尾不允許出現空格。

樣例輸入

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

樣例輸出

2 4

答案程式碼:

#include<cstdio>
#define MAXN 100000
//#define QWERTY 
int main()
{
	int N,M,C1,C2;
	scanf("%d%d%d%d",&N,&M,&C1,&C2);
	int SaveMen[N];
	int arcs[N][N];
	int Num[N];
	int Team[N];
	int i,i1,i2,d;
	for(i1=0;i1<N;++i1)
	{
		for(i2=0;i2<N;i2++)
		{
			arcs[i1][i2]=MAXN;
			if(i1==i2)
				arcs[i1][i2]=0;
		}
	}
	for(i=0;i<N;++i)
		scanf("%d",&SaveMen[i]);
	for(i=0;i<M;++i)
	{
		scanf("%d%d%d",&i1,&i2,&d);
		arcs[i1][i2]=d;
		arcs[i2][i1]=d;
	}
	
	bool S[N];
	int dist[N],path[N];
	for(i=0;i<N;++i)
	{
		S[i]=false;
		dist[i]=arcs[C1][i];
		//path[i]=C1;
		if(i==C1)
		{
			Num[C1]=1;
			Team[C1]=SaveMen[C1];
		}else
		{
			Num[i]=0;
			Team[i]=0;
		}
	}
	
	int c=-1;
	while(c!=C2)
	{
		d=MAXN;
		for(i=0;i<N;++i)
		{
			if(S[i]==true)
				continue;
			if(dist[i]<d)
			{
				i1=i;
				d=dist[i];
			}
		}
		S[i1]=true;
		c=i1;
#ifdef QWERTY
		printf("@@@@@@@ begin \n");
		for(i=0;i<N;++i)
		{	
			printf("%d ",dist[i]);
		}
		putchar('\n');
		for(i=0;i<N;++i)
		{	
			printf("%d ",Team[i]);
		}
		putchar('\n');
		printf("@@@@@@@ end \n");
#endif
		for(i=0;i<N;++i)
		{
			if(S[i]==true)
				continue;
			if(dist[i]>dist[c]+arcs[c][i])
			{
				dist[i]=dist[c]+arcs[c][i];
				//path[i]=c;
				Team[i]=SaveMen[i]+Team[c];
				Num[i]=Num[c];
			}else if(dist[i]==dist[c]+arcs[c][i])
			{
				Num[i]=Num[i]+Num[c];
				if(Team[i]<Team[c]+SaveMen[i])
					Team[i]=Team[c]+SaveMen[i];
			}
		}
	}
	int sum=0;
	//while(c!=C1)
	//{
		//printf("%d ",c);
	//	sum+=SaveMen[c];
	//	c=path[c];
	//}
	//sum+=SaveMen[C1];
	printf("%d %d",Num[C2],Team[C2]);
	return 0;
}


說明與心得:

我留下了程式碼的debug痕跡,所以程式碼顯得很長。

上看的,文章中包含了Dijkstra演算法的一些變形。