#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]+adjList[0][i][j].c+adjList[0][i][j].d);
}
else {
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: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 |
19 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
38 ms |
384 KB |
Output is correct |
4 |
Correct |
42 ms |
384 KB |
Output is correct |
5 |
Correct |
10 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 |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
69 ms |
504 KB |
Output is correct |
11 |
Correct |
69 ms |
384 KB |
Output is correct |
12 |
Correct |
68 ms |
384 KB |
Output is correct |
13 |
Incorrect |
11 ms |
384 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1091 ms |
3068 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
172 ms |
2296 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
197 ms |
3064 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
79 ms |
3192 KB |
Output is correct |
9 |
Correct |
79 ms |
3840 KB |
Output is correct |
10 |
Correct |
134 ms |
4088 KB |
Output is correct |
11 |
Correct |
132 ms |
3960 KB |
Output is correct |
12 |
Correct |
141 ms |
4216 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 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 |
141 ms |
4096 KB |
Output is correct |
20 |
Correct |
130 ms |
3960 KB |
Output is correct |
21 |
Correct |
164 ms |
3960 KB |
Output is correct |
22 |
Correct |
146 ms |
3968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
38 ms |
384 KB |
Output is correct |
4 |
Correct |
42 ms |
384 KB |
Output is correct |
5 |
Correct |
10 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 |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
69 ms |
504 KB |
Output is correct |
11 |
Correct |
69 ms |
384 KB |
Output is correct |
12 |
Correct |
68 ms |
384 KB |
Output is correct |
13 |
Incorrect |
11 ms |
384 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |