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;
typedef long long ll;
const ll INF = 1e18+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define ull unsigned ll
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
vector<vector<ii> >adj;
vector<vector<iii> >exo;
vector<ll> dist;
void dijkstra(ll s){
priority_queue<ii,vector<ii>, greater<ii> >pq;
pq.push(ii(0,s));
dist[s] = 0;
while(!pq.empty()){
ii f = pq.top();
pq.pop();
ll w = f.F;
ll u = f.S;
if(w > dist[u]){
continue;
}
for(auto v:adj[u]){
if(dist[u] + v.S < dist[v.F]){
dist[v.F] = dist[u] + v.S;
pq.push(ii(dist[v.F],v.F));
}
}
}
}
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n,m;
cin>>n>>m;
ll siz = 2 * (n + m + 5);
adj.assign(siz,vector<ii>());
exo.assign(siz,vector<iii>());
dist.assign(siz,1e18);
ll fake = n+5;
f(i,0,m){
ll a,b,c,d;
cin>>a>>b>>c>>d;
adj[a].pb(ii(b,d));
adj[b].pb(ii(a,d));
exo[a].pb(iii(b,ii(c,d)));
exo[b].pb(iii(a,ii(c,d)));
}
ll sum[m+1] = {0};
ll xroma[m+1] = {0};
f(i,1,n+1){
for(auto x:exo[i]){
sum[x.S.F] += x.S.S;
if(!xroma[x.S.F]){
xroma[x.S.F] = fake;
fake++;
}
}
for(auto x:exo[i]){
adj[i].pb(ii(x.F,sum[x.S.F] - x.S.S));
adj[x.F].pb(ii(xroma[x.S.F],sum[x.S.F]));
//adj[xroma[x.S.F]].pb(ii(x.F,-x.S.S));
}
for(auto x:exo[i]){
sum[x.S.F] -= x.S.S;
xroma[x.S.F] = 0;
}
}
dijkstra(1);
if(dist[n] == 1e18){
dist[n] = -1;
}
cout<<dist[n]<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |