#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N=5e4+5;
vector <pair <pair <int,int> ,int> > g[205];
int dist[205][205];
int a[N],b[N],c[N],d[N];
priority_queue <pair <int,int> > pq;
/*void djkstra(int n,int tp,int x){
dis[x][n]=0;
pq.push({0,n});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
for(auto to : g[tp][v]){
if(dis[x][to.ff.ff]>dis[x][v]+to.ff.ss){
dis[x][to.ff.ff]=dis[x][v]+to.ff.ss;
p[x][to.ff.ff]=to.ss;
pq.push({dis[x][to.ff.ff],to.ff.ff});
}
}
}
}*/
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=2e9;
}
dist[i][i]=0;
}
for(int i=1;i<=m;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i];
dist[a[i]][b[i]]=min(dist[a[i]][b[i]],c[i]);
g[a[i]].pb({{b[i],c[i]},i});
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
int mn=2e9;
mn=min(dist[1][n]+dist[n][1],mn);
for(int i=1;i<=m;i++){
int new1n=min(dist[1][n],dist[1][b[i]]+dist[a[i]][n]+c[i]);
int newn1=min(dist[n][1],dist[n][b[i]]+dist[a[i]][1]+c[i]);
if(new1n+newn1+d[i]>=mn)continue;
vector <int> dis(n+1,2e9);
dis[1]=0;
pq.push({0,1});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
if(v==b[i]){
if(dis[a[i]]>dis[b[i]]+c[i]){
dis[a[i]]=dis[b[i]]+c[i];
pq.push({dis[a[i]],a[i]});
}
}
for(auto to : g[v]){
if(to.ss==i)continue;
if(dis[to.ff.ff]>dis[v]+to.ff.ss){
dis[to.ff.ff]=dis[v]+to.ff.ss;
pq.push({dis[to.ff.ff],to.ff.ff});
}
}
}
if(dis[n]!=2e9){
int ans=dis[n];
for(int j=1;j<=n;j++){
dis[j]=2e9;
}
dis[n]=0;
pq.push({0,n});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
if(v==b[i]){
if(dis[a[i]]>dis[b[i]]+c[i]){
dis[a[i]]=dis[b[i]]+c[i];
pq.push({dis[a[i]],a[i]});
}
}
for(auto to : g[v]){
if(to.ss==i)continue;
if(dis[to.ff.ff]>dis[v]+to.ff.ss){
dis[to.ff.ff]=dis[v]+to.ff.ss;
pq.push({dis[to.ff.ff],to.ff.ff});
}
}
}
mn=min(mn,ans+dis[1]+d[i]);
}
}
if(mn==2e9)mn=-1;
cout<<mn<<"\n";
}
//g[1]-r
//dis[0] 1
//dis[1] 1r
//dis[2] n
//dis[3] nr
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
856 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
11 ms |
896 KB |
Output is correct |
4 |
Correct |
8 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
484 KB |
Output is correct |
6 |
Correct |
8 ms |
600 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 |
74 ms |
892 KB |
Output is correct |
11 |
Correct |
320 ms |
888 KB |
Output is correct |
12 |
Correct |
19 ms |
860 KB |
Output is correct |
13 |
Correct |
10 ms |
900 KB |
Output is correct |
14 |
Correct |
8 ms |
892 KB |
Output is correct |
15 |
Correct |
10 ms |
860 KB |
Output is correct |
16 |
Correct |
10 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
4956 KB |
Output is correct |
2 |
Correct |
24 ms |
5212 KB |
Output is correct |
3 |
Correct |
20 ms |
5208 KB |
Output is correct |
4 |
Correct |
11 ms |
856 KB |
Output is correct |
5 |
Correct |
10 ms |
860 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
8 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
64 ms |
5288 KB |
Output is correct |
10 |
Correct |
37 ms |
5212 KB |
Output is correct |
11 |
Correct |
25 ms |
5208 KB |
Output is correct |
12 |
Correct |
22 ms |
5464 KB |
Output is correct |
13 |
Correct |
22 ms |
5464 KB |
Output is correct |
14 |
Correct |
25 ms |
5724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
860 KB |
Output is correct |
2 |
Correct |
8 ms |
820 KB |
Output is correct |
3 |
Correct |
16 ms |
3920 KB |
Output is correct |
4 |
Correct |
8 ms |
604 KB |
Output is correct |
5 |
Correct |
20 ms |
4956 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
21 ms |
4864 KB |
Output is correct |
9 |
Correct |
25 ms |
4956 KB |
Output is correct |
10 |
Correct |
19 ms |
4952 KB |
Output is correct |
11 |
Correct |
19 ms |
4696 KB |
Output is correct |
12 |
Correct |
19 ms |
4956 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
19 ms |
4980 KB |
Output is correct |
20 |
Correct |
25 ms |
4952 KB |
Output is correct |
21 |
Correct |
19 ms |
4824 KB |
Output is correct |
22 |
Correct |
19 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
856 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
11 ms |
896 KB |
Output is correct |
4 |
Correct |
8 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
484 KB |
Output is correct |
6 |
Correct |
8 ms |
600 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 |
74 ms |
892 KB |
Output is correct |
11 |
Correct |
320 ms |
888 KB |
Output is correct |
12 |
Correct |
19 ms |
860 KB |
Output is correct |
13 |
Correct |
10 ms |
900 KB |
Output is correct |
14 |
Correct |
8 ms |
892 KB |
Output is correct |
15 |
Correct |
10 ms |
860 KB |
Output is correct |
16 |
Correct |
10 ms |
860 KB |
Output is correct |
17 |
Correct |
19 ms |
4956 KB |
Output is correct |
18 |
Correct |
24 ms |
5212 KB |
Output is correct |
19 |
Correct |
20 ms |
5208 KB |
Output is correct |
20 |
Correct |
11 ms |
856 KB |
Output is correct |
21 |
Correct |
10 ms |
860 KB |
Output is correct |
22 |
Correct |
8 ms |
604 KB |
Output is correct |
23 |
Correct |
8 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
64 ms |
5288 KB |
Output is correct |
26 |
Correct |
37 ms |
5212 KB |
Output is correct |
27 |
Correct |
25 ms |
5208 KB |
Output is correct |
28 |
Correct |
22 ms |
5464 KB |
Output is correct |
29 |
Correct |
22 ms |
5464 KB |
Output is correct |
30 |
Correct |
25 ms |
5724 KB |
Output is correct |
31 |
Correct |
8 ms |
860 KB |
Output is correct |
32 |
Correct |
8 ms |
820 KB |
Output is correct |
33 |
Correct |
16 ms |
3920 KB |
Output is correct |
34 |
Correct |
8 ms |
604 KB |
Output is correct |
35 |
Correct |
20 ms |
4956 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
21 ms |
4864 KB |
Output is correct |
39 |
Correct |
25 ms |
4956 KB |
Output is correct |
40 |
Correct |
19 ms |
4952 KB |
Output is correct |
41 |
Correct |
19 ms |
4696 KB |
Output is correct |
42 |
Correct |
19 ms |
4956 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
19 ms |
4980 KB |
Output is correct |
50 |
Correct |
25 ms |
4952 KB |
Output is correct |
51 |
Correct |
19 ms |
4824 KB |
Output is correct |
52 |
Correct |
19 ms |
4696 KB |
Output is correct |
53 |
Correct |
19 ms |
4952 KB |
Output is correct |
54 |
Correct |
19 ms |
5008 KB |
Output is correct |
55 |
Correct |
19 ms |
5212 KB |
Output is correct |
56 |
Correct |
8 ms |
860 KB |
Output is correct |
57 |
Correct |
8 ms |
860 KB |
Output is correct |
58 |
Correct |
24 ms |
4248 KB |
Output is correct |
59 |
Correct |
30 ms |
4184 KB |
Output is correct |
60 |
Correct |
38 ms |
4188 KB |
Output is correct |
61 |
Correct |
28 ms |
4120 KB |
Output is correct |
62 |
Correct |
205 ms |
4184 KB |
Output is correct |
63 |
Correct |
355 ms |
4188 KB |
Output is correct |
64 |
Correct |
187 ms |
4140 KB |
Output is correct |
65 |
Correct |
689 ms |
4404 KB |
Output is correct |
66 |
Correct |
784 ms |
4404 KB |
Output is correct |
67 |
Correct |
10 ms |
3796 KB |
Output is correct |
68 |
Correct |
38 ms |
5452 KB |
Output is correct |
69 |
Correct |
41 ms |
5448 KB |
Output is correct |
70 |
Correct |
27 ms |
5464 KB |
Output is correct |
71 |
Correct |
23 ms |
5212 KB |
Output is correct |
72 |
Correct |
21 ms |
5212 KB |
Output is correct |
73 |
Correct |
21 ms |
5336 KB |
Output is correct |
74 |
Correct |
24 ms |
5212 KB |
Output is correct |