This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct state_t {
ll id, inc;
ll cost, inw;
};
struct edge_t {
ll to, col;
ll cost;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
auto cmp = [](const state_t& a, const state_t& b){return a.cost > b.cost;};
priority_queue<state_t, vector<state_t>, decltype(cmp)> pq(cmp);
ll n, m, i, tu, tv, tw, tc;
cin >> n >> m;
vector<vector<edge_t>> adj(n + 1);
vector<unordered_map<int, ll>> coltot(n + 1);
vector<unordered_map<int, vector<edge_t>>> c2e(n + 1);
for (i = 0; i < m; i++) {
cin >> tu >> tv >> tc >> tw;
adj[tu].push_back({tv, tc, tw});
adj[tv].push_back({tu, tc, tw});
coltot[tu][tc] += tw;
coltot[tv][tc] += tw;
c2e[tu][tc].push_back({tv, tc, tw});
c2e[tv][tc].push_back({tu, tc, tw});
}
vector<map<int, ll>> best(n + 1);
pq.push({1, 0, 0});
best[1][0] = 0;
while (not pq.empty()) {
state_t cur = pq.top();
pq.pop();
if (cur.id == n) {
cout << cur.cost;
return 0;
}
if (cur.cost > best[cur.id][cur.inc]) continue;
if (cur.inc == 0) {
for (edge_t& e : adj[cur.id]) {
ll tmp = cur.cost + coltot[cur.id][e.col] - e.cost;
if (best[e.to].find(0) == best[e.to].end() or best[e.to][0] > tmp) {
pq.push({e.to, 0, tmp, 0});
best[e.to][0] = tmp;
}
tmp = cur.cost + e.cost;
if (best[e.to].find(e.col) == best[e.to].end() or best[e.to][e.col] > tmp) {
pq.push({e.to, e.col, tmp, e.cost});
best[e.to][e.col] = tmp;
}
}
} else {
if (best[cur.id].find(0) == best[cur.id].end() or best[cur.id][0] > cur.cost) {
pq.push({cur.id, 0, cur.cost, 0});
best[cur.id][0] = cur.cost;
}
for (edge_t& e : c2e[cur.id][cur.inc]) {
ll tmp = cur.cost + coltot[cur.id][e.col] - e.cost - cur.inw;
if (best[e.to].find(0) == best[e.to].end() or best[e.to][0] > tmp) {
pq.push({e.to, 0, tmp, 0});
best[e.to][0] = tmp;
}
}
}
}
cout << -1;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |