PAT甲级1003(C语言)

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

 

来自 <https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376>

解题思路:1.dfs,2.dijkstra

 

1.dfs:

dfs是用来遍历图的算法,算法的核心思想是栈(递归实现和非递归实现),本文采用递归实现(代码更加简洁)。

图的存储有两种形式:邻接矩阵和邻接链表。(本文采用邻接矩阵)

 

由题目可知,要求出共有多少条最短路径以及哪条路径上的总救援人数最多。

递归返回条件为初始节点(中间节点)==目标节点和剪枝(当前路径总权值大于已有最短路径总权值)。

dfs需要一个遍历数组用来记录已经遍历的节点。

代码如下:

#include<stdio.h>
#include<string.h>
#define inf 1e8
#define max 500 
int t = inf;
int  den = 0, maxN = 0;//den:不同路径数, maxN:最大救援数
int mat[max][max];//节点之间的权值 
int city[max];//各节点救援队数 
int v[max];//记录节点是否已经被访问 
int n, m, c1, c2;
void dfs(int c1, int c2, int dist, int num);//初始节点(中间节点),目标节点,权值,救援队数量 
int main() {
	scanf("%d %d %d %d", &n, &m, &c1, &c2);
	memset(v, 0, n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &city[i]);
	}
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			mat[i][j] = inf;
		}
	}
	for (int i = 0; i < m; ++i) {
		int start, end, len;
		scanf("%d %d %d", &start, &end, &len);
		mat[start][end] = mat[end][start] = len;
	}
	dfs(c1, c2, 0, city[c1]);
	printf("%d %d", den, maxN);
	return 0;
} 
void dfs(int c1, int c2, int dist, int num) {
	
	if (c1 == c2) {
		if (dist < t){
			den = 1;
			t = dist;
			maxN = num;
		}
		else if (dist == t) {
			den++;
			if (maxN < num) maxN = num;
		}
		return ;
	}
	if (dist > t) return ;//剪枝 
	
	
	for(int i = 0; i < n; ++i) {
		if (!v[i] && mat[c1][i] != inf) {
			v[i] = 1;
			dfs(i, c2, dist+mat[c1][i], num+city[i]);
			v[i] = 0;
		}
	}
}

2.dijkstra

dijkstra算法是通过比较直接到达与间接到达时谁最短来求得最短路径。dijkstra一般需要一个初始路径数组用来存放初始节点-图中所有节点的权值。(权值不能为负)

 

终止条件:初始节点(中间节点) != 目标节点

代码如下:

#include<stdio.h>
#include<string.h>
#define inf 1e8
#define max 500
/*city[]:救援人数,mat[][]:各节点之间的权值,num[]:dijkstra[]:遍历过程中产生的中间值(最短路径条数),
team[]:可集结的最多救援对,dis[]:用于存放初始节点到图中所有节点的权值,v[]:记录节点状态是否已经被访问(0:未访问,1:已访问)*/ 
int city[max], mat[max][max], num[max], team[max], dis[max], v[max];  
int n, m, c1, c2;
int main() {
	void dijstra();
	scanf("%d %d %d %d", &n, &m, &c1, &c2);
	memset(v, 0, n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &city[i]);
	}
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			mat[i][j] = inf;
		}
	}
	for (int i = 0; i < m; ++i) {
		int start, end, len;
		scanf("%d %d %d", &start, &end, &len);
		mat[start][end] = mat[end][start] = len;
	}
	dijstra();
	printf("%d %d", num[c2], team[c2]);
	return 0;
} 

 void dijstra()
 {
 	int  ct;//中间节点 
 	for (int i = 0; i < n; ++i) {
	 	dis[i] = mat[c1][i];
	}
	
	
	
	memset(num, 0, n);
	memset(team, 0, n);
	
	num[c1] = 1;
	dis[c1] = 0;
	team[c1] = city[c1];
	
	ct = -1;
	
	while (ct != c2) {
		int d = inf;
		for (int i = 0; i < n; ++i) {
			if ( !v[i] && dis[i] < d) {
				d = dis[i];
				ct = i;
				}
		}
		v[ct] = 1;
		
		for (int i = 0; i < n; ++i) {
			if (!v[i]){
				if (dis[i] > d + mat[ct][i]) {
					dis[i] = d + mat[ct][i];
					team[i] = city[i] + team[ct];
					num[i] = num[ct];	
				}
				else if (dis[i] == d + mat[ct][i]) {
					num[i] = num[i] + num[ct];
					if (team[i] < team[ct] + city[i]) {
						team[i] = team[ct] + city[i];
					}
				}
			}
		}
	}
	 
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值