# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1108579 | chaoslong | Robot (JOI21_ho_t4) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Calm down.
// Think three times, code twice.
#include "bits/stdc++.h"
#define forr(_a,_b,_c) for(int _a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
#define fore(x, v) for(auto &x : v)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <ll,int>
#define pll pair <ll,ll>
#define piii pair <ll,pii>
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7; // 998244353
const ll oo = 1e18;
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] < oo ? dp[n] : -1);
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef LOCAL
freopen(file".inp","r",stdin);
freopen(file".out","w",stdout);
#endif // LOCAL
int t = 1;
// cin >> t;
while(t--) to_nho_cau();
}
/*
1.self check:
2.long long
3.size of array
4.code for testing
5.initializing
6.modulo number
*/