跳到正文

个人记录

富裕的

Edge_Faded约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年10月31日
收录
2026年10月31日
更新
2026年10月31日
篇幅
约 3 分钟

版本

  1. v3当前
  2. v2
  3. v1
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 10005;
const int M = 100005;
struct Edge { int to, w; };
vector<Edge> g[N * 3];
struct Node {
    int id, dis;
    bool operator<(const Node& x) const { return dis > x.dis; }
};
int dis[N * 3];
void add(int u, int v, int w) {
    g[u].push_back({v, w});
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        if (d == 0) {
            for (int k = 0; k < 3; k++) {
                add(a + k * n, b + k * n, c);
                add(b + k * n, a + k * n, c);
            }
        } else {
            for (int k = 0; k < 2; k++) {
                add(a + k * n, b + (k + 1) * n, c);
                add(b + k * n, a + (k + 1) * n, c);
            }
        }
    }
    int S = 1;
    memset(dis, 0x3f, sizeof dis);
    dis[S] = 0;
    priority_queue<Node> q;
    q.push({S, 0});

    while (!q.empty()) {
        auto [x, d] = q.top(); q.pop();
        if (d > dis[x]) continue;
        for (auto& e : g[x]) {
            int y = e.to, w = e.w;
            if (dis[y] > dis[x] + w) {
                dis[y] = dis[x] + w;
                q.push({y, dis[y]});
            }
        }
    }
    int original = dis[n];
    int after = min({dis[n], dis[n + n], dis[n + 2 * n]});
    cout << original - after << '\n';
    return 0;
}

1

原文