제출 #1234519

#제출 시각아이디문제언어결과실행 시간메모리
1234519newbie_tRobot (JOI21_ho_t4)C++20
100 / 100
661 ms75704 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
 
const ll LINF = 1e15;
const int N = 1e5+5;
const int M = 2e5+5;
 
int n, m;
map<int, vector<ii>> graph[N];
ll dp[N];
map<int, ll> dp2[N], sum[N];
 
int main() {
  cin >> n >> m;

  foru(i, 1, m) {
    int u, v, c, p;
    cin >> u >> v >> c >> p;
    graph[u][c].emplace_back(v, p);
    graph[v][c].emplace_back(u, p);
    sum[u][c] += p;
    sum[v][c] += p;
  } 

  memset(dp, 0x3f, sizeof(dp));
  priority_queue<pair<ll, ii>> pq;
  dp[1] = 0;
  pq.push({0, {1, 0}});

  while (!pq.empty()) {
    auto top = pq.top(); pq.pop();

    ll cost = top.fi;
    int u = top.se.fi, c = top.se.se;

    if (!c) {
      if (-cost != dp[u]) continue;
      fore(nc, graph[u]) fore(info, nc.se) {
        int v = info.fi, p = info.se;
        ll ncost;

        ncost = sum[u][nc.fi] - p - cost;
        if (ncost < dp[v]) {
          dp[v] = ncost;
          pq.push({-ncost, {v, 0}});
        }

        ncost = p - cost;
        if (ncost < dp[v]) {
          dp[v] = ncost;
          pq.push({-ncost, {v, 0}});
        }

        ncost = -cost;
        if ((dp2[v].find(nc.fi) == dp2[v].end()) || ncost < dp2[v][nc.fi]) {
          dp2[v][nc.fi] = ncost;
          pq.push({-ncost, {v, nc.fi}});
        }
      }
    } 
    else {
      if (-cost != dp2[u][c]) continue;
      ll ncost = sum[u][c] - cost;
      fore(info, graph[u][c]) {
        int v = info.fi, p = info.se;
        if (ncost - p < dp[v]) {
          dp[v] = ncost - p;
          pq.push({-ncost + p, {v, 0}});
        }
      }
    }
  }

  cout << (dp[n] < LINF ? dp[n] : -1);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...