#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<map>
#include<set>
#include<algorithm>
#define ll long long
#define pi pair < ll,ll >
#define mp(a,b) make_pair(a,b)
#define rep(i,a,b) for(int i = a;i < b;i++)
#define N 204
#define M 50005
#define INF 1e15
using namespace std;
struct edge{
ll to;
ll cost;
ll reverse_cost;
ll id;
ll from;
bool operator <(const edge &a)const
{
return id < a.id;
}
bool operator ==(const edge &a)
{
return a.id == id;
}
};
ll n,m;
vector < edge > graph[N],inverse[N];
ll dist[N][N][4];
ll from_where[N][N][4];
bool is_interesting[M];
bool vis[N];
vector < edge > edges;
edge antestram;
void dijkstra(ll source,vector < edge > g[N],ll inv)
{
rep(i,0,n+1)
{
vis[i]=false;
dist[source][i][inv] = INF;
}
dist[source][source][inv] = 0;
rep(iterations,0,n)
{
ll cur = 0;
rep(i,1,n+1)
{
if(!vis[i] && dist[source][i][inv] < dist[source][cur][inv])
cur=i;
}
vis[cur] = true;
for(auto ed : g[cur])
{
ll v = ed.to;
ll cc = ed.cost;
ll ide = ed.id;
if(ide == antestram.id)
{
continue;
}
if(dist[source][v][inv] > dist[source][cur][inv] + cc)
{
from_where[source][v][inv] = ide;
dist[source][v][inv] = dist[source][cur][inv] + cc;
}
}
if(cur == antestram.from)
{
ll v = antestram.to;
ll cc = antestram.cost;
ll ide = antestram.id;
if(dist[source][v][inv] > dist[source][cur][inv] + cc)
{
from_where[source][v][inv] = ide;
dist[source][v][inv] = dist[source][cur][inv] + cc;
}
}
}
return;
}
void find_interesting(ll source,ll inv)
{
rep(i,0,n+1)
{
if(i == source)
continue;
is_interesting[from_where[source][i][inv]] = true;
}
return;
}
ll do_normal(ll id)
{
ll u = edges[id].from;
ll v = edges[id].to;
ll D = edges[id].reverse_cost;
ll c = edges[id].cost;
ll goton = min(dist[1][n][0],dist[1][v][0]+dist[n][u][1]+c);
ll goto1 = min(dist[n][1][0],dist[n][v][0]+dist[1][u][1]+c);
ll path = goton+goto1;
return path + D;
}
ll do_interesting(ll id)
{
antestram = edges[id];
antestram.from ^= antestram.to;
antestram.to ^= antestram.from;
antestram.from ^= antestram.to;
dijkstra(1,graph,2);
dijkstra(n,graph,2);
return dist[1][n][2]+dist[n][1][2]+antestram.reverse_cost;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
rep(i,0,m)
{
ll a,b,c,d;
cin >> a >> b >> c >> d;
edges.push_back({b,c,d,i,a});
graph[a].push_back({b,c,d,i});
inverse[b].push_back({a,c,d,i});
}
antestram.from = -1;
antestram.id = m+2;
dijkstra(1,graph,0);
find_interesting(1,0);
dijkstra(1,inverse,1);
find_interesting(1,1);
dijkstra(n,graph,0);
find_interesting(n,0);
dijkstra(n,inverse,1);
find_interesting(n,1);
ll ans = dist[1][n][0] + dist[n][1][0];
rep(i,0,m)
{
if(is_interesting[i])
ans=min(ans,do_interesting(i));
else
ans = min(ans,do_normal(i));
}
if(ans < INF)
cout << ans << "\n";
else
cout << -1 << "\n";
return 0;
}
/*
4 4
2 1 0 1
4 2 0 2
1 3 0 0
4 1 0 3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
760 KB |
Output is correct |
2 |
Correct |
33 ms |
512 KB |
Output is correct |
3 |
Correct |
140 ms |
760 KB |
Output is correct |
4 |
Correct |
139 ms |
648 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
44 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 |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
70 ms |
760 KB |
Output is correct |
11 |
Correct |
91 ms |
640 KB |
Output is correct |
12 |
Correct |
84 ms |
640 KB |
Output is correct |
13 |
Correct |
76 ms |
640 KB |
Output is correct |
14 |
Correct |
123 ms |
640 KB |
Output is correct |
15 |
Correct |
120 ms |
760 KB |
Output is correct |
16 |
Correct |
116 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
265 ms |
9428 KB |
Output is correct |
2 |
Correct |
258 ms |
9352 KB |
Output is correct |
3 |
Correct |
243 ms |
9484 KB |
Output is correct |
4 |
Correct |
131 ms |
768 KB |
Output is correct |
5 |
Correct |
106 ms |
640 KB |
Output is correct |
6 |
Correct |
61 ms |
512 KB |
Output is correct |
7 |
Correct |
16 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
144 ms |
9352 KB |
Output is correct |
10 |
Correct |
151 ms |
9480 KB |
Output is correct |
11 |
Correct |
213 ms |
9736 KB |
Output is correct |
12 |
Correct |
207 ms |
9476 KB |
Output is correct |
13 |
Correct |
195 ms |
9476 KB |
Output is correct |
14 |
Correct |
188 ms |
10116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
640 KB |
Output is correct |
2 |
Correct |
54 ms |
504 KB |
Output is correct |
3 |
Correct |
275 ms |
8084 KB |
Output is correct |
4 |
Correct |
49 ms |
384 KB |
Output is correct |
5 |
Correct |
287 ms |
9104 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
165 ms |
8976 KB |
Output is correct |
9 |
Correct |
172 ms |
8976 KB |
Output is correct |
10 |
Correct |
249 ms |
8968 KB |
Output is correct |
11 |
Correct |
237 ms |
9232 KB |
Output is correct |
12 |
Correct |
237 ms |
9476 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
244 ms |
9232 KB |
Output is correct |
20 |
Correct |
245 ms |
8968 KB |
Output is correct |
21 |
Correct |
244 ms |
9356 KB |
Output is correct |
22 |
Correct |
248 ms |
9224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
760 KB |
Output is correct |
2 |
Correct |
33 ms |
512 KB |
Output is correct |
3 |
Correct |
140 ms |
760 KB |
Output is correct |
4 |
Correct |
139 ms |
648 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
44 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 |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
70 ms |
760 KB |
Output is correct |
11 |
Correct |
91 ms |
640 KB |
Output is correct |
12 |
Correct |
84 ms |
640 KB |
Output is correct |
13 |
Correct |
76 ms |
640 KB |
Output is correct |
14 |
Correct |
123 ms |
640 KB |
Output is correct |
15 |
Correct |
120 ms |
760 KB |
Output is correct |
16 |
Correct |
116 ms |
640 KB |
Output is correct |
17 |
Correct |
265 ms |
9428 KB |
Output is correct |
18 |
Correct |
258 ms |
9352 KB |
Output is correct |
19 |
Correct |
243 ms |
9484 KB |
Output is correct |
20 |
Correct |
131 ms |
768 KB |
Output is correct |
21 |
Correct |
106 ms |
640 KB |
Output is correct |
22 |
Correct |
61 ms |
512 KB |
Output is correct |
23 |
Correct |
16 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
144 ms |
9352 KB |
Output is correct |
26 |
Correct |
151 ms |
9480 KB |
Output is correct |
27 |
Correct |
213 ms |
9736 KB |
Output is correct |
28 |
Correct |
207 ms |
9476 KB |
Output is correct |
29 |
Correct |
195 ms |
9476 KB |
Output is correct |
30 |
Correct |
188 ms |
10116 KB |
Output is correct |
31 |
Correct |
101 ms |
640 KB |
Output is correct |
32 |
Correct |
54 ms |
504 KB |
Output is correct |
33 |
Correct |
275 ms |
8084 KB |
Output is correct |
34 |
Correct |
49 ms |
384 KB |
Output is correct |
35 |
Correct |
287 ms |
9104 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
165 ms |
8976 KB |
Output is correct |
39 |
Correct |
172 ms |
8976 KB |
Output is correct |
40 |
Correct |
249 ms |
8968 KB |
Output is correct |
41 |
Correct |
237 ms |
9232 KB |
Output is correct |
42 |
Correct |
237 ms |
9476 KB |
Output is correct |
43 |
Correct |
5 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
5 ms |
384 KB |
Output is correct |
49 |
Correct |
244 ms |
9232 KB |
Output is correct |
50 |
Correct |
245 ms |
8968 KB |
Output is correct |
51 |
Correct |
244 ms |
9356 KB |
Output is correct |
52 |
Correct |
248 ms |
9224 KB |
Output is correct |
53 |
Correct |
239 ms |
8836 KB |
Output is correct |
54 |
Correct |
245 ms |
9104 KB |
Output is correct |
55 |
Correct |
232 ms |
8848 KB |
Output is correct |
56 |
Correct |
141 ms |
640 KB |
Output is correct |
57 |
Correct |
131 ms |
760 KB |
Output is correct |
58 |
Correct |
112 ms |
8084 KB |
Output is correct |
59 |
Correct |
126 ms |
8084 KB |
Output is correct |
60 |
Correct |
166 ms |
8076 KB |
Output is correct |
61 |
Correct |
111 ms |
8084 KB |
Output is correct |
62 |
Correct |
125 ms |
8212 KB |
Output is correct |
63 |
Correct |
166 ms |
8084 KB |
Output is correct |
64 |
Correct |
164 ms |
7828 KB |
Output is correct |
65 |
Correct |
176 ms |
7700 KB |
Output is correct |
66 |
Correct |
220 ms |
7700 KB |
Output is correct |
67 |
Correct |
26 ms |
6396 KB |
Output is correct |
68 |
Correct |
136 ms |
9232 KB |
Output is correct |
69 |
Correct |
136 ms |
9488 KB |
Output is correct |
70 |
Correct |
203 ms |
9476 KB |
Output is correct |
71 |
Correct |
205 ms |
9352 KB |
Output is correct |
72 |
Correct |
210 ms |
9104 KB |
Output is correct |
73 |
Correct |
200 ms |
9500 KB |
Output is correct |
74 |
Correct |
203 ms |
9540 KB |
Output is correct |