PAT甲级1003 Emergency Dijkstra算法(堆优化版/朴素版)

前言

  最近花了很多的时间在写JAVA项目上面,疏忽了算法和数据结构的学习。最近突然醒悟基础更为重要,打算从今天开始每天抽出一些时间做下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, 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 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.

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

题目大意:

给出n个城市,这些城市中共有m条路径,这些路径的长度已知,每个城市都有一些人手,经过城市的时候会带走这些人手,给出c1起点城市编号,c2终点城市编号,求从c1到c2最短路径的条数以及最短路径情况下能召集到最多的人手。

AC代码

堆优化版Dijkstra

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 510 * 510;

int h[N], e[N], w[N], ne[N], idx;

int sum[N], per[N], p[N], d[N];
bool st[N];
using PII = pair<int, int>;
priority_queue<PII, vector<PII>, greater<PII>> heap;
void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

int main()
{
	int n, m, c1, c2;
	cin >> n >> m >> c1 >> c2;

	for (int i = 0; i < n; ++i) cin >> p[i];

	memset(d, 0x3f, sizeof(d));
    memset(h, -1, sizeof(h));
    
	for (int i = 0; i < m; ++i)
	{
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
        add(b, a, c);
	}

	d[c1] = 0;
    sum[c1] = 1;
    per[c1] = p[c1];
    heap.push({0, c1});
    
	while(heap.size())
	{
		auto f = heap.top();
        heap.pop();
        
        int dist = f.first, ver = f.second;
        if(st[ver]) continue;
        st[ver] = true;

		for(int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            
            if(d[j] > d[ver] + w[i])
            {
                d[j] = d[ver] + w[i];
                heap.push({d[j], j});
                sum[j] = sum[ver];
                per[j] = per[ver] + p[j];
            }
            else if (d[j] == d[ver] + w[i])
			{
				sum[j] += sum[ver];
				per[j] = max(per[j], per[ver] + p[j]);
			}
        }
	}

	cout << sum[c2] << " " << per[c2] << endl;
	return 0;
}

朴素版Dijkstra

#include<iostream>
#include<cstring>
using namespace std;
const int N = 510, INF = 0x3f3f3f3f;

int g[N][N], d[N];
bool st[N];

int sum[N], per[N], p[N];

int main()
{
	int n, m, c1, c2;
	cin >> n >> m >> c1 >> c2;

	for (int i = 0; i < n; ++i) cin >> p[i];

	memset(d, 0x3f, sizeof(d));
	memset(g, 0x3f, sizeof(g));
	for (int i = 0; i < m; ++i)
	{
		int a, b, c;
		cin >> a >> b >> c;
		g[b][a] = g[a][b] = min(g[a][b], c);
	}

	per[c1] = p[c1], d[c1] = 0, sum[c1] = 1;

	for (int i = 0; i < n; ++i)
	{
		int t = -1;

		for (int j = 0; j < n; ++j)
		{
			if (!st[j] && (t == -1 || d[t] > d[j])) t = j;
		}

		for (int v = 0; v < n; ++v)
		{
			if (d[t] + g[t][v] < d[v])
			{
				sum[v] = sum[t];
				per[v] = per[t] + p[v];
				d[v] = d[t] + g[t][v];
			}
			else if (d[t] + g[t][v] == d[v])
			{
				sum[v] += sum[t];
				per[v] = max(per[v], per[t] + p[v]);
			}
		}
		st[t] = true;
	}

	cout << sum[c2] << " " << per[c2] << endl;
	return 0;
}

分析思路

1.每个城市有的人手可以理解成点权,城市间的距离是边权,那么其实就是一道最短路的题目。

2.题目要求的并不是最短路那么简单,而是求最短路的条数和经过点权和最大的情况。

3.对于最短路条数和点权,可以仿照Dijkstra算法的思想,在迭代更新最短距离的时候做出额外的更新。首先开出sum[i]表示到第i个城市最短路径条数,per[i]表示到第i个点最短路径走法下能召集到的最多人数。然后修改原算法:如果距离是更短的情况下(也就是原算法中要更新最短距离的时候)要改变那个点的sum为最短点的sum,同时把点权情况也更改为最短点的per + 该点的权。 如果是距离相等,那最短路径数量要加上sum[t]。然后per取二者更大的情况。

4.朴素版和堆优化版Dijkstra这道题目都是可以的,因为数据规模很小( N < = 500 N <= 500 N<=500) 而根据y神的说法,朴素版适用稠密图,堆优化版本适用于稀疏图。而稠密图和稀疏图的存储方式又有差异,邻接矩阵好写但效率低,邻接表效率高但难写,不过其实熟练了之后邻接表的方式应该是更加具有通用性的。

5.还要注意一个点就是这道题的图是无向图,那么在输入的时候两个方向都要连上边,邻接表也不例外。

文末广告

学习算法和数据结构真的是个很累的过程,不会做只能求助于题解。 因为写代码这个东西基本上是千人千面。同时网络上搜到的题解很多要么用到的是自己还没学到的知识,看不懂;要么内核过于简陋,只能糊弄当前题目,不具有普适性。
如果你是一个喜欢做洛谷,ACwing和PTA的题目的同学,欢迎关注我的博客,我主要在这三个平台上做题,认为有价值和有难度的题目我会写题解发布出来。

TreeTraverler的往期文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

102101141高孙炜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值