1. 程式人生 > >【笨方法學PAT】1003 Emergency(25 分)

【笨方法學PAT】1003 Emergency(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:

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

二、題目大意

從出發地到目的地,要求路程最短,在路程最短的情況下,權重最小。

三、考點

DFS、圖

四、解題思路

1、使用二維陣列儲存圖,一維陣列儲存權重,一維陣列標記點是否訪問過;將起點、終點、輸出結果作為全域性變數。

2、DFS,最終要輸出最短路徑的個數,最短路徑中最大救援組個數(權重),所以 dfs(int index,int path_length,int team_num),維護當前點、當前路徑長度、當前救援小組個數,三個變數。

3、路徑更短的時候,對最短路徑、最短路徑個數、救援小組個數進行更新;路徑一樣,救援小組更多,對最短路徑個數、救援小組個數進行更新。

3、DFS遍歷中,要同時對是否訪問過點進行標記,使用 bool visit[] 一維陣列。

五、程式碼

#include<iostream>
#include<string>
#define N 510
#define INF 99999999
using namespace std;

int e[N][N],  weight[N];
bool visit[N];
int ans_path_num=0, ans_team_num=0,ans_min_length = INF;
int n, m, m_start, m_end;

void dfs(int index,int path_length,int team_num) {
	//剪枝
	if (path_length > ans_min_length)
		return;

	//終止條件
	if (index == m_end) {
		if (ans_min_length>path_length) {
			ans_min_length = path_length;
			ans_path_num = 1;
			ans_team_num = team_num;
		}
		else if (ans_min_length == path_length) {
			ans_path_num++;
			if (ans_team_num < team_num)
				ans_team_num = team_num;
		}
		return;
	}

	//下一層
	for (int i = 0; i < n; ++i) {
		if (visit[i] == false && e[index][i] != INF) {
			visit[i] = true;
			dfs(i, path_length + e[index][i], team_num + weight[i]);
			visit[i] = false;
		}
	}
}

int main() {
	//初始化路
	fill(e[0], e[0] + N*N, INF);
	fill(visit, visit + N, false);

	//讀取資料
	cin >> n >> m >> m_start >> m_end;
	for (int i = 0; i < n; ++i) 
		cin >> weight[i];
	for (int i = 0; i < m; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		e[a][b] = e[b][a] = c;
	}

	//DFS
	visit[m_start] = true;
	dfs(m_start, 0, weight[m_start]);

	//輸出
	cout << ans_path_num << " " << ans_team_num;

	system("pause");
	return 0;
}