跳到正文

剪贴板

剪贴板 pz3ky7pe

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
using namespace std;

namespace Sweet {

template <typename T> inline void read(T &x) {
    char ch;
    int f = 1;
    while (ch = getchar(), ch > '9' || ch < '0')
        if (ch == '-') f = -1;
    x = (ch ^ 48);
    while (ch = getchar(), ch >= '0' && ch <= '9')
        x = x * 10 + (ch ^ 48);
    x *= f;
}
template <typename T> inline void write(T x) {
    static int stk[100], top = 0;
    if (x == 0) return (void)putchar('0');
    if (x < 0) x = -x, putchar('-');
    while (x) stk[++top] = x % 10, x /= 10;
    while (top) putchar(stk[top--] + '0');
}

typedef long long ll;
const int N = 1e5 + 10, V = 4e5 + 10;

vector<pair<int, int> > e[V];
vector<pair<int, int> >::iterator to;
inline void add(int x, int y, int z) {
    e[x].push_back(make_pair(y, z));
}

struct T {
    int x;
    ll dis;
    T(int X, ll Dis) : x(X), dis(Dis) {}
    bool operator<(const T &rhs) const { return dis > rhs.dis; }
};
int s = 0, t = 1;
ll dis[V];
ll Dijkstra() {
    memset(dis, 0x3f, sizeof(dis));
    priority_queue<T> q;
    q.push(T(s, dis[s] = 0));
    static T u = T(0, 0);
    while (!q.empty()) {
        u = q.top(), q.pop();
        if (u.dis != dis[u.x]) continue;
        for (to = e[u.x].begin(); to != e[u.x].end(); ++to) {
            if (dis[to->first] > u.dis + to->second) {
                q.push(T(to->first, dis[to->first] = u.dis + to->second));
            }
        }
    }
    return dis[t];
}

struct Edge {
    int v, w, id;
    bool operator<(const Edge &rhs) const { return w < rhs.w; }
};
vector<Edge> g[N];
vector<Edge>::iterator it;

int n, m;
inline void main() {
    read(n), read(m);
    for (int i = 1, a, b, c; i <= m; ++i) {
        read(a), read(b), read(c);
        g[a].push_back((Edge){b, c, i << 1});
        g[b].push_back((Edge){a, c, i << 1 | 1});
    }
    for (int i = 1; i <= n; ++i) {
        if (g[i].empty()) continue;
        sort(g[i].begin(), g[i].end());
        for (it = g[i].begin(); it != g[i].end(); ++it) {
            add(it->id ^ 1, it->id, it->w);
            if (i == 1) add(s, it->id, it->w);
            if (it->v == n) add(it->id, t, it->w);
        }
        for (it = g[i].begin(), ++it; it != g[i].end(); ++it) {
            add(it->id, (it - 1)->id, 0);
            add((it - 1)->id, it->id, it->w - (it - 1)->w);
        }
    }
    write(Dijkstra());
}

}    // namespace Sweet

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
    freopen("D:\\OI\\STDquantum\\test.in", "r", stdin);
    freopen("D:\\OI\\STDquantum\\test.out", "w", stdout);
#endif
#ifndef LOCAL
    freopen("P6822_[PA2012]_Tax.in", "r", stdin);
    freopen("P6822_[PA2012]_Tax.out", "w", stdout);
#endif
#endif
    Sweet::main();
    return 0;
}