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;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define maxn 220000
int n, m;
vector<pair<int,pair<int,int>>> g[maxn];
map<int,vector<pair<int,int>>> gc[maxn];
map<int,int> d[maxn], sm[maxn];
void shp() {
for(int i = 1; i <= n; i++) {
d[i][0] = inf;
for(auto V : g[i]) {
d[i][V.sc.fr] = inf;
}
}
d[1][0] = 0;
priority_queue<pair<int,pair<int,int>>> pq;
pq.push(mp(0,mp(1,0)));
while(pq.size()) {
int u = pq.top().sc.fr;
int dist = -pq.top().fr;
int cl = pq.top().sc.sc;
pq.pop();
if(dist != d[u][cl]) continue;
if(cl == 0) {
for(auto V : g[u]) {
int v = V.fr;
int c = V.sc.fr;
int w = V.sc.sc;
//usando o c e so podendo usar ele no proximo
if(d[v][c] > d[u][0]) {
d[v][c] = d[u][0];
pq.push(mp(-d[v][c],mp(v,c)));
}
//passando pro proximo e fodase e os caralho
if(d[v][0] > d[u][0] + min(sm[u][c]-w,w)) {
d[v][0] = d[u][0] + min(sm[u][c]-w,w);
pq.push(mp(-d[v][0],mp(v,0)));
}
}
}
else {
for(auto V : gc[u][cl]) {
int v = V.fr;
int w = V.sc;
int c = cl;
// nunca continua em c fodase vtnc
// if(d[v][c] > d[u][c] + sm[u][c] - w) {
// d[v][c] = d[u][c] + sm[u][c] - w;
// pq.push(mp(-d[v][c],mp(v,c)));
// }
//muda pra 0
if(d[v][0] > d[u][c] + sm[u][c] - w) {
d[v][0] = d[u][c] + sm[u][c] - w;
pq.push(mp(-d[v][0],mp(v,0)));
}
}
}
}
}
void solve() {
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int u, v, c, w;
cin >> u >> v >> c >> w;
g[u].pb(mp(v,mp(c,w)));
g[v].pb(mp(u,mp(c,w)));
}
for(int i = 1; i <= n; i++) {
for(auto V : g[i]) {
int v = V.fr;
int c = V.sc.fr;
int w = V.sc.sc;
gc[i][c].pb(mp(v,w));
sm[i][c]+= w;
}
}
shp();
int ans = d[n][0];
// for(int i = 1; i <= n; i++) {
// for(int j = 0; j < g[i].size(); j++) {
// cout << i << " " << j << " " << d[i][j] << endl;
// }
// }
if(ans == inf) cout << -1 << endl;
else cout << ans << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |