이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct state_t {
int id, inc;
ll cost;
};
struct edge_t {
int 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);
int 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 and cur.inc == 0) {
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});
best[e.to][0] = tmp;
}
tmp = cur.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});
best[e.to][e.col] = tmp;
}
tmp = cur.cost + e.cost;
if (best[e.to].find(0) == best[e.to].end() or best[e.to][0] > tmp) {
pq.push({e.to, 0, tmp});
best[e.to][0] = tmp;
}
}
} else {
for (edge_t& e : c2e[cur.id][cur.inc]) {
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});
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... |