#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;
#define rep(x,start,end) for(auto x=start-(start>end);x!=end-(start>end);x+=(start<end?1:-1))
#define all(x) x.begin(),x.end()
ll MAX(ll a){return a;}
ll MIN(ll a){return a;}
template<typename... Args>
ll MAX(ll a, Args... args) { return max(a,MAX(args...)); }
template<typename... Args>
ll MIN(ll a, Args... args) { return min(a,MIN(args...)); }
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
const ll INF=1e18;
struct E{
int u,v;
ll len,flip;
int id;
E(int a,int b,int c,int d,int e){
u=a,v=b;
len=c,flip=d;
id=e;
}
};
int n,m;
vector<E> al[205];
vector<E> al_rev[205]; //store reversed edges
vector<E> edges;
priority_queue<ii,vector<ii>,greater<ii> > pq;
ll sf[205]; //source front
int ps[205]; //parent front
ll sb[205]; //source back
int pt[205]; //parent back
ll tf[205]; //sink front
ll tb[205]; //sink back
ll w[205];
map<int,ll> sp_f,sp_b; //is some edge in SP
ll temp,node; //stuff for dijk
void initial_d(){ //epic name
rep(x,0,205) sf[x]=sb[x]=tf[x]=tb[x]=INF;
sf[1]=0;
pq.push(ii(sf[1],1));
while (!pq.empty()){
temp=pq.top().first;
node=pq.top().second;
pq.pop();
if (temp>sf[node]) continue; //important
for (auto &it:al[node]){
if (sf[it.v]>sf[node]+it.len){
sf[it.v]=sf[node]+it.len;
ps[it.v]=it.id;
pq.push(ii (sf[it.v],it.v));
}
}
}
sb[1]=0;
pq.push(ii(sb[1],1));
while (!pq.empty()){
temp=pq.top().first;
node=pq.top().second;
pq.pop();
if (temp>sb[node]) continue; //important
for (auto &it:al_rev[node]){
if (sb[it.v]>sb[node]+it.len){
sb[it.v]=sb[node]+it.len;
pq.push(ii (sb[it.v],it.v));
}
}
}
tf[n]=0;
pq.push(ii(tf[n],n));
while (!pq.empty()){
temp=pq.top().first;
node=pq.top().second;
pq.pop();
if (temp>tf[node]) continue; //important
for (auto &it:al[node]){
if (tf[it.v]>tf[node]+it.len){
tf[it.v]=tf[node]+it.len;
pt[it.v]=it.id;
pq.push(ii (tf[it.v],it.v));
}
}
}
tb[n]=0;
pq.push(ii(tb[n],n));
while (!pq.empty()){
temp=pq.top().first;
node=pq.top().second;
pq.pop();
if (temp>tb[node]) continue; //important
for (auto &it:al_rev[node]){
if (tb[it.v]>tb[node]+it.len){
tb[it.v]=tb[node]+it.len;
pq.push(ii (tb[it.v],it.v));
}
}
}
}
void SP_d(){
if (sf[n]!=INF){ //find best from S to T
int curr=n;
while (curr!=1){
//debug(ps[curr]);
sp_f[ps[curr]]=INF;
curr=edges[ps[curr]].u;
}
for (auto &it2:sp_f){
//we run a new iteration of dijkstra for this where we flip every edge on the SP
//cout<<edges[bad].u<<" "<<edges[bad].v<<endl;
rep(x,0,205) w[x]=INF;
w[1]=0;
pq.push(ii(w[1],1));
while (!pq.empty()){
temp=pq.top().first;
node=pq.top().second;
pq.pop();
if (temp>w[node]) continue; //important
for (auto &it:al[node]){
if (it.id==it2.first) continue;
if (w[it.v]>w[node]+it.len){
w[it.v]=w[node]+it.len;
pq.push(ii (w[it.v],it.v));
}
}
}
it2.second=w[n];
}
}
//find the path on best line from T->S
if (tf[1]!=INF){
int curr=1;
while (curr!=n){
//debug(pt[curr]);
sp_b[pt[curr]]=INF;
curr=edges[pt[curr]].u;
}
for (auto &it2:sp_b) {
//we run a new iteration of dijkstra for this where we flip every edge on the SP
//cout<<edges[bad].u<<" "<<edges[bad].v<<endl;
rep(x,0,205) w[x]=INF;
w[n]=0;
pq.push(ii(w[n],n));
while (!pq.empty()){
temp=pq.top().first;
node=pq.top().second;
pq.pop();
if (temp>w[node]) continue; //important
for (auto &it:al[node]){
if (it.id==it2.first) continue;
if (w[it.v]>w[node]+it.len){
w[it.v]=w[node]+it.len;
pq.push(ii (w[it.v],it.v));
}
}
}
it2.second=w[1];
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
int a,b,c,d;
rep(x,0,m){
cin>>a>>b>>c>>d;
al[a].push_back(E(a,b,c,d,x));
al_rev[b].push_back(E(b,a,c,d,x));
edges.push_back(E(a,b,c,d,x));
}
initial_d();
SP_d();
//try dijk start and end
ll ans=sf[n]+tf[1];
for (auto &it:edges){
ans=min(ans,(sp_f.count(it.id)?sp_f[it.id]:min(sf[n],sf[it.v]+tb[it.u]+it.len))+(sp_b.count(it.id)?sp_b[it.id]:min(tf[1],tf[it.v]+sb[it.u]+it.len))+it.flip);
//debug(min((sp_f.count(it.id)?sp_f[it.id]:sf[n]),sf[it.v]+tb[it.u]+it.len));
//debug(min((sp_b.count(it.id)?sp_b[it.id]:tf[1]),tf[it.v]+sb[it.u]+it.len));
}
if (ans>=INF) cout<<-1<<endl;
else cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
20 ms |
512 KB |
Output is correct |
11 |
Correct |
20 ms |
512 KB |
Output is correct |
12 |
Correct |
20 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
7144 KB |
Output is correct |
2 |
Correct |
33 ms |
7012 KB |
Output is correct |
3 |
Correct |
33 ms |
6896 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
31 ms |
7024 KB |
Output is correct |
10 |
Correct |
33 ms |
7016 KB |
Output is correct |
11 |
Correct |
35 ms |
6768 KB |
Output is correct |
12 |
Correct |
33 ms |
7016 KB |
Output is correct |
13 |
Correct |
33 ms |
6760 KB |
Output is correct |
14 |
Correct |
34 ms |
7528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
24 ms |
6132 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
30 ms |
6760 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
30 ms |
7408 KB |
Output is correct |
9 |
Correct |
28 ms |
7792 KB |
Output is correct |
10 |
Correct |
29 ms |
7408 KB |
Output is correct |
11 |
Correct |
31 ms |
7524 KB |
Output is correct |
12 |
Correct |
30 ms |
7656 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
28 ms |
7792 KB |
Output is correct |
20 |
Correct |
29 ms |
7536 KB |
Output is correct |
21 |
Correct |
29 ms |
7656 KB |
Output is correct |
22 |
Correct |
29 ms |
7400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
20 ms |
512 KB |
Output is correct |
11 |
Correct |
20 ms |
512 KB |
Output is correct |
12 |
Correct |
20 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
6 ms |
512 KB |
Output is correct |
17 |
Correct |
33 ms |
7144 KB |
Output is correct |
18 |
Correct |
33 ms |
7012 KB |
Output is correct |
19 |
Correct |
33 ms |
6896 KB |
Output is correct |
20 |
Correct |
7 ms |
640 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
31 ms |
7024 KB |
Output is correct |
26 |
Correct |
33 ms |
7016 KB |
Output is correct |
27 |
Correct |
35 ms |
6768 KB |
Output is correct |
28 |
Correct |
33 ms |
7016 KB |
Output is correct |
29 |
Correct |
33 ms |
6760 KB |
Output is correct |
30 |
Correct |
34 ms |
7528 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
24 ms |
6132 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
30 ms |
6760 KB |
Output is correct |
36 |
Correct |
4 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
30 ms |
7408 KB |
Output is correct |
39 |
Correct |
28 ms |
7792 KB |
Output is correct |
40 |
Correct |
29 ms |
7408 KB |
Output is correct |
41 |
Correct |
31 ms |
7524 KB |
Output is correct |
42 |
Correct |
30 ms |
7656 KB |
Output is correct |
43 |
Correct |
5 ms |
384 KB |
Output is correct |
44 |
Correct |
4 ms |
384 KB |
Output is correct |
45 |
Correct |
4 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
384 KB |
Output is correct |
47 |
Correct |
4 ms |
384 KB |
Output is correct |
48 |
Correct |
4 ms |
384 KB |
Output is correct |
49 |
Correct |
28 ms |
7792 KB |
Output is correct |
50 |
Correct |
29 ms |
7536 KB |
Output is correct |
51 |
Correct |
29 ms |
7656 KB |
Output is correct |
52 |
Correct |
29 ms |
7400 KB |
Output is correct |
53 |
Correct |
35 ms |
7536 KB |
Output is correct |
54 |
Correct |
35 ms |
7536 KB |
Output is correct |
55 |
Correct |
37 ms |
7544 KB |
Output is correct |
56 |
Correct |
6 ms |
512 KB |
Output is correct |
57 |
Correct |
6 ms |
512 KB |
Output is correct |
58 |
Correct |
91 ms |
6772 KB |
Output is correct |
59 |
Correct |
94 ms |
6772 KB |
Output is correct |
60 |
Correct |
97 ms |
6772 KB |
Output is correct |
61 |
Correct |
78 ms |
6900 KB |
Output is correct |
62 |
Correct |
92 ms |
6772 KB |
Output is correct |
63 |
Correct |
102 ms |
6764 KB |
Output is correct |
64 |
Correct |
661 ms |
6388 KB |
Output is correct |
65 |
Correct |
640 ms |
6516 KB |
Output is correct |
66 |
Correct |
590 ms |
6516 KB |
Output is correct |
67 |
Correct |
23 ms |
5600 KB |
Output is correct |
68 |
Correct |
33 ms |
8040 KB |
Output is correct |
69 |
Correct |
32 ms |
8040 KB |
Output is correct |
70 |
Correct |
33 ms |
7656 KB |
Output is correct |
71 |
Correct |
36 ms |
7656 KB |
Output is correct |
72 |
Correct |
32 ms |
7908 KB |
Output is correct |
73 |
Correct |
35 ms |
7912 KB |
Output is correct |
74 |
Correct |
36 ms |
8040 KB |
Output is correct |