L3-1 直捣黄龙 (30 分)
//dijkstar更新数据时分清楚优先度,距离>城市数>敌军数。
// 测试点2:最短距离相同时,更新最短路径条数,rootNum[i] += rootNum[middle], 千万不是直接+1,因为起点到middle的路径可能不止一条
如果错误比较多,考虑题意要求的是最短路径数,不是路过城市数
本题是一部战争大片 —— 你需要从己方大本营出发,一路攻城略地杀到敌方大本营。首先时间就是生命,所以你必须选择合适的路径,以最快的速度占领敌方大本营。当这样的路径不唯一时,要求选择可以沿途解放最多城镇的路径。若这样的路径也不唯一,则选择可以有效杀伤最多敌军的路径。
输入格式: 输入第一行给出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
#include <bits/stdc++.h>
using namespace std;
#define INX 0x3f3f3f3f
typedef struct City {
string name;
int army;
}city;
vector<city> citys(210);
vector<int> distLen(210, INX);
vector<int> distCity(210, 0);
vector<int> distArmy(210, 0);
vector<bool> vist(210, false);
vector<int> rootNum(210, 0);
vector<int> pre(210, -1);
vector<vector<int>> Map(210, vector<int>(210,INX));
stack<int> path;
int n, m, Star, End;
int find(string str) {
for (int i = 0; i < n; i++) {
if (citys[i].name == str) {
return i;
}
}
}
void dijkstar(int s, int e) {
distLen[s] = 0;
rootNum[s] = 1;
for (int k = 0; k < n; k++) {
int Min = INX;
int middle = -1;
for (int i = 0; i < n; i++) {
if (!vist[i] && distLen[i] < Min) {
Min = distLen[i];
middle = i;
}
}
if (middle == -1) return;
vist[middle] = true;
for (int i = 0; i < n; i++) {
if (!vist[i] && Map[middle][i] < INX) {
if (distLen[i] > distLen[middle] + Map[middle][i]) {
distLen[i] = distLen[middle] + Map[middle][i];
distCity[i] = distCity[middle] + 1;
distArmy[i] = distArmy[middle] + citys[i].army;
rootNum[i] = rootNum[middle];
pre[i] = middle;
}
else if (distLen[i] == distLen[middle] + Map[middle][i]) {
rootNum[i] += rootNum[middle];
if (distCity[i] < distCity[middle] + 1) {
distCity[i] = distCity[middle] + 1;
distArmy[i] = distArmy[middle] + citys[i].army;
pre[i] = middle;
}
else if (distCity[i] == distCity[middle] + 1) {
if (distArmy[i] < distArmy[middle] + citys[i].army) {
distArmy[i] = distArmy[middle] + citys[i].army;
pre[i] = middle;
}
}
}
}
}
}
}
void backRoad() {
int step = End;
while (pre[step]!=-1){
path.push(step);
step = pre[step];
}
}
int main() {
int len;
string strA, strB;
cin >> n >> m >> strA >> strB;
citys[0].name = strA, citys[0].army = 0;
for (int i = 1; i < n; i++) {
cin >> citys[i].name >> citys[i].army;
}
Star = 0, End = find(strB);
for (int i = 0; i < m; i++) {
cin >> strA >> strB >> len;
int x = find(strA);
int y = find(strB);
Map[x][y] = Map[y][x] = len;
}
dijkstar(Star,End);
backRoad();
cout << citys[Star].name;
while (!path.empty()){
cout << "->" << citys[path.top()].name;
path.pop();
}
cout<<"\n"<<rootNum[End] << " " << distLen[End]<<" "<<distArmy[End]<<endl;
}
|