제출 #970875

#제출 시각아이디문제언어결과실행 시간메모리
970875htphong0909Robot (JOI21_ho_t4)C++17
34 / 100
3082 ms50256 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

vector<array<int, 3>> adj[100001];
int DP[100001];
map<int, int> DP2[100001], sum[100001];

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    //freopen("TEST.in", "r", stdin);
    //freopen("TEST.out", "w", stdout);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int u, v, c, w;
        cin >> u >> v >> c >> w;
        adj[u].push_back({v, c, w});
        adj[v].push_back({u, c, w});
        sum[u][c] += w;
        sum[v][c] += w;
    }
    memset(DP, 0x3f, sizeof DP);
    DP[1] = 1;
    priority_queue<array<int, 3>, vector<array<int, 3>>, greater<>> q;
    q.push({DP[1], 1, 0});
    while (!q.empty())
    {
        auto [d, u, pc] = q.top();
        //if (pc == 0) cerr << u << " " << pc << " " << d - 1 << endl;
        q.pop();
        for (const auto& [v, c, w] : adj[u])
        {
            if (pc == c && DP[v] > DP2[u][c] + sum[u][c] - w)
            {
                DP[v] = DP2[u][c] + sum[u][c] - w;
                q.push({DP[v], v, 0});
            }
            if (DP[v] > DP[u] + min(w, sum[u][c] - w))
            {
                DP[v] = DP[u] + min(w, sum[u][c] - w);
                q.push({DP[v], v, 0});
            }
            if (DP2[v][c] > DP[u] || !DP2[v][c])
            {
                DP2[v][c] = DP[u];
                q.push({DP2[v][c], v, c});
            }
        }
    }
    if (DP[n] > (int)1e18) cout << -1;
    else cout << DP[n] - 1;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...