이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |