PAT甲级1003 Emergency//Dijkstra算法运用

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​1and 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 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.
翻译:每个输入包括一个测是用例。对每个测试用例,第一行包括4个正整数:N(<= 500) - 代表城市的数量(城市序号由0开始),M是道路的数量,C1和C2分别是你身处的城市和你要去拯救的城市。接下来一行有N个整数,其中第i个整数是第i个城市中的救援队数量。接下来由M行,每行用3个整数来描述道路,由一条路连接起来的两座城市以及这条路的长度。题目确保至少存在一条从C1到C2的路径

Output Specification:

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.
翻译:对每个测试样例,在一行中输出两个数字:C1到C2这之间最短路径的数量,以及你能聚集到的救援队的最大数量。在一行中的所有数字之间必须间隔一个空格,并且一行中的末尾不能有多余的空格

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

思路

本题可以将问题简化成求最短路径条数以及最短路径上的最大点权之和,可以看中Djikstra算法拓展部分。

#include <iostream>
#include <cstdlib>
#include <vector>
#include <map>
#include <climits>
#include <string.h>
using namespace std;
const int INF = 1000000000;
const int MAXV = 510;
map<int, bool> mp;
int n, m, c1, c2;
int G[MAXV][MAXV];
int w[MAXV], d[MAXV], num[MAXV], weight[MAXV];
void Dijkstra(int);
int main()
{
	cin >> n >> m >> c1 >> c2;
	fill(G[0], G[0] + MAXV * MAXV, INF); //初始化
	for (int i = 0; i < n; i++)
		cin >> weight[i]; //输入点权
	for (int i = 0; i < m; i++)
	{
		int r, c, v;
		cin >> r >> c >> v;
		//注意是无向图,所以邻接矩阵对称
		G[r][c] = v;
		G[c][r] = v;
	}
	Dijkstra(c1);
	cout << num[c2] << ' ' << w[c2] << endl;
	return 0;
}
void Dijkstra(int s)
{
	fill(d, d + MAXV, INF);
	memset(num, 0, sizeof(num));
	memset(w, 0, sizeof(w));
	d[s] = 0;
	w[s] = weight[s];
	num[s] = 1;
	for (int i = 0; i < n; i++)
	{
		int u = -1, MIN = INF;
		for (int j = 0; j < n; j++)
		{
			if (mp[j] == false && d[j] < MIN)
			{
				u = j;
				MIN = d[j];
			}
		}
		if (u == -1)
			return;
		mp[u] = true;
		for (int j = 0; j < n; j++)
		{
			if (mp[j] == false && G[u][j] != INF)
			{
				if (d[u] + G[u][j] < d[j])
				{
					d[j] = d[u] + G[u][j];
					w[j] = w[u] + weight[j];
					num[j] = num[u];
				}else if (d[u] + G[u][j] == d[j])
				{
					if (w[u] + weight[j] > w[j])
						w[j] = w[u] + weight[j];
					num[j] += num[u];
				}
			}
		}
	}
}

法二

在学习了Dijkstra + DFS的方法后,这题再用该方法写了一遍,这个方法更加通用,能解决更多问题

#include <iostream>
#include <vector>
#include <map>
#include <cstdlib>
#include <climits>
using namespace std;
vector<int> temp_path, path;
vector<int> pre[501];
map<int, bool> visit;
int G[501][501], d[501], weight[501];
int n, m, sta, des, num = 0, maxn = 0;
void Dijkstra(int);
void DFS(int);
int main()
{
	fill(G[0], G[0] + 501 * 501, INT_MAX);
	fill(weight, weight + 501, INT_MAX);
	cin >> n >> m >> sta >> des;
	for (int i = 0; i < n; i++)
		cin >> weight[i];
	for (int i = 0; i < m; i++)
	{
		int c1, c2, length;
		cin >> c1 >> c2 >> length;
		G[c1][c2] = G[c2][c1] = length;
	}
	Dijkstra(sta);
	DFS(des);
	cout << num << ' ' << maxn;
	system("pause");
	return 0;
}
void Dijkstra(int s)
{
	fill(d, d + 501, INT_MAX);
	d[s] = 0;
	for (int i = 0; i < n; i++)
	{
		int u = -1, MIN = INT_MAX;
		for (int j = 0; j < n; j++)
		{
			if (visit[j] == false && d[j] < MIN)
			{
				u = j;
				MIN = d[j];
			}
		}
		if (u == -1)
			return;
		visit[u] = true;
		for (int v = 0; v < n; v++)
		{
			if (visit[v] == false && G[u][v] != INT_MAX)
			{
				if (d[u] + G[u][v] < d[v])
				{
					d[v] = d[u] + G[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}else if (d[u] + G[u][v] == d[v])
					pre[v].push_back(u);
			}
		}
	}
}
void DFS(int t)
{
	if (t == sta)
	{
		num++;
		temp_path.push_back(t);
		int maxp = 0;
		for (int i = temp_path.size() - 1; i >= 0; i--)
			maxp += weight[temp_path[i]];
		if (maxp > maxn)
		{
			maxn = maxp;
			path = temp_path;
		}
		temp_path.pop_back();
		return;
	}
	temp_path.push_back(t);
	for (int i = 0; i < pre[t].size(); i++)
		DFS(pre[t][i]);
	temp_path.pop_back();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值