//記得跳題
#include<iostream>
#include<array>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
#include<unordered_map>
#include<cstring>
#include<iomanip>
#include<bitset>
#include<tuple>
#define ll long long
#define LL __int128_t
#define DB double
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define INF (ll)(1e9+2e8+5)
#define MOD (ll)(1e9+7)
#define F first
#define S second
#define endl "\n"
#define AC ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
template<class T> using PQ=priority_queue<T,vector<T>,greater<T> >;
void file(){
freopen("/Users/liaoyunyang/Desktop/meta_in.txt","r",stdin);
freopen("/Users/liaoyunyang/Desktop/meta_out.txt","w",stdout);
}
bool used[2][50005],vis[205];
int dis[2][2][205];
int a[50005],b[50005],c[50005],d[50005];
ll tmp[205];
vector<int> e[2][205];
signed main(){
AC;
int n,m; cin>>n>>m;
FOR(i,1,m+1){
cin>>a[i]>>b[i]>>c[i]>>d[i];
e[0][a[i]].pb(i);
e[0][b[i]].pb(i+m);
e[1][b[i]].pb(i);
}
FOR(x,0,2)FOR(y,0,2)FOR(i,0,n+1) dis[x][y][i]=INF;
PQ<pair<int,pair<int,int> > > pq;
pq.push({0,{1,0}});
while(pq.size()){
int t=pq.top().F,now=pq.top().S.F,id=pq.top().S.S; pq.pop();
if(dis[0][0][now]!=INF) continue;
dis[0][0][now]=t; used[0][id]=1;
for(auto i:e[0][now])if(i<=m){
pq.push({t+c[i],{b[i],i}});
}
}
pq.push({0,{n,0}});
while(pq.size()){
int t=pq.top().F,now=pq.top().S.F; pq.pop();
if(dis[0][1][now]!=INF) continue;
dis[0][1][now]=t;
for(auto i:e[1][now])if(i<=m){
pq.push({t+c[i],{a[i],i}});
}
}
pq.push({0,{n,0}});
while(pq.size()){
int t=pq.top().F,now=pq.top().S.F,id=pq.top().S.S; pq.pop();
if(dis[1][0][now]!=INF) continue;
dis[1][0][now]=t; used[1][id]=1;
for(auto i:e[0][now])if(i<=m){
pq.push({t+c[i],{b[i],i}});
}
}
pq.push({0,{1,0}});
while(pq.size()){
int t=pq.top().F,now=pq.top().S.F; pq.pop();
if(dis[1][1][now]!=INF) continue;
dis[1][1][now]=t;
for(auto i:e[1][now])if(i<=m){
pq.push({t+c[i],{a[i],i}});
}
}
ll ans=(ll)dis[0][0][n]+dis[1][0][1];
FOR(i,1,m+1){
//cout<<"ans="<<ans<<endl;
ll d1=INF,d2=INF;
if(used[0][i]){
FOR(j,0,n+2){
tmp[j]=INF; vis[j]=0;
}
int now=n+1;
tmp[1]=0;
FOR(x,0,n){
now=n+1;
FOR(y,1,n+1)if(!vis[y]&&tmp[y]<tmp[now]){
now=y;
}
if(now==n+1) break;
if(now==n) break;
vis[now]=1;
for(auto j:e[0][now])if(j!=i){
if(j<=m) tmp[b[j]]=min(tmp[b[j]],tmp[now]+c[j]);
else if(j==i+m) tmp[a[i]]=min(tmp[a[i]],tmp[now]+c[i]);
}
}
d1=tmp[n];
}
else{
//cout<<"hi"<<endl;
d1=min((ll)dis[0][0][n],(ll)dis[0][0][b[i]]+dis[0][1][a[i]]+c[i]);
}
if(used[1][i]){
FOR(j,0,n+2){
tmp[j]=INF; vis[j]=0;
}
int now=n+1;
tmp[n]=0;
FOR(x,0,n){
now=n+1;
FOR(y,1,n+1)if(!vis[y]&&tmp[y]<tmp[now]){
now=y;
}
if(now==n+1) break;
if(now==1) break;
vis[now]=1;
for(auto j:e[0][now])if(j!=i){
if(j<=m) tmp[b[j]]=min(tmp[b[j]],tmp[now]+c[j]);
else if(j==i+m) tmp[a[i]]=min(tmp[a[i]],tmp[now]+c[i]);
}
}
d2=tmp[1];
}
else{
//cout<<"i="<<i<<endl;
d2=min((ll)dis[1][0][1],(ll)dis[1][0][b[i]]+dis[1][1][a[i]]+c[i]);
}
//cout<<"d1="<<d1<<" d2="<<d2<<endl;
ans=min((ll)ans,((ll)d1+(ll)d2+(ll)d[i]));
}
if(ans>=INF) cout<<"-1"<<endl;
else cout<<ans<<endl;
}
/*
4 5
1 2 4 4
1 3 2 1
4 3 1 2
4 1 6 1
2 4 2 5
*/
Compilation message
ho_t4.cpp: In function 'void file()':
ho_t4.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen("/Users/liaoyunyang/Desktop/meta_in.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen("/Users/liaoyunyang/Desktop/meta_out.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
356 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
28 ms |
396 KB |
Output is correct |
4 |
Correct |
31 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
43 ms |
352 KB |
Output is correct |
11 |
Correct |
41 ms |
340 KB |
Output is correct |
12 |
Correct |
41 ms |
416 KB |
Output is correct |
13 |
Correct |
13 ms |
400 KB |
Output is correct |
14 |
Correct |
27 ms |
396 KB |
Output is correct |
15 |
Correct |
20 ms |
340 KB |
Output is correct |
16 |
Correct |
27 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
178 ms |
3148 KB |
Output is correct |
2 |
Correct |
199 ms |
3148 KB |
Output is correct |
3 |
Correct |
144 ms |
3028 KB |
Output is correct |
4 |
Correct |
11 ms |
524 KB |
Output is correct |
5 |
Correct |
18 ms |
440 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
87 ms |
2804 KB |
Output is correct |
10 |
Correct |
82 ms |
2696 KB |
Output is correct |
11 |
Correct |
163 ms |
3124 KB |
Output is correct |
12 |
Correct |
148 ms |
3156 KB |
Output is correct |
13 |
Correct |
188 ms |
3144 KB |
Output is correct |
14 |
Correct |
160 ms |
4300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
173 ms |
2068 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
238 ms |
2448 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
105 ms |
2420 KB |
Output is correct |
9 |
Correct |
121 ms |
2440 KB |
Output is correct |
10 |
Correct |
267 ms |
2500 KB |
Output is correct |
11 |
Correct |
119 ms |
2516 KB |
Output is correct |
12 |
Correct |
107 ms |
2644 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
266 ms |
2608 KB |
Output is correct |
20 |
Correct |
138 ms |
2468 KB |
Output is correct |
21 |
Correct |
104 ms |
3276 KB |
Output is correct |
22 |
Correct |
134 ms |
3272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
356 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
28 ms |
396 KB |
Output is correct |
4 |
Correct |
31 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
43 ms |
352 KB |
Output is correct |
11 |
Correct |
41 ms |
340 KB |
Output is correct |
12 |
Correct |
41 ms |
416 KB |
Output is correct |
13 |
Correct |
13 ms |
400 KB |
Output is correct |
14 |
Correct |
27 ms |
396 KB |
Output is correct |
15 |
Correct |
20 ms |
340 KB |
Output is correct |
16 |
Correct |
27 ms |
340 KB |
Output is correct |
17 |
Correct |
178 ms |
3148 KB |
Output is correct |
18 |
Correct |
199 ms |
3148 KB |
Output is correct |
19 |
Correct |
144 ms |
3028 KB |
Output is correct |
20 |
Correct |
11 ms |
524 KB |
Output is correct |
21 |
Correct |
18 ms |
440 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
87 ms |
2804 KB |
Output is correct |
26 |
Correct |
82 ms |
2696 KB |
Output is correct |
27 |
Correct |
163 ms |
3124 KB |
Output is correct |
28 |
Correct |
148 ms |
3156 KB |
Output is correct |
29 |
Correct |
188 ms |
3144 KB |
Output is correct |
30 |
Correct |
160 ms |
4300 KB |
Output is correct |
31 |
Correct |
39 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
173 ms |
2068 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
238 ms |
2448 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
105 ms |
2420 KB |
Output is correct |
39 |
Correct |
121 ms |
2440 KB |
Output is correct |
40 |
Correct |
267 ms |
2500 KB |
Output is correct |
41 |
Correct |
119 ms |
2516 KB |
Output is correct |
42 |
Correct |
107 ms |
2644 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
266 ms |
2608 KB |
Output is correct |
50 |
Correct |
138 ms |
2468 KB |
Output is correct |
51 |
Correct |
104 ms |
3276 KB |
Output is correct |
52 |
Correct |
134 ms |
3272 KB |
Output is correct |
53 |
Correct |
234 ms |
3916 KB |
Output is correct |
54 |
Correct |
247 ms |
3916 KB |
Output is correct |
55 |
Correct |
139 ms |
3924 KB |
Output is correct |
56 |
Correct |
28 ms |
340 KB |
Output is correct |
57 |
Correct |
15 ms |
460 KB |
Output is correct |
58 |
Correct |
331 ms |
2988 KB |
Output is correct |
59 |
Correct |
322 ms |
3016 KB |
Output is correct |
60 |
Correct |
320 ms |
3040 KB |
Output is correct |
61 |
Correct |
325 ms |
3036 KB |
Output is correct |
62 |
Correct |
322 ms |
3016 KB |
Output is correct |
63 |
Correct |
327 ms |
2988 KB |
Output is correct |
64 |
Correct |
273 ms |
3104 KB |
Output is correct |
65 |
Correct |
258 ms |
3104 KB |
Output is correct |
66 |
Correct |
237 ms |
3112 KB |
Output is correct |
67 |
Correct |
24 ms |
3144 KB |
Output is correct |
68 |
Correct |
94 ms |
3628 KB |
Output is correct |
69 |
Correct |
89 ms |
3732 KB |
Output is correct |
70 |
Correct |
250 ms |
4048 KB |
Output is correct |
71 |
Correct |
251 ms |
4048 KB |
Output is correct |
72 |
Correct |
185 ms |
4196 KB |
Output is correct |
73 |
Correct |
266 ms |
4176 KB |
Output is correct |
74 |
Correct |
219 ms |
4176 KB |
Output is correct |