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 For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define RRH(v) v.resize(unique(all(v)) - v.begin())
using namespace std;
const int N = 1e6+7;
const int M = 1e9+7;
const ll oo = 1e18;
int n, m;
ll d[N];
struct data {
int v, c;ll p;
};
vector<data> g[N];
void dij() {
For (i, 2, n) d[i] = oo;
d[1] = 0;
priority_queue<ii, vector<ii>, greater<ii>> q;
q.push({0ll, 1});
unordered_map<ll,ll> ma, min_c;
while ((int)q.size() > 0) {
ii k = q.top();
q.pop();
if (d[k.se] != k.fi) continue;
for (auto x: g[k.se]) min_c[x.c] = oo, ma[x.c] = 0;
for (auto x: g[k.se]) {
ma[x.c] += x.p;
min_c[x.c] = min(min_c[x.c], d[x.v]);
}
for (auto x: g[k.se]) {
if (d[x.v] > k.fi + min(x.p, ma[x.c] - x.p)) {
d[x.v] = k.fi + min(x.p, ma[x.c] - x.p);
q.push({d[x.v], x.v});
}
if (ma[x.c] > x.p) if (d[x.v] > min_c[x.c] + ma[x.c] - x.p) {
d[x.v] = min_c[x.c] + ma[x.c] - x.p;
q.push({d[x.v], x.v});
}
}
}
if (d[n] >= oo) d[n] = -1;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
#define TASK ""
if (fopen (".inp", "r")) {
freopen (".inp", "r", stdin);
freopen (".out", "w", stdout);
}
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> m;
For (i, 1, m) {
int u, v, c, p; cin >> u >> v >> c >> p;
g[u].push_back({v, c, p});
g[v].push_back({u, c, p});
}
dij();
cout << d[n] << '\n';
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:60:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen (".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:61:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen (".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
Main.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen(TASK".out", "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... |