# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
836775 | TS_2392 | 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.
#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 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;}
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){
for(auto &[c, x] : adj[u]){
ll tot = 0;
for(auto e : x) tot += e.p;
d[u][c] = oo; sump[u][c] = tot;
}
}
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;
}