#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Edge{
int u, v, idx; ll c, d;
Edge(){}
Edge(int u, int v, int idx, ll c, ll d): u(u), v(v), idx(idx), c(c), d(d){}
bool operator==(const Edge &r)const{
return idx == r.idx;
}
};
int n, m;
vector<Edge> link[202];
vector<Edge> revLink[202];
Edge arr[50002];
ll ans = LLONG_MAX;
struct Dijkstra {
int n;
ll dist[202]; int prv[202], start, last;
bool used[50002], chk[202];
void init(int _n, int _start){
n = _n, start = _start, last = n+1-start;
}
void run(){
fill(dist+1, dist+n+1, 1e18);
fill(chk+1, chk+n+1, false);
fill(used+1, used+m+1, false);
dist[start] = 0;
while(1){
int pnt = 0; ll pntD = 1e18;
for(int i=1; i<=n; i++){
if(!chk[i] && pntD > dist[i]) pntD = dist[i], pnt = i;
}
if(pnt == 0) break;
chk[pnt] = 1;
for(auto y: link[pnt]){
int nxt = y.v;
ll nxtCost = dist[pnt] + y.c;
if(nxtCost < dist[nxt]){
dist[nxt] = nxtCost;
prv[nxt] = y.idx;
}
}
}
if(dist[last] == 1e18) return;
int tmp = last;
while(tmp != start){
used[prv[tmp]] = 1;
tmp = arr[prv[tmp]].u;
}
}
} dijk1, dijk2, dijktmp, dijk1rev, dijk2rev;
int main(){
scanf("%d %d", &n, &m);
for(int i=1; i<=m; i++){
int u, v; ll c, d;
scanf("%d %d %lld %lld", &u, &v, &c, &d);
link[u].push_back(Edge(u, v, i, c, d));
revLink[v].push_back(Edge(v, u, i, c, d));
arr[i] = Edge(u, v, i, c, d);
}
dijk1.init(n, 1), dijk1rev.init(n, n), dijk2.init(n, n), dijk2rev.init(n, 1);
dijk1.run(), dijk2.run();
ans = dijk1.dist[n] + dijk2.dist[1];
for(int i=1; i<=n; i++) revLink[i].swap(link[i]);
for(int i=1; i<=m; i++) swap(arr[i].u, arr[i].v);
dijk1rev.run();
dijk2rev.run();
for(int i=1; i<=n; i++) revLink[i].swap(link[i]);
for(int i=1; i<=m; i++) swap(arr[i].u, arr[i].v);
for(int i=1; i<=m; i++){
ll ret = arr[i].d;
if(!dijk1.used[i]) ret += min(dijk1.dist[n], dijk1.dist[arr[i].v] + dijk1rev.dist[arr[i].u] + arr[i].c);
else{
link[arr[i].u].erase(find(link[arr[i].u].begin(), link[arr[i].u].end(), arr[i]));
swap(arr[i].u, arr[i].v);
link[arr[i].u].push_back(arr[i]);
dijktmp.init(n, 1);
dijktmp.run();
ret += dijktmp.dist[n];
link[arr[i].u].erase(find(link[arr[i].u].begin(), link[arr[i].u].end(), arr[i]));
swap(arr[i].u, arr[i].v);
link[arr[i].u].push_back(arr[i]);
}
if(!dijk2.used[i]) ret += min(dijk2.dist[1], dijk2.dist[arr[i].v] + dijk2rev.dist[arr[i].u] + arr[i].c);
else{
link[arr[i].u].erase(find(link[arr[i].u].begin(), link[arr[i].u].end(), arr[i]));
swap(arr[i].u, arr[i].v);
link[arr[i].u].push_back(arr[i]);
dijktmp.init(n, n);
dijktmp.run();
ret += dijktmp.dist[1];
link[arr[i].u].erase(find(link[arr[i].u].begin(), link[arr[i].u].end(), arr[i]));
swap(arr[i].u, arr[i].v);
link[arr[i].u].push_back(arr[i]);
}
ans = min(ans, ret);
}
printf("%lld", ans > 1e17 ? -1 : ans);
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
66 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
ho_t4.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
69 | scanf("%d %d %lld %lld", &u, &v, &c, &d);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
5 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
50 ms |
460 KB |
Output is correct |
11 |
Correct |
50 ms |
496 KB |
Output is correct |
12 |
Correct |
50 ms |
484 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
3 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
6856 KB |
Output is correct |
2 |
Correct |
30 ms |
7916 KB |
Output is correct |
3 |
Correct |
39 ms |
7996 KB |
Output is correct |
4 |
Correct |
4 ms |
588 KB |
Output is correct |
5 |
Correct |
4 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
38 ms |
7960 KB |
Output is correct |
10 |
Correct |
28 ms |
8004 KB |
Output is correct |
11 |
Correct |
31 ms |
8260 KB |
Output is correct |
12 |
Correct |
31 ms |
8296 KB |
Output is correct |
13 |
Correct |
31 ms |
8004 KB |
Output is correct |
14 |
Correct |
38 ms |
8388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
21 ms |
5948 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
27 ms |
7832 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
27 ms |
7928 KB |
Output is correct |
9 |
Correct |
35 ms |
8004 KB |
Output is correct |
10 |
Correct |
29 ms |
7752 KB |
Output is correct |
11 |
Correct |
43 ms |
7732 KB |
Output is correct |
12 |
Correct |
28 ms |
8244 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
28 ms |
8040 KB |
Output is correct |
20 |
Correct |
27 ms |
7876 KB |
Output is correct |
21 |
Correct |
34 ms |
7620 KB |
Output is correct |
22 |
Correct |
28 ms |
7820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
5 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
50 ms |
460 KB |
Output is correct |
11 |
Correct |
50 ms |
496 KB |
Output is correct |
12 |
Correct |
50 ms |
484 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
3 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
32 ms |
6856 KB |
Output is correct |
18 |
Correct |
30 ms |
7916 KB |
Output is correct |
19 |
Correct |
39 ms |
7996 KB |
Output is correct |
20 |
Correct |
4 ms |
588 KB |
Output is correct |
21 |
Correct |
4 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
38 ms |
7960 KB |
Output is correct |
26 |
Correct |
28 ms |
8004 KB |
Output is correct |
27 |
Correct |
31 ms |
8260 KB |
Output is correct |
28 |
Correct |
31 ms |
8296 KB |
Output is correct |
29 |
Correct |
31 ms |
8004 KB |
Output is correct |
30 |
Correct |
38 ms |
8388 KB |
Output is correct |
31 |
Correct |
3 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
320 KB |
Output is correct |
33 |
Correct |
21 ms |
5948 KB |
Output is correct |
34 |
Correct |
1 ms |
328 KB |
Output is correct |
35 |
Correct |
27 ms |
7832 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
316 KB |
Output is correct |
38 |
Correct |
27 ms |
7928 KB |
Output is correct |
39 |
Correct |
35 ms |
8004 KB |
Output is correct |
40 |
Correct |
29 ms |
7752 KB |
Output is correct |
41 |
Correct |
43 ms |
7732 KB |
Output is correct |
42 |
Correct |
28 ms |
8244 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
28 ms |
8040 KB |
Output is correct |
50 |
Correct |
27 ms |
7876 KB |
Output is correct |
51 |
Correct |
34 ms |
7620 KB |
Output is correct |
52 |
Correct |
28 ms |
7820 KB |
Output is correct |
53 |
Correct |
29 ms |
7760 KB |
Output is correct |
54 |
Correct |
37 ms |
7696 KB |
Output is correct |
55 |
Correct |
29 ms |
7668 KB |
Output is correct |
56 |
Correct |
3 ms |
460 KB |
Output is correct |
57 |
Correct |
3 ms |
460 KB |
Output is correct |
58 |
Correct |
106 ms |
6140 KB |
Output is correct |
59 |
Correct |
109 ms |
6220 KB |
Output is correct |
60 |
Correct |
112 ms |
6252 KB |
Output is correct |
61 |
Correct |
90 ms |
6136 KB |
Output is correct |
62 |
Correct |
107 ms |
6120 KB |
Output is correct |
63 |
Correct |
109 ms |
6184 KB |
Output is correct |
64 |
Correct |
153 ms |
6564 KB |
Output is correct |
65 |
Correct |
142 ms |
6596 KB |
Output is correct |
66 |
Correct |
131 ms |
6644 KB |
Output is correct |
67 |
Correct |
21 ms |
5428 KB |
Output is correct |
68 |
Correct |
29 ms |
8188 KB |
Output is correct |
69 |
Correct |
29 ms |
8004 KB |
Output is correct |
70 |
Correct |
32 ms |
7884 KB |
Output is correct |
71 |
Correct |
38 ms |
7996 KB |
Output is correct |
72 |
Correct |
30 ms |
7984 KB |
Output is correct |
73 |
Correct |
30 ms |
8104 KB |
Output is correct |
74 |
Correct |
32 ms |
8260 KB |
Output is correct |