제출 #706355

#제출 시각아이디문제언어결과실행 시간메모리
706355becaidoRobot (JOI21_ho_t4)C++17
100 / 100
841 ms85668 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const ll INF = 1e18;
const int SIZE = 1e5 + 5;

int n, m;
ll ans = INF;
unordered_set<int> us[SIZE];
unordered_map<int, vector<pair<int, int>>> adj[SIZE];
priority_queue<tuple<ll, int, int>, vector<tuple<ll, int, int>>, greater<tuple<ll, int, int>>> pq;

void solve() {
    cin >> n >> m;
    while (m--) {
        int a, b, c, w;
        cin >> a >> b >> c >> w;
        adj[a][c].pb(b, w);
        adj[b][c].pb(a, w);
    }
    pq.emplace(0, 1, 0);
    while (pq.size()) {
        auto [d, pos, color] = pq.top();
        pq.pop();
        if (us[pos].count(color)) continue;
        if (pos == n && color == 0) ans = min(ans, d);
        us[pos].insert(color);
        if (color == 0) {
            for (auto [c, v] : adj[pos]) {
                ll sum = 0;
                for (auto [np, w] : v) sum += w;
                for (auto [np, w] : v) {
                    pq.emplace(d + min((ll) w, sum - w), np, 0);
                    pq.emplace(d, np, c);
                }
            }
        } else {
            ll sum = 0;
            for (auto [np, w] : adj[pos][color]) sum += w;
            for (auto [np, w] : adj[pos][color]) pq.emplace(d + sum - w, np, 0);
        }
    }
    cout << (ans == INF ? -1 : ans) << '\n';
}

int main() {
    Waimai;
    solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...