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;
#define fileIO(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
#define SPEED ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define EL cout << '\n'
#define dbg(x) cout << #x << " = " << (x) << ' '
#define dbgp(x) cout << #x << " = (" << (x.fi) << ", " << (x.se) << ") "
#define dbga(x, l, r) {cout << #x << '[' << l << ", " << r << "] = { "; for(int i = l; i <= (int)r; ++i) cout << x[i] << ' '; cout << "} ";}
#define epl emplace
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
template<class T1, class T2> bool minimize(T1 &a, T2 b){return a > b ? a = b, true : false;}
template<class T1, class T2> bool maximize(T1 &a, T2 b){return a < b ? a = b, true : false;}
const int N = 1e5 + 3;
const ll oo = 1e17;
int n, m;
struct adjNode{
pii v;
int c, p;
};
map< int, ll > sump[N], d[N];
map< int, vector<adjNode> > adj[N];
priority_queue< pair<ll, pii>, vector< pair<ll, pii> >, greater< pair<ll, pii> > > pq;
void add_edge(pii a, pii b, int c, int p){
adj[a.fi][a.se].pb({b, c, p});
adj[b.fi][b.se].pb({a, c, p});
}
int main(){
SPEED; fileIO("text");
cin >> n >> m;
for(int i = 1; i <= m; ++i){
int u, v, c, p;
cin >> u >> v >> c >> p;
add_edge({u, 0}, {v, 0}, c, p);
add_edge({u, 0}, {v, c}, c, p);
add_edge({u, c}, {v, 0}, c, p);
}
for(int u = 1; u <= n; ++u){
//dbg(u); EL;
for(auto &[c, x] : adj[u]){
ll tot = 0;
for(auto e : x) tot += e.p;
d[u][c] = oo; sump[u][c] = tot;
//dbg(c); dbg(sump[u][c]); EL;
}
}
d[1][0] = 0; pq.epl(0, mp(1, 0));
while(!pq.empty()){
ll dist = pq.top().fi;
int u, x; tie(u, x) = pq.top().se;
pq.pop();
if(dist != d[u][x]) continue;
for(auto &[b, c, p] : adj[u][x]){
int v, y; tie(v, y) = b;
if(x == 0){
if(!y && minimize(d[v][y], d[u][x] + min(1LL * p, sump[u][c] - p))) pq.epl(d[v][y], mp(v, y));
else if(y && minimize(d[v][y], d[u][x])) pq.epl(d[v][y], mp(v, y));
}
else if(!y && minimize(d[v][y], d[u][x] + sump[u][x] - p)) pq.epl(d[v][y], mp(v, y));
}
}
cout << (d[n][0] == oo ? -1 : d[n][0]);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:5:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define fileIO(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:40:12: note: in expansion of macro 'fileIO'
40 | SPEED; fileIO("text");
| ^~~~~~
Main.cpp:5:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define fileIO(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:40:12: note: in expansion of macro 'fileIO'
40 | SPEED; fileIO("text");
| ^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |