PAT 1003 Emergency (25)

1003 Emergency (25)(25 分)

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 Specification:

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, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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 1:

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 1:

2 4

Sample Input 2:

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

Sample Output 2:

3 8

思路:

用dijkstra求c1到c2的最短路径,同时还要统计最短路径的条数和最短路径中救援队和的最大值。需要注意两个两点,一是起点和终点是同一个城市,二是上面多提供的第二组样例的情况。 原以为这题较简单,但是折腾了我好久,只能说细节很重要啊_(:з」∠)_

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include   
#include 
using namespace std;

struct node
{
	int vis;   //是否被访问过
	int dis;   //到该点的最短距离
	int cnt;   //到该点的最短路径条数
	int maxt;  //到该点的最短路径的最大队数
};

vector city;
int w[500][500], team[500], n, m, c1, c2;

void dijkstra(int a, int b)
{
	if (a == b)  //起点和终点是同一个城市直接结束
		return;
	for (int i = 0; i < n; i++)
	{
		city[i].vis = 0;
		city[i].dis = w[a][i];
		if (i != a)
		{
			city[i].cnt = 0;
			city[i].maxt = 0;
		}
	}
	for (int i = 0; i < n; i++)
	{
		int min = INT_MAX, k = 0;
		for (int j = 0; j < n; j++)  //每次找出未访问过的城市中距离最短的那一个
		{
			if (!city[j].vis && city[j].dis < min)
			{
				min = city[j].dis;
				k = j;
			}
		}
		city[k].vis = 1;
		if (k == b)  //当终点被访问过即可结束
			return;
		for (int j = 0; j < n; j++)  //检查从c1经过k再到j的距离是否比从c1直接到j的距离短
		{
			if (!city[j].vis && city[k].dis + w[k][j] < city[j].dis) 
			{
				city[j].dis = city[k].dis + w[k][j]; 
				city[j].maxt = city[k].maxt + team[j];
				city[j].cnt = city[k].cnt;
			}
			else if (!city[j].vis && city[k].dis + w[k][j] == city[j].dis)
			{
				if (city[k].maxt + team[j] > city[j].maxt)
					city[j].maxt = city[k].maxt + team[j];
				city[j].cnt += city[k].cnt;
			}
		}
	}
}

int main()
{
	memset(w, 0x3f3f3f3f, sizeof(w));
	cin >> n >> m >> c1 >> c2;
	city.resize(n);
	for (int i = 0; i < n; i++)
	{
		cin >> team[i];
		w[i][i] = 0;
	}
	for (int i = 0; i < m; i++)
	{
		int a, b, v;
		cin >> a >> b >> v;
		w[a][b] = v;
		w[b][a] = v;
	}
	city[c1].cnt = 1;
	city[c1].maxt = team[c1];
	dijkstra(c1, c2);
	cout << city[c2].cnt << " " << city[c2].maxt << endl;
	return 0;
}

 

你可能感兴趣的:(PAT,图,最短路径,vector)