题解:P15803 [GESP202603 七级] 物流网络
_OokoukioO_ · · 题解
纪念我 GESP 七级
题意
在模板上多一个元素
求
思路
首先如果直接记录
因此
然后这题就秒了。
AC 代码
#include <bits/stdc++.h>
using namespace std;
#define in cin
#define out cout
#define int long long
#define ios ios::sync_with_stdio(false), in.tie(nullptr), out.tie(nullptr)
int dis[5005][5005], n, m, cost[5005];
struct node {
int a, b, c, d;
friend bool operator <(node x, node y) {
return x.a - cost[x.d] > y.a - cost[y.d];
}
};
priority_queue <node> q;
bool vis[5005][5005];
vector <node> e[5005];
signed main() {
ios;
in >> n >> m;
memset(dis, 0x3f, sizeof(dis)), dis[1][0] = 0, q.push({0, 1, 0, 0});
for (int i = 1; i <= m; i++) {
int u, v, w, b;
in >> u >> v >> w >> b;
e[u].push_back({v, w, b, i}), e[v].push_back({u, w, b, i}), cost[i] = w;
}
int ans = LLONG_MAX;
while (!q.empty()) {
auto [ur, u, ures, uid] = q.top();
q.pop();
if (u == n) {
ans = min(ans, ur - cost[uid]);
break;
}
if (vis[u][uid])
continue;
vis[u][uid] = true;
for (auto [v, w, b, id] : e[u]) {
int vmax, vid;
if (ures < b || (ures == b && cost[uid] < w))
vmax = b, vid = id;
else
vmax = ures, vid = uid;
if (dis[u][uid] + w < dis[v][vid])
dis[v][vid] = dis[u][uid] + w, q.push({dis[v][vid], v, vmax, vid});
}
}
if (ans == LLONG_MAX)
out << -1;
else
out << ans;
return 0;
}