PTA(3)–Emergency
解答:
1.深度优先搜索:
注意此处不能将对角线初始化为0,因为在没有救援队数量的属性时,自己访问自己对结果没有影响,但本题如果自己访问自己就会使同一个城市的救援队数量加两次
#include<iostream>
using namespace std;
int m, n, c1, c2;
int e[501][501];
int Min = 999999999;
int a, b, c, teams = 0;
int book[501] = {
0 }, City[501],paths;
void dfs(int cur, int dis, int curteams) {
if (dis > Min) return;
if (cur == c2) {
if (dis == Min) {
//同距离结,判断是否需要更新队伍数
if (curteams > teams) teams = curteams;
paths++;
}
else {
//如果不是同距离解,那么一定需要更新最小距离及队伍数量
Min = dis;
teams = curteams;
paths=1;
}
}
else {
for (int i = 0; i < m; i++)
if (e[cur][i] != 999999999 && book[i] == 0) {
book[i] = 1;
dfs(i, dis + e[cur][i], curteams + City[i]);
book[i] = 0;
}
}
return;
}
int main() {
cin >> m >> n >> c1 >> c2;
for (int i = 0; i < m; i++) {
cin >> City[i];
}
for (int i = 0; i < m; i++