编程题目:PAT(Advanced Level) Practice 1003. Emergency (25)

1003. Emergency (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked 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 the mean time, call up as many hands on the way as possible.

Input

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

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 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 is no extra space allowed at the end of a line.

Sample Input
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
Sample Output
2 4

       本题的一个基本的方法是基于最短路的算法(个人采用的dijkstra)加以改进,注意做好多条相等的最短路径的处理。另外明确题目要求的是最短路径的数目,以及沿着某一条路径可能获得的rescue team的最大数,这一点上我起初理解错误。参考代码如下,写的有些冗余,但是好歹AC过了。
/*
http://pat.zju.edu.cn/contests/pat-a-practise/1003 1003. Emergency (25),找到最短路径,计算其条数,并得到沿途人手最多的一条路径
*/
#include
#include
#include
#include
using namespace std;

#define Inf 10000000

int N ,M ,C1,C2; 
int RT[501]={0}; //rescue team
int LENGTH[501][501]; //length of two cities
int visited[501] = {0}; //是否访问过的标记
int dist[501]; //存最短距离
int pathNum = 0; //最短路径的数目
int maxRTs = 0; //最短路径中rescue team 的总数
int tempRTs= 0; //暂存一条路径的人手总数

struct prenode  //定义结构体,用于存指定点到源点路径的前一个点,若有多条路径,则存在vector next中
{
	int number;
	vector next;
};

prenode pre[501];

void improved_dijkstra()//获得最短路径
{
	int i;
	for(i = 0; idist[num]+LENGTH[num][k])
					{
						dist[k] = dist[num]+LENGTH[num][k];
						pre[k].number = num;
						pre[k].next.clear();
					}	
					else if(dist[k]==dist[num]+ LENGTH[num][k])//多个相等的距离
					{
						pre[k].next.push_back(num);
					}
				}
			}
		}
	}
}
void getpathNum(int nodenow,int nodetarget) //得到path数目
{
	if(nodenow == nodetarget)
	{
		pathNum++;
		//tempRTs += RT[nodenow];
		if(tempRTs+RT[nodenow]>maxRTs)
			maxRTs = tempRTs+RT[nodenow];	
		return;
	}
	else
	{
		int temp = RT[nodenow];
		tempRTs += RT[nodenow];
		getpathNum(pre[nodenow].number,nodetarget);
		int i =0;
		while(i> N >>M >>C1>>C2;
	int i;
	for( i = 0;i>RT[i];
	int a, b, l;
	for(int i = 0;i> a >> b>> l;
		LENGTH[a][b] = l;
		LENGTH[b][a] = l;
	}
	improved_dijkstra();
	getpathNum(C2,C1);

	cout<

另外看到一篇写得比较简洁的代码,供参考: http://www.cnblogs.com/echobfy/p/3545917.html

你可能感兴趣的:(编程题目)