#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
typedef long long int LLI;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
struct edge { int v,c,d,i; };
vector<edge> adjList[2][200];
int dist1[200],dist2[200],dist3[200],dist4[200],dist5[200],dist6[200];
priority_queue<pii> H;
int inTree[50000];
int findPath(int u,int *dist,int N,int r,edge rev) {
int i;
for (i = 0; i < N; i++) dist[i] = -1;
dist[u] = 0,H.push(mp(0,u));
while (!H.empty()) {
int u = H.top().second;
int d = -H.top().first;
H.pop();
if (d > dist[u]) continue;
for (i = 0; i < adjList[r][u].size(); i++) {
int v = adjList[r][u][i].v;
if (adjList[r][u][i].i == rev.i) continue;
if ((dist[v] == -1) || (dist[u]+adjList[r][u][i].c < dist[v])) {
dist[v] = dist[u]+adjList[r][u][i].c;
if (rev.i == -1) inTree[adjList[r][u][i].i] = 1;
H.push(mp(-dist[v],v));
}
}
if (u == rev.v) {
int v = rev.d;
if ((dist[v] == -1) || (dist[u]+rev.c < dist[v])) {
dist[v] = dist[u]+rev.c;
H.push(mp(-dist[v],v));
}
}
}
return 0;
}
int main() {
int i;
int N,M;
int U,V,C,D;
scanf("%d %d",&N,&M);
for (i = 0; i < M; i++) {
scanf("%d %d %d %d",&U,&V,&C,&D);
U--,V--;
adjList[0][U].pb((edge){V,C,D,i});
adjList[1][V].pb((edge){U,C,D,i});
}
int j,ans = 2e9;
findPath(0,dist1,N,0,(edge){-1,-1,-1,-1}),findPath(N-1,dist2,N,1,(edge){-1,-1,-1,-1});
findPath(0,dist3,N,1,(edge){-1,-1,-1,-1}),findPath(N-1,dist4,N,0,(edge){-1,-1,-1,-1});
if ((dist1[N-1] != -1) && (dist4[0] != -1)) ans = dist1[N-1]+dist4[0];
for (i = 0; i < N; i++) {
for (j = 0; j < adjList[0][i].size(); j++) {
int v = adjList[0][i][j].v;
if (!inTree[adjList[0][i][j].i]) {
if ((dist1[v] != -1) && (dist2[i] != -1) && (dist4[0] != -1))
ans = min(ans,dist1[v]+dist2[i]+dist4[0]+adjList[0][i][j].c+adjList[0][i][j].d);
if ((dist1[N-1] != -1) && (dist4[v] != -1) && (dist3[i] != -1))
ans = min(ans,dist1[N-1]+dist4[v]+dist3[i]+adjList[0][i][j].c+adjList[0][i][j].d);
if ((dist1[v] != -1) && (dist2[i] != -1) && (dist4[v] != -1) && (dist3[i] != -1))
ans = min(ans,dist1[v]+dist2[i]+dist4[v]+dist3[i]+2*adjList[0][i][j].c+adjList[0][i][j].d);
}
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < adjList[0][i].size(); j++) {
int v = adjList[0][i][j].v;
if (inTree[adjList[0][i][j].i]) {
int x = 2e9;
if ((dist1[v] != -1) && (dist2[i] != -1) && (dist4[0] != -1))
x = min(x,dist1[v]+dist2[i]+dist4[0]+adjList[0][i][j].c+adjList[0][i][j].d);
if ((dist1[N-1] != -1) && (dist4[v] != -1) && (dist3[i] != -1))
x = min(x,dist1[N-1]+dist4[v]+dist3[i]+adjList[0][i][j].c+adjList[0][i][j].d);
if ((dist1[v] != -1) && (dist2[i] != -1) && (dist4[v] != -1) && (dist3[i] != -1))
x = min(x,dist1[v]+dist2[i]+dist4[v]+dist3[i]+2*adjList[0][i][j].c+adjList[0][i][j].d);
if (x >= ans) continue;
edge e = adjList[0][i][j];
e.d = i;
findPath(0,dist5,N,0,e),findPath(N-1,dist6,N,0,e);
if ((dist5[N-1] != -1) && (dist6[0] != -1)) ans = min(ans,dist5[N-1]+dist6[0]+adjList[0][i][j].d);
}
}
}
if (ans == 2e9) printf("-1\n");
else printf("%d\n",ans);
return 0;
}
Compilation message
ho_t4.cpp: In function 'int findPath(int, int*, int, int, edge)':
ho_t4.cpp:25:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[r][u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:61:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < adjList[0][i].size(); j++) {
~~^~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:74:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < adjList[0][i].size(); j++) {
~~^~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&M);
~~~~~^~~~~~~~~~~~~~~
ho_t4.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d",&U,&V,&C,&D);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
9 ms |
384 KB |
Output is correct |
11 |
Correct |
19 ms |
384 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
3064 KB |
Output is correct |
2 |
Correct |
32 ms |
4088 KB |
Output is correct |
3 |
Correct |
36 ms |
4344 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
35 ms |
4216 KB |
Output is correct |
10 |
Correct |
34 ms |
4224 KB |
Output is correct |
11 |
Correct |
37 ms |
4344 KB |
Output is correct |
12 |
Correct |
33 ms |
4216 KB |
Output is correct |
13 |
Correct |
34 ms |
4216 KB |
Output is correct |
14 |
Correct |
35 ms |
4472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
27 ms |
2176 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
31 ms |
3072 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
30 ms |
3064 KB |
Output is correct |
9 |
Correct |
29 ms |
2944 KB |
Output is correct |
10 |
Correct |
30 ms |
2944 KB |
Output is correct |
11 |
Correct |
30 ms |
3072 KB |
Output is correct |
12 |
Correct |
30 ms |
3072 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
30 ms |
3200 KB |
Output is correct |
20 |
Correct |
30 ms |
3072 KB |
Output is correct |
21 |
Correct |
32 ms |
3064 KB |
Output is correct |
22 |
Correct |
31 ms |
3012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
9 ms |
384 KB |
Output is correct |
11 |
Correct |
19 ms |
384 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
31 ms |
3064 KB |
Output is correct |
18 |
Correct |
32 ms |
4088 KB |
Output is correct |
19 |
Correct |
36 ms |
4344 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
35 ms |
4216 KB |
Output is correct |
26 |
Correct |
34 ms |
4224 KB |
Output is correct |
27 |
Correct |
37 ms |
4344 KB |
Output is correct |
28 |
Correct |
33 ms |
4216 KB |
Output is correct |
29 |
Correct |
34 ms |
4216 KB |
Output is correct |
30 |
Correct |
35 ms |
4472 KB |
Output is correct |
31 |
Correct |
6 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
27 ms |
2176 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
31 ms |
3072 KB |
Output is correct |
36 |
Correct |
4 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
30 ms |
3064 KB |
Output is correct |
39 |
Correct |
29 ms |
2944 KB |
Output is correct |
40 |
Correct |
30 ms |
2944 KB |
Output is correct |
41 |
Correct |
30 ms |
3072 KB |
Output is correct |
42 |
Correct |
30 ms |
3072 KB |
Output is correct |
43 |
Correct |
4 ms |
384 KB |
Output is correct |
44 |
Correct |
4 ms |
384 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
4 ms |
384 KB |
Output is correct |
47 |
Correct |
4 ms |
384 KB |
Output is correct |
48 |
Correct |
4 ms |
384 KB |
Output is correct |
49 |
Correct |
30 ms |
3200 KB |
Output is correct |
50 |
Correct |
30 ms |
3072 KB |
Output is correct |
51 |
Correct |
32 ms |
3064 KB |
Output is correct |
52 |
Correct |
31 ms |
3012 KB |
Output is correct |
53 |
Correct |
36 ms |
3968 KB |
Output is correct |
54 |
Correct |
36 ms |
3960 KB |
Output is correct |
55 |
Correct |
33 ms |
3960 KB |
Output is correct |
56 |
Correct |
6 ms |
384 KB |
Output is correct |
57 |
Correct |
6 ms |
384 KB |
Output is correct |
58 |
Correct |
30 ms |
3064 KB |
Output is correct |
59 |
Correct |
32 ms |
3072 KB |
Output is correct |
60 |
Correct |
28 ms |
3072 KB |
Output is correct |
61 |
Correct |
29 ms |
3064 KB |
Output is correct |
62 |
Correct |
57 ms |
3072 KB |
Output is correct |
63 |
Correct |
106 ms |
3248 KB |
Output is correct |
64 |
Correct |
89 ms |
3584 KB |
Output is correct |
65 |
Correct |
193 ms |
3576 KB |
Output is correct |
66 |
Correct |
394 ms |
3584 KB |
Output is correct |
67 |
Correct |
25 ms |
2932 KB |
Output is correct |
68 |
Correct |
33 ms |
4216 KB |
Output is correct |
69 |
Correct |
33 ms |
4088 KB |
Output is correct |
70 |
Correct |
34 ms |
4344 KB |
Output is correct |
71 |
Correct |
33 ms |
4216 KB |
Output is correct |
72 |
Correct |
37 ms |
4216 KB |
Output is correct |
73 |
Correct |
34 ms |
4344 KB |
Output is correct |
74 |
Correct |
35 ms |
4216 KB |
Output is correct |