L3-011 直捣黄龙
Statement
Metadata
- 作者: 陈越
- 单位: 浙江大学
- 代码长度限制: 16 KB
- 时间限制: 150 ms
- 内存限制: 64 MB
本题是一部战争大片 —— 你需要从己方大本营出发,一路攻城略地杀到敌方大本营。首先时间就是生命,所以你必须选择合适的路径,以最快的速度占领敌方大本营。当这样的路径不唯一时,要求选择可以沿途解放最多城镇的路径。若这样的路径也不唯一,则选择可以有效杀伤最多敌军的路径。
输入格式
输入第一行给出 2 个正整数 N(2 城镇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
输出样例
Solution
#include <bits/stdc++.h>
using namespace std;
#define db double
#define ll long long
#define INF 0x3f3f3f3f
#define N 220
struct Graph {
struct node {
int to, nx, w;
node() {}
node(int to, int nx, int w) : to(to), nx(nx), w(w) {}
} a[N * N];
int head[N], pos;
void init() {
memset(head, 0, sizeof head);
pos = 0;
}
void add(int u, int v, int w) {
a[++pos] = node(v, head[u], w);
head[u] = pos;
a[++pos] = node(u, head[v], w);
head[v] = pos;
}
} G;
#define erp(u) \
for (int it = G.head[u], v = G.a[it].to, w = G.a[it].w; it; it = G.a[it].nx, v = G.a[it].to, w = G.a[it].w)
string name[N];
map<string, int> id;
int n, m;
string str[2];
int st, ed;
int val[N];
struct node {
int u, a, b, c, fa;
node() {}
node(int u, int a, int b, int c, int fa) : u(u), a(a), b(b), c(c), fa(fa) {}
bool operator<(const node &other) const {
if (a != other.a)
return a > other.a;
if (b != other.b)
return b < other.b;
return c < other.c;
}
};
int dist[4][N];
bool used[N];
int pre[N];
void Dijkstra() {
for (int i = 1; i <= n; ++i) {
dist[0][i] = INF;
dist[1][i] = dist[2][i] = -INF;
used[i] = 0;
}
dist[0][1] = dist[1][1] = 0;
dist[2][1] = val[1];
dist[3][1] = 1;
priority_queue<node> pq;
pq.push(node(1, 0, 0, val[1], 0));
vector<int> res;
while (!pq.empty()) {
int u = pq.top().u, fa = pq.top().fa;
pq.pop();
if (used[u])
continue;
used[u] = 1;
pre[u] = fa;
if (u == ed) {
int it = u;
while (it) {
res.push_back(it);
it = pre[it];
}
reverse(res.begin(), res.end());
break;
}
erp(u) {
if (dist[0][v] == dist[0][u] + w)
dist[3][v] += dist[3][u];
if (dist[0][v] > dist[0][u] + w) {
dist[0][v] = dist[0][u] + w;
dist[1][v] = dist[1][u] + 1;
dist[2][v] = dist[2][u] + val[v];
dist[3][v] = dist[3][u];
pq.push(node(v, dist[0][v], dist[1][v], dist[2][v], u));
} else if (dist[0][v] == dist[0][u] + w && dist[1][v] < dist[1][u] + 1) {
dist[1][v] = dist[1][u] + 1;
dist[2][v] = dist[2][u] + val[v];
// dist[3][v] += dist[3][u];
pq.push(node(v, dist[0][v], dist[1][v], dist[2][v], u));
} else if (dist[0][v] == dist[0][u] + w && dist[1][v] == dist[1][u] + 1 &&
dist[2][v] < dist[2][u] + val[v]) {
dist[2][v] = dist[2][u] + val[v];
// dist[3][v] += dist[3][u];
pq.push(node(v, dist[0][v], dist[1][v], dist[2][v], u));
}
}
}
for (int i = 0, len = res.size(); i < len; ++i) {
cout << name[res[i]];
if (i == len - 1)
cout << "\n";
else
cout << "->";
}
cout << dist[3][ed] << " " << dist[0][ed] << " " << dist[2][ed] << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while (cin >> n >> m) {
id.clear();
G.init();
cin >> str[0] >> str[1];
id[str[0]] = 1;
name[1] = str[0];
st = 1;
ed = -1;
for (int i = 2; i <= n; ++i) {
cin >> name[i] >> val[i];
id[name[i]] = i;
if (ed == -1 && str[1] == name[i])
ed = i;
}
for (int i = 1, u, v, w; i <= m; ++i) {
string a, b;
cin >> a >> b >> w;
u = id[a], v = id[b];
G.add(u, v, w);
}
Dijkstra();
}
return 0;
}
Last update: May 4, 2022