#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct edge{
int a,b,c,d;
};
const int mxn=205;
vector<tuple<int,int,int>> adj[2][mxn];
int n,m;
ll dist[5][mxn];
vector<int> from[4][mxn];
void dijk(int type,int rot,int st,int en){
dist[type][st]=0;
priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
pq.push({0,st});
while(!pq.empty()){
auto tmp=pq.top();
pq.pop();
ll len=tmp.f;
int v=tmp.s;
if(len!=dist[type][v]) continue;
for(auto u:adj[rot][v]){
int nxt,w,id;
tie(nxt,w,id)=u;
if(dist[type][v]+1ll*w<dist[type][nxt]){
dist[type][nxt]=dist[type][v]+1ll*w;
from[type][nxt]=from[type][v];
from[type][nxt].push_back(id);
pq.push({dist[type][nxt],nxt});
}
}
}
sort(all(from[type][en]));
}
ll dijk2(int type,int rot,int st,int en,int ban){
for(int j=1;j<=n;j++){
dist[type][j]=0x3f3f3f3f3f3f3f3f;
}
dist[type][st]=0;
priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
pq.push({0,st});
while(!pq.empty()){
auto tmp=pq.top();
pq.pop();
ll len=tmp.f;
int v=tmp.s;
if(len!=dist[type][v]) continue;
for(auto u:adj[rot][v]){
int nxt,w,id;
tie(nxt,w,id)=u;
if(id==ban) continue;
if(dist[type][v]+1ll*w<dist[type][nxt]){
dist[type][nxt]=dist[type][v]+1ll*w;
pq.push({dist[type][nxt],nxt});
}
}
}
return dist[type][en];
}
int main() {_
//setIO("wayne");
cin>>n>>m;
edge E[m];
for(int i=0;i<m;i++){
int a,b,c,d;
cin>>a>>b>>c>>d;
E[i]={a,b,c,d};
adj[0][a].push_back({b,c,i});
adj[1][b].push_back({a,c,i});
}
memset(dist,0x3f3f3f3f,sizeof(dist));
dijk(0,0,1,n);
dijk(1,1,1,n);
dijk(2,0,n,1);
dijk(3,1,n,1);
ll ans=dist[0][n]+dist[2][1];
for(int i=0;i<m;i++){
ll a,b,c,d;
a=E[i].a,b=E[i].b,c=E[i].c,d=E[i].d;
ll ans1;
ll ans2;
{
if(upper_bound(all(from[0][n]),i)-lower_bound(all(from[0][n]),i)>=1){
ans1=dijk2(4,0,1,n,i);
}
else{
ans1=min(dist[0][b]+c+dist[3][a],dist[0][n]);
}
}
{
if(upper_bound(all(from[2][1]),i)-lower_bound(all(from[2][1]),i)>=1){
ans2=dijk2(4,0,n,1,i);
}
else{
ans2=min(dist[2][b]+c+dist[1][a],dist[2][1]);
}
}
ans=min(ans,ans1+ans2+d);
}
cout<<(ans>=0x3f3f3f3f3f3f3f3f?-1:ans);
return 0;
}
//maybe its multiset not set
Compilation message
ho_t4.cpp: In function 'void setIO(std::string)':
ho_t4.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
13 ms |
1040 KB |
Output is correct |
11 |
Correct |
13 ms |
1028 KB |
Output is correct |
12 |
Correct |
14 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
3028 KB |
Output is correct |
2 |
Correct |
19 ms |
3092 KB |
Output is correct |
3 |
Correct |
19 ms |
3064 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
17 ms |
3008 KB |
Output is correct |
10 |
Correct |
18 ms |
3028 KB |
Output is correct |
11 |
Correct |
19 ms |
3100 KB |
Output is correct |
12 |
Correct |
21 ms |
3028 KB |
Output is correct |
13 |
Correct |
18 ms |
3036 KB |
Output is correct |
14 |
Correct |
19 ms |
3320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
13 ms |
2340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
18 ms |
3060 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
16 ms |
3028 KB |
Output is correct |
9 |
Correct |
17 ms |
3064 KB |
Output is correct |
10 |
Correct |
16 ms |
3028 KB |
Output is correct |
11 |
Correct |
16 ms |
3156 KB |
Output is correct |
12 |
Correct |
17 ms |
3100 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
18 ms |
3140 KB |
Output is correct |
20 |
Correct |
22 ms |
3064 KB |
Output is correct |
21 |
Correct |
18 ms |
3028 KB |
Output is correct |
22 |
Correct |
17 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
13 ms |
1040 KB |
Output is correct |
11 |
Correct |
13 ms |
1028 KB |
Output is correct |
12 |
Correct |
14 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
18 ms |
3028 KB |
Output is correct |
18 |
Correct |
19 ms |
3092 KB |
Output is correct |
19 |
Correct |
19 ms |
3064 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
17 ms |
3008 KB |
Output is correct |
26 |
Correct |
18 ms |
3028 KB |
Output is correct |
27 |
Correct |
19 ms |
3100 KB |
Output is correct |
28 |
Correct |
21 ms |
3028 KB |
Output is correct |
29 |
Correct |
18 ms |
3036 KB |
Output is correct |
30 |
Correct |
19 ms |
3320 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
13 ms |
2340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
18 ms |
3060 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
16 ms |
3028 KB |
Output is correct |
39 |
Correct |
17 ms |
3064 KB |
Output is correct |
40 |
Correct |
16 ms |
3028 KB |
Output is correct |
41 |
Correct |
16 ms |
3156 KB |
Output is correct |
42 |
Correct |
17 ms |
3100 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
18 ms |
3140 KB |
Output is correct |
50 |
Correct |
22 ms |
3064 KB |
Output is correct |
51 |
Correct |
18 ms |
3028 KB |
Output is correct |
52 |
Correct |
17 ms |
3028 KB |
Output is correct |
53 |
Correct |
18 ms |
3028 KB |
Output is correct |
54 |
Correct |
20 ms |
3028 KB |
Output is correct |
55 |
Correct |
20 ms |
3016 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
67 ms |
2852 KB |
Output is correct |
59 |
Correct |
68 ms |
2772 KB |
Output is correct |
60 |
Correct |
70 ms |
2840 KB |
Output is correct |
61 |
Correct |
52 ms |
2780 KB |
Output is correct |
62 |
Correct |
66 ms |
2856 KB |
Output is correct |
63 |
Correct |
69 ms |
2832 KB |
Output is correct |
64 |
Correct |
552 ms |
3364 KB |
Output is correct |
65 |
Correct |
529 ms |
4376 KB |
Output is correct |
66 |
Correct |
490 ms |
4196 KB |
Output is correct |
67 |
Correct |
16 ms |
3024 KB |
Output is correct |
68 |
Correct |
20 ms |
4128 KB |
Output is correct |
69 |
Correct |
19 ms |
4144 KB |
Output is correct |
70 |
Correct |
22 ms |
4148 KB |
Output is correct |
71 |
Correct |
21 ms |
4188 KB |
Output is correct |
72 |
Correct |
19 ms |
4208 KB |
Output is correct |
73 |
Correct |
20 ms |
4296 KB |
Output is correct |
74 |
Correct |
20 ms |
4180 KB |
Output is correct |