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>
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
void setIO(string name) {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (!name.empty()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
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() {
setIO("");
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<int, 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);
}
Compilation message (stderr)
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |