PAT甲级 1003.Emergency(25) 题目翻译与答案

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/qq278672818/article/details/55669457

题目来源自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痕迹,所以代码显得很长。

这这个题目用到的方法我是在http://www.liuchuo.net/archives/2357

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



展开阅读全文

没有更多推荐了,返回首页