#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 510, INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
struct node {
int to, len, time;
};
int n, m, s, d;
vector<node> v[MAX_N];
int dis_t[MAX_N], dis_l[MAX_N], pre[MAX_N], num[MAX_N];
bool flag;
vector<int> ti, le;
void dfs(int x) {
if (x == pre[x]) {
if (!flag) ti.push_back(x);
else le.push_back(x);
return;
}
dfs(pre[x]);
if (!flag) ti.push_back(x);
else le.push_back(x);
}
void Dijstra(int s) {
memset(dis_t, INF, sizeof dis_t);
memset(dis_l, INF, sizeof dis_l);
iota(pre, pre + n, 0);
dis_l[s] = dis_t[s] = 0, num[s] = 1;
priority_queue<PII, vector<PII>, greater<PII>> que;
if (!flag) que.push({dis_t[s], s});
else que.push({dis_l[s], s});
while (que.size()) {
auto t = que.top();
que.pop();
int u = t.second, e = t.first;
if (e > dis_t[u] && !flag || e > dis_l[u] && flag) continue;
for (auto c : v[u]) {
if (!flag && dis_t[u] + c.time < dis_t[c.to]) {
dis_t[c.to] = dis_t[u] + c.time;
dis_l[c.to] = dis_l[u] + c.len;
pre[c.to] = u;
que.push({dis_t[c.to], c.to});
}
else if (!flag && dis_t[u] + c.time == dis_t[c.to] && dis_l[u] + c.len < dis_l[c.to]) {
dis_l[c.to] = dis_l[u] + c.len;
pre[c.to] = u;
}
if (flag && dis_l[u] + c.len < dis_l[c.to]) {
dis_l[c.to] = dis_l[u] + c.len;
num[c.to] = num[u] + 1;
pre[c.to] = u;
que.push({dis_l[c.to], c.to});
}
else if (flag && dis_l[u] + c.len == dis_l[c.to] && num[u] + 1 < num[c.to]) {
dis_l[c.to] = dis_l[u] + c.len;
num[c.to] = num[u] + 1;
pre[c.to] = u;
}
}
}
}
int main()
{
cin >> n >> m;
while (m--) {
int a, b, way, len, time;
scanf("%d%d%d%d%d", &a, &b, &way, &len, &time);
v[a].push_back({b, len, time});
if (!way) v[b].push_back({a, len, time});
}
cin >> s >> d;
Dijstra(s);
int time = dis_t[d];
dfs(d);
flag = true;
Dijstra(s);
int dis = dis_l[d];
dfs(d);
if (ti == le) {
printf("Time = %d; Distance = %d: ", time, dis);
for (int i = 0; i < ti.size(); i++) {
if (i) cout << " => ";
cout << ti[i];
}
}
else {
cout << "Time = " << time << ": ";
for (int i = 0; i < ti.size(); i++) {
if (i) cout << " => ";
cout << ti[i];
}
puts("");
cout << "Distance = " << dis << ": ";
for (int i = 0; i < le.size(); i++) {
if (i) cout << " => ";
cout << le[i];
}
}
return 0;
}
其实判断两个vector相等直接==就能判断了
可以,今天我改改题解👌