在阅读本博客之前需要先对Dijkstra算法有一定的了解。
往常的Dijkstra一般都是以路径长度作为单一的权重,但是有些问题可能会要求存在路径相同时取通过费用最少、如果通过费用还一样就取用最新修建的道路等等可以叠加无限多个条件,就不止一个条件那么简单了。
除此之外,还可能会要求如果A到X有多条最短路径,就要输出最短路径的条数。
问题引入
本问题取自PAT天梯赛直捣黄龙【点击此处访问】
本题是一部战争大片 —— 你需要从己方大本营出发,一路攻城略地杀到敌方大本营。首先时间就是生命,所以你必须选择合适的路径,以最快的速度占领敌方大本营。当这样的路径不唯一时,要求选择可以沿途解放最多城镇的路径。若这样的路径也不唯一,则选择可以有效杀伤最多敌军的路径。
输入格式:
输入第一行给出 2 个正整数 N(2 ≤ N ≤ 200,城镇总数)和 K(城镇间道路条数),以及己方大本营和敌方大本营的代号。随后 N-1 行,每行给出除了己方大本营外的一个城镇的代号和驻守的敌军数量,其间以空格分隔。再后面有 K 行,每行按格式城镇1 城镇2 距离给出两个城镇之间道路的长度。这里设每个城镇(包括双方大本营)的代号是由 3 个大写英文字母组成的字符串。
输出格式:
按照题目要求找到最合适的进攻路径(题目保证速度最快、解放最多、杀伤最强的路径是唯一的),并在第一行按照格式己方大本营->城镇1->…->敌方大本营输出。第二行顺序输出最快进攻路径的条数、最短进攻距离、歼敌总数,其间以 1 个空格分隔,行首尾不得有多余空格。
输入样例:
10 12 PAT DBY
DBY 100
PTA 20
PDS 90
PMS 40
TAP 50
ATP 200
LNN 80
LAO 30
LON 70
PAT PTA 10
PAT PMS 10
PAT ATP 20
PAT LNN 10
LNN LAO 10
LAO LON 10
LON DBY 10
PMS TAP 10
TAP DBY 10
DBY PDS 10
PDS PTA 10
DBY ATP 10
输出样例:
PAT->PTA->PDS->DBY
3 30 210
题目分析
题目大意是说,从己方阵营杀到敌方阵营,路上会救城镇,也会杀死城镇上的敌军,求出最优的那条路径。
什么是最优的路径,题目说明了三点:
- 距离最近(也就是速度最快)
- 如果两条路距离相同,那么选经过的点数最多(也就是拯救城镇的数量最多)
- 如果两条路距离相同,经过的点数也相同,那么就选能杀死最多的敌人
同时还要求输出最短路径的条数。
朴素Dijkstra
多条件与最短路径计数问题在dijkstra函数中的注释已说明
#include<bits/stdc++.h>
using namespace std;
//#define DEBUG
struct Camp {
string name;
int code;
int person;
};
int n, k; //n个大本营,k个道路
map<string, Camp> nameToCamp; // 名字索引阵营类
map<int, Camp> codeToCamp; // 下标索引阵营
const int MAX_SIZE = 200 + 10; // 最多200个阵营
#define DISCONNECT INT_MAX // 定义不相连的两个城镇距离时INT_MAX
int distances[MAX_SIZE][MAX_SIZE]; // 邻接矩阵,不相连则为INT_MAX,相连则为距离
void init() {
for (auto &distance : distances) {
for (int &j : distance) {
j = DISCONNECT;
}
}
}
void dijkstra(const string &start, const string &end) {
vector<bool> vis(n, false); // 是否被确定过了,如果确认了就不要再去动了,因为已经是最优解了
vector<int> shortDistance(n, DISCONNECT); // 第一个条件:最短路径,记录从start到每个点的最短距离
vector<int> liberate(n, 0); // 第二个条件:最大拯救城镇数量,记录从start到每个点在距离最近的前提下最多能救多少个城镇
vector<int> canKill(n, 0); // 第三个条件:最大杀敌数量,记录从start到每个点在距离最近且拯救最多城镇的前提下最大杀敌数
vector<int> cntShortPath(n, 0); // 记录从start到每个点有多少个最短路径(忽略后两个条件)
vector<string> path(n); // 记录从start到每个点的最优路径的字符串表达
int startCode = nameToCamp[start].code; // 开始坐标,其实就是0
shortDistance[startCode] = 0; // 从start到start距离为0
cntShortPath[startCode] = 1; // 一开始从start到start就一个路径
path[startCode] = start;
while (true) { // 这里用for循环n也是一样的,只是这样更省事
int index = -1;
int minDistance = DISCONNECT;
// 这里还是一样的,找到没有确认过的但是是里start最近的点
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
if (shortDistance[i] < minDistance) {
index = i;
minDistance = shortDistance[i];
}
}
}
if (index == -1) break;
vis[index] = true;
for (int i = 0; i < n; ++i) {
if (!vis[i] && distances[i][index] != DISCONNECT) {
// 如果index与i相连,那么就可能可以优化,让start->i变成start->index->i
// 这里定义了最新的权重,分别为最新的距离、最新的解放城镇数量以及最新的击杀数
int newDistance = minDistance + distances[i][index];
int newLiberate = liberate[index] + 1;
int newKill = canKill[index] + codeToCamp[i].person;
// 这里就是记录多少条最短路径(只考虑距离的最短路径)
// PS: 这里的other一定不可能是index,因为other必须是上几轮循环中确认的坐标,才能继续在other的基础上找其他点
// 1. 如果 start->other->i == start->index->i
// 那么就说明start到i,除了可以通过other,还可以通过index
// 2. 如果 start->other->i > start->index->i
// 那么就说明start到i还有最短的路径,于是抛弃之,并让之等于Start->index的最短路径数量
if (shortDistance[i] == newDistance) {
cntShortPath[i] += cntShortPath[index];
} else if (shortDistance[i] > newDistance) {
cntShortPath[i] = cntShortPath[index];
}
// 这里是多条件的核心,无非多加几个条件,顾名思义
if ((shortDistance[i] > newDistance) ||
(shortDistance[i] == newDistance && liberate[i] < newLiberate) ||
(shortDistance[i] == newDistance && liberate[i] == newLiberate && canKill[i] < newKill)) {
// 可以同时更新这些权重,不必再将三个条件分开来写了,否则太臃肿了
shortDistance[i] = newDistance;
liberate[i] = newLiberate;
canKill[i] = newKill;
path[i] = path[index] + "->" + codeToCamp[i].name;
}
}
}
}
int endCode = nameToCamp[end].code;
cout << path[endCode] << endl;
// 最快进攻路径的条数、最短进攻距离、歼敌总数
cout << cntShortPath[endCode] << " " << shortDistance[endCode] << " " << canKill[endCode] << endl;
#ifdef DEBUG
for (const auto &item : path) {
cout << "--" << item << endl;
}
#endif
}
int main() {
init();
string start, end;
cin >> n >> k >> start >> end;
Camp startCamp = {start, 0, 0};
nameToCamp[start] = codeToCamp[0] = startCamp;
for (int i = 1; i < n; ++i) {
string name;
int person;
cin >> name >> person;
Camp camp = {name, i, person};
nameToCamp[name] = codeToCamp[i] = camp;
}
for (int i = 0; i < k; ++i) {
string from, to;
int length;
cin >> from >> to >> length;
int startCode = nameToCamp[from].code;
int endCode = nameToCamp[to].code;
distances[startCode][endCode] = distances[endCode][startCode] = length;
}
dijkstra_pile(start, end);
return 0;
}
堆优化Dijkstra
其实对于多条件+最短路径计数两个附加条件,对堆优化本身没有影响,堆优化只是将for循环找最短路径换成了用优先队列自动排序的top操作。其附加条件的核心不变。
#include<bits/stdc++.h>
using namespace std;
//#define DEBUG
struct Camp {
string name;
int code;
int person;
};
int n, k; //n个大本营,k个道路
map<string, Camp> nameToCamp; // 名字索引阵营类
map<int, Camp> codeToCamp; // 下标索引阵营
const int MAX_SIZE = 200 + 10; // 最多200个阵营
#define DISCONNECT INT_MAX // 定义不相连的两个城镇距离时INT_MAX
int distances[MAX_SIZE][MAX_SIZE]; // 邻接矩阵,不相连则为INT_MAX,相连则为距离
void init() {
for (auto &distance : distances) {
for (int &j : distance) {
j = DISCONNECT;
}
}
}
void dijkstra_pile(const string &start, const string &end) {
vector<bool> vis(n, false);
vector<int> shortDistance(n, DISCONNECT);
vector<int> canKill(n, 0);
vector<int> liberate(n, 0);
vector<int> cntShortPath(n, 0);
vector<string> path(n);
int startCode = nameToCamp[start].code;
shortDistance[startCode] = 0;
cntShortPath[startCode] = 1;
path[startCode] = start;
// 堆优化第一步:使用优先队列
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, 0}); // {length, code}
while (!pq.empty()) {
// 堆优化第二部:将for循环找最短路径换成优先队列的top
pair<int, int> p = pq.top();
pq.pop();
int minDistance = p.first;
int index = p.second;
if(vis[index]) continue;
vis[index] = true;
for (int i = 0; i < n; ++i) {
if (!vis[i] && distances[i][index] != DISCONNECT) {
int newDistance = minDistance + distances[i][index];
int newLiberate = liberate[index] + 1;
int newKill = canKill[index] + codeToCamp[i].person;
// 记录最短路径条数的道理还是一样的
if (shortDistance[i] == newDistance) {
cntShortPath[i] += cntShortPath[index];
} else if (shortDistance[i] > newDistance) {
cntShortPath[i] = cntShortPath[index];
}
// 多条件的道理也还是一样的
if ((shortDistance[i] > newDistance) ||
(shortDistance[i] == newDistance && liberate[i] < newLiberate) ||
(shortDistance[i] == newDistance && liberate[i] == newLiberate && canKill[i] < newKill)) {
shortDistance[i] = newDistance;
liberate[i] = newLiberate;
canKill[i] = newKill;
path[i] = path[index] + "->" + codeToCamp[i].name;
pq.push({newDistance, i});
}
}
}
}
int endCode = nameToCamp[end].code;
cout << path[endCode] << endl;
// 最快进攻路径的条数、最短进攻距离、歼敌总数
cout << cntShortPath[endCode] << " " << shortDistance[endCode] << " " << canKill[endCode] << endl;
#ifdef DEBUG
for (const auto &item : path) {
cout << "--" << item << endl;
}
#endif
}
int main() {
init();
string start, end;
cin >> n >> k >> start >> end;
Camp startCamp = {start, 0, 0};
nameToCamp[start] = codeToCamp[0] = startCamp;
for (int i = 1; i < n; ++i) {
string name;
int person;
cin >> name >> person;
Camp camp = {name, i, person};
nameToCamp[name] = codeToCamp[i] = camp;
}
for (int i = 0; i < k; ++i) {
string from, to;
int length;
cin >> from >> to >> length;
int startCode = nameToCamp[from].code;
int endCode = nameToCamp[to].code;
distances[startCode][endCode] = distances[endCode][startCode] = length;
}
dijkstra_pile(start, end);
return 0;
}