#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
const long long inf=1e18;
struct edg{
int u,v;
long long c,d;
}b[50009];
#define pil pair<int,long long>
vector<pil>a[209];
struct cus{
int u;
long long val;
bool operator < (const cus &p)const {
return val>p.val;
}
};
int p1[209],p2[209],rp1[209],rp2[209];
bool use[50009];
long long d[209][209];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int n,m;
cin>>n>>m;
for1(i,1,m)cin>>b[i].u>>b[i].v>>b[i].c>>b[i].d;
for1(i,1,n){
for1(j,1,n){
d[i][j]=inf;
}
d[i][i]=0;
}
for1(i,1,m){
d[b[i].u][b[i].v]=min(d[b[i].u][b[i].v],b[i].c+1);
}
for1(i,1,n){
for1(j,1,n){
for1(k,1,n){
d[j][k]=min(d[j][k],d[j][i]+d[i][k]);
}
}
}
auto dijkstra=[&](int s,int t){
vector<bool>vis(n+1);
vector<long long>d1(n+1);
for1(i,1,n)d1[i]=inf;
d1[s]=0;
priority_queue<cus>t1;
t1.push({s,0});
while (!t1.empty()){
auto u=t1.top();
t1.pop();
if (vis[u.u])continue;
vis[u.u]=1;
for (auto v:a[u.u]){
long long val=u.val+v.se;
if (val<d1[v.fi]){
d1[v.fi]=val;
t1.push({v.fi,d1[v.fi]});
}
}
}
return d1[t];
};
for1(i,1,n)a[i].clear();
for1(i,1,m){
if (d[1][b[i].u]+b[i].c+1==d[1][b[i].v]&&!p1[b[i].v]){
p1[b[i].v]=b[i].u;
use[i]=1;
}
if (d[n][b[i].u]+b[i].c+1==d[n][b[i].v]&&!p2[b[i].v]){
p2[b[i].v]=b[i].u;
use[i]=1;
}
if (d[b[i].v][n]+b[i].c+1==d[b[i].u][n]&&!rp1[b[i].u]){
rp1[b[i].u]=b[i].v;
use[i]=1;
}
if (d[b[i].v][1]+b[i].c+1==d[b[i].u][1]&&!rp2[b[i].u]){
rp2[b[i].u]=b[i].v;
use[i]=1;
}
}
for1(i,1,n){
for1(j,1,n){
d[i][j]=inf;
}
d[i][i]=0;
}
for1(i,1,m){
d[b[i].u][b[i].v]=min(d[b[i].u][b[i].v],b[i].c);
}
for1(i,1,n){
for1(j,1,n){
for1(k,1,n){
d[j][k]=min(d[j][k],d[j][i]+d[i][k]);
}
}
}
long long ans=d[1][n]+d[n][1];
auto invert=[&](int id){
if (use[id]){
for1(i,1,n)a[i].clear();
swap(b[id].u,b[id].v);
for1(i,1,m){
a[b[i].u].pb({b[i].v,b[i].c});
}
swap(b[id].u,b[id].v);
return b[id].d+dijkstra(1,n)+dijkstra(n,1);
}
return b[id].d+min(d[1][n],d[1][b[id].v]+b[id].c+d[b[id].u][n])+min(d[n][1],d[n][b[id].v]+b[id].c+d[b[id].u][1]);
};
for1(i,1,m){
ans=min(ans,invert(i));
}
if (ans>=inf)ans=-1;
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
856 KB |
Output is correct |
2 |
Correct |
14 ms |
604 KB |
Output is correct |
3 |
Correct |
30 ms |
856 KB |
Output is correct |
4 |
Correct |
32 ms |
872 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
17 ms |
820 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
26 ms |
860 KB |
Output is correct |
11 |
Correct |
29 ms |
852 KB |
Output is correct |
12 |
Correct |
28 ms |
860 KB |
Output is correct |
13 |
Correct |
19 ms |
856 KB |
Output is correct |
14 |
Correct |
25 ms |
860 KB |
Output is correct |
15 |
Correct |
24 ms |
748 KB |
Output is correct |
16 |
Correct |
25 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
310 ms |
4396 KB |
Output is correct |
2 |
Correct |
313 ms |
4240 KB |
Output is correct |
3 |
Correct |
312 ms |
4792 KB |
Output is correct |
4 |
Correct |
32 ms |
860 KB |
Output is correct |
5 |
Correct |
22 ms |
860 KB |
Output is correct |
6 |
Correct |
15 ms |
856 KB |
Output is correct |
7 |
Correct |
14 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
172 ms |
4504 KB |
Output is correct |
10 |
Correct |
167 ms |
4520 KB |
Output is correct |
11 |
Correct |
254 ms |
4508 KB |
Output is correct |
12 |
Correct |
251 ms |
4444 KB |
Output is correct |
13 |
Correct |
245 ms |
4688 KB |
Output is correct |
14 |
Correct |
235 ms |
4456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
848 KB |
Output is correct |
2 |
Correct |
15 ms |
604 KB |
Output is correct |
3 |
Correct |
312 ms |
3420 KB |
Output is correct |
4 |
Correct |
15 ms |
604 KB |
Output is correct |
5 |
Correct |
404 ms |
4248 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
476 KB |
Output is correct |
8 |
Correct |
201 ms |
4032 KB |
Output is correct |
9 |
Correct |
204 ms |
4212 KB |
Output is correct |
10 |
Correct |
307 ms |
4276 KB |
Output is correct |
11 |
Correct |
306 ms |
4244 KB |
Output is correct |
12 |
Correct |
311 ms |
4524 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
360 KB |
Output is correct |
19 |
Correct |
307 ms |
4372 KB |
Output is correct |
20 |
Correct |
310 ms |
4228 KB |
Output is correct |
21 |
Correct |
305 ms |
4180 KB |
Output is correct |
22 |
Correct |
306 ms |
4252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
856 KB |
Output is correct |
2 |
Correct |
14 ms |
604 KB |
Output is correct |
3 |
Correct |
30 ms |
856 KB |
Output is correct |
4 |
Correct |
32 ms |
872 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
17 ms |
820 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
26 ms |
860 KB |
Output is correct |
11 |
Correct |
29 ms |
852 KB |
Output is correct |
12 |
Correct |
28 ms |
860 KB |
Output is correct |
13 |
Correct |
19 ms |
856 KB |
Output is correct |
14 |
Correct |
25 ms |
860 KB |
Output is correct |
15 |
Correct |
24 ms |
748 KB |
Output is correct |
16 |
Correct |
25 ms |
860 KB |
Output is correct |
17 |
Correct |
310 ms |
4396 KB |
Output is correct |
18 |
Correct |
313 ms |
4240 KB |
Output is correct |
19 |
Correct |
312 ms |
4792 KB |
Output is correct |
20 |
Correct |
32 ms |
860 KB |
Output is correct |
21 |
Correct |
22 ms |
860 KB |
Output is correct |
22 |
Correct |
15 ms |
856 KB |
Output is correct |
23 |
Correct |
14 ms |
600 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
172 ms |
4504 KB |
Output is correct |
26 |
Correct |
167 ms |
4520 KB |
Output is correct |
27 |
Correct |
254 ms |
4508 KB |
Output is correct |
28 |
Correct |
251 ms |
4444 KB |
Output is correct |
29 |
Correct |
245 ms |
4688 KB |
Output is correct |
30 |
Correct |
235 ms |
4456 KB |
Output is correct |
31 |
Correct |
25 ms |
848 KB |
Output is correct |
32 |
Correct |
15 ms |
604 KB |
Output is correct |
33 |
Correct |
312 ms |
3420 KB |
Output is correct |
34 |
Correct |
15 ms |
604 KB |
Output is correct |
35 |
Correct |
404 ms |
4248 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
476 KB |
Output is correct |
38 |
Correct |
201 ms |
4032 KB |
Output is correct |
39 |
Correct |
204 ms |
4212 KB |
Output is correct |
40 |
Correct |
307 ms |
4276 KB |
Output is correct |
41 |
Correct |
306 ms |
4244 KB |
Output is correct |
42 |
Correct |
311 ms |
4524 KB |
Output is correct |
43 |
Correct |
0 ms |
344 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
360 KB |
Output is correct |
49 |
Correct |
307 ms |
4372 KB |
Output is correct |
50 |
Correct |
310 ms |
4228 KB |
Output is correct |
51 |
Correct |
305 ms |
4180 KB |
Output is correct |
52 |
Correct |
306 ms |
4252 KB |
Output is correct |
53 |
Correct |
312 ms |
4324 KB |
Output is correct |
54 |
Correct |
325 ms |
4532 KB |
Output is correct |
55 |
Correct |
299 ms |
4176 KB |
Output is correct |
56 |
Correct |
32 ms |
860 KB |
Output is correct |
57 |
Correct |
34 ms |
848 KB |
Output is correct |
58 |
Correct |
130 ms |
3556 KB |
Output is correct |
59 |
Correct |
153 ms |
3564 KB |
Output is correct |
60 |
Correct |
199 ms |
3408 KB |
Output is correct |
61 |
Correct |
135 ms |
3552 KB |
Output is correct |
62 |
Correct |
148 ms |
3568 KB |
Output is correct |
63 |
Correct |
197 ms |
3544 KB |
Output is correct |
64 |
Correct |
495 ms |
4120 KB |
Output is correct |
65 |
Correct |
530 ms |
4056 KB |
Output is correct |
66 |
Correct |
691 ms |
4068 KB |
Output is correct |
67 |
Correct |
11 ms |
3276 KB |
Output is correct |
68 |
Correct |
169 ms |
4432 KB |
Output is correct |
69 |
Correct |
169 ms |
4496 KB |
Output is correct |
70 |
Correct |
261 ms |
4528 KB |
Output is correct |
71 |
Correct |
255 ms |
4440 KB |
Output is correct |
72 |
Correct |
257 ms |
4500 KB |
Output is correct |
73 |
Correct |
256 ms |
4592 KB |
Output is correct |
74 |
Correct |
254 ms |
4688 KB |
Output is correct |