#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<lint, lint>;
const int MAXN = 205;
int n, m;
lint adj[MAXN][MAXN];
int trk[MAXN][MAXN];
lint dap[50005];
struct edg{
int u, v, c, d, idx;
}ed[50005];
vector<int> pth;
void dfs(int x, int y){
if(trk[x][y] <= 0) pth.push_back(-trk[x][y]);
else{
dfs(x, trk[x][y]);
dfs(trk[x][y], y);
}
}
bool chk[50005];
lint kek[MAXN][MAXN];
lint Do_rev(int i, int s, int t){
memset(kek, 0x3f, sizeof(kek));
for(int j=0; j<m; j++){
int u = ed[j].u;
int v = ed[j].v;
lint c = ed[j].c;
if(j != i) kek[u][v] = min(kek[u][v], c);
else kek[v][u] = min(kek[v][u], c);
}
lint dist[MAXN];
bool vis[MAXN] = {};
memset(dist, 0x3f, sizeof(dist));
dist[s] = 0;
for(int i=0; i<n; i++){
lint cur = 1e18;
int pos = -1;
for(int j=1; j<=n; j++){
if(!vis[j] && dist[j] < cur){
cur = dist[j];
pos = j;
}
}
if(pos == -1) break;
vis[pos] = 1;
for(int j=1; j<=n; j++){
dist[j] = min(dist[pos] + kek[pos][j], dist[j]);
}
}
return dist[t];
}
pi dist[2][MAXN][2]; // distance, which edge
void dijk(int s, int flag){
bool vis[MAXN] = {};
vector<edg> gph[MAXN];
for(int i=0; i<m; i++){
gph[ed[i].u].push_back(ed[i]);
}
dist[flag][s][0] = pi(0, 0);
for(int i=0; i<n; i++){
lint cur = 1e18;
int pos = -1;
for(int j=1; j<=n; j++){
if(!vis[j] && dist[flag][j][0].first < cur){
cur = dist[flag][j][0].first;
pos = j;
}
}
if(pos == -1) break;
vis[pos] = 1;
for(auto &j : gph[pos]){
if(dist[flag][j.v][0].first > dist[flag][pos][0].first + j.c){
dist[flag][j.v][0].first = dist[flag][pos][0].first + j.c;
dist[flag][j.v][0].second = j.idx;
}
}
}
for(int i=1; i<=n; i++){
if(s == i){
dist[flag][i][1].first = 0;
continue;
}
dist[flag][i][1].first = Do_rev(dist[flag][i][0].second, s, i);
}
}
void solve(int s, int t){
memset(chk, 0, sizeof(chk));
for(int i=0; i<2; i++){
for(int j=0; j<MAXN; j++){
for(int k=0; k<2; k++){
dist[i][j][k] = pi(1e18, -1);
}
}
}
pth.clear();
if(adj[s][t] < 1e17){
dfs(s, t);
for(int i=0; i<sz(pth); i++){
chk[pth[i]] = 1;
}
}
dijk(s, 0);
for(int i=0; i<m; i++) swap(ed[i].u, ed[i].v);
dijk(t, 1);
for(int i=0; i<m; i++) swap(ed[i].u, ed[i].v);
for(int i=0; i<m; i++){
if(!chk[i]){
lint foo = ed[i].c;
if(dist[0][ed[i].v][0].second == i) foo += dist[0][ed[i].v][1].first;
else foo += dist[0][ed[i].v][0].first;
if(dist[1][ed[i].v][0].second == i) foo += dist[1][ed[i].u][1].first;
else foo += dist[1][ed[i].u][0].first;
dap[i] += min(adj[s][t], foo);
}
else{
dap[i] += Do_rev(i, s, t);
}
}
}
int main(){
scanf("%d %d",&n,&m);
memset(adj, 0x1f, sizeof(adj));
for(int i=1; i<=n; i++) adj[i][i] = 0;
for(int i=0; i<m; i++){
scanf("%d %d %d %d",&ed[i].u,&ed[i].v,&ed[i].c,&ed[i].d);
ed[i].idx = i;
if(adj[ed[i].u][ed[i].v] > ed[i].c){
adj[ed[i].u][ed[i].v] = ed[i].c;
trk[ed[i].u][ed[i].v] = -i;
}
dap[i] = ed[i].d;
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
for(int k=1; k<=n; k++){
if(adj[j][k] > adj[j][i] + adj[i][k]){
adj[j][k] = adj[j][i] + adj[i][k];
trk[j][k] = i;
}
}
}
}
lint ans = adj[1][n] + adj[n][1];
solve(1, n);
solve(n, 1);
ans = min(ans, *min_element(dap, dap + m));
if(ans > 1e15) ans = -1;
cout << ans << endl;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:134:7: 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:138:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d",&ed[i].u,&ed[i].v,&ed[i].c,&ed[i].d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
1276 KB |
Output is correct |
2 |
Correct |
34 ms |
1272 KB |
Output is correct |
3 |
Correct |
151 ms |
1324 KB |
Output is correct |
4 |
Correct |
148 ms |
1316 KB |
Output is correct |
5 |
Correct |
9 ms |
1148 KB |
Output is correct |
6 |
Correct |
54 ms |
1264 KB |
Output is correct |
7 |
Correct |
7 ms |
1016 KB |
Output is correct |
8 |
Correct |
5 ms |
1016 KB |
Output is correct |
9 |
Correct |
6 ms |
1196 KB |
Output is correct |
10 |
Correct |
215 ms |
1320 KB |
Output is correct |
11 |
Correct |
208 ms |
1272 KB |
Output is correct |
12 |
Correct |
212 ms |
1324 KB |
Output is correct |
13 |
Correct |
85 ms |
1324 KB |
Output is correct |
14 |
Correct |
114 ms |
1320 KB |
Output is correct |
15 |
Correct |
118 ms |
1400 KB |
Output is correct |
16 |
Correct |
111 ms |
1320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
454 ms |
4324 KB |
Output is correct |
2 |
Correct |
404 ms |
4352 KB |
Output is correct |
3 |
Correct |
452 ms |
4312 KB |
Output is correct |
4 |
Correct |
147 ms |
1272 KB |
Output is correct |
5 |
Correct |
128 ms |
1196 KB |
Output is correct |
6 |
Correct |
62 ms |
1272 KB |
Output is correct |
7 |
Correct |
28 ms |
1272 KB |
Output is correct |
8 |
Correct |
6 ms |
1016 KB |
Output is correct |
9 |
Correct |
376 ms |
4244 KB |
Output is correct |
10 |
Correct |
358 ms |
4168 KB |
Output is correct |
11 |
Correct |
409 ms |
4232 KB |
Output is correct |
12 |
Correct |
407 ms |
4192 KB |
Output is correct |
13 |
Correct |
406 ms |
4204 KB |
Output is correct |
14 |
Correct |
415 ms |
4320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
1272 KB |
Output is correct |
2 |
Correct |
61 ms |
1144 KB |
Output is correct |
3 |
Correct |
345 ms |
3416 KB |
Output is correct |
4 |
Correct |
51 ms |
1144 KB |
Output is correct |
5 |
Correct |
380 ms |
4232 KB |
Output is correct |
6 |
Correct |
5 ms |
1016 KB |
Output is correct |
7 |
Correct |
5 ms |
1016 KB |
Output is correct |
8 |
Correct |
305 ms |
4208 KB |
Output is correct |
9 |
Correct |
357 ms |
4316 KB |
Output is correct |
10 |
Correct |
389 ms |
4188 KB |
Output is correct |
11 |
Correct |
381 ms |
4156 KB |
Output is correct |
12 |
Correct |
384 ms |
4284 KB |
Output is correct |
13 |
Correct |
5 ms |
1016 KB |
Output is correct |
14 |
Correct |
5 ms |
1016 KB |
Output is correct |
15 |
Correct |
6 ms |
1016 KB |
Output is correct |
16 |
Correct |
6 ms |
1016 KB |
Output is correct |
17 |
Correct |
6 ms |
1016 KB |
Output is correct |
18 |
Correct |
5 ms |
1016 KB |
Output is correct |
19 |
Correct |
378 ms |
4328 KB |
Output is correct |
20 |
Correct |
384 ms |
4220 KB |
Output is correct |
21 |
Correct |
378 ms |
4184 KB |
Output is correct |
22 |
Correct |
377 ms |
4148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
1276 KB |
Output is correct |
2 |
Correct |
34 ms |
1272 KB |
Output is correct |
3 |
Correct |
151 ms |
1324 KB |
Output is correct |
4 |
Correct |
148 ms |
1316 KB |
Output is correct |
5 |
Correct |
9 ms |
1148 KB |
Output is correct |
6 |
Correct |
54 ms |
1264 KB |
Output is correct |
7 |
Correct |
7 ms |
1016 KB |
Output is correct |
8 |
Correct |
5 ms |
1016 KB |
Output is correct |
9 |
Correct |
6 ms |
1196 KB |
Output is correct |
10 |
Correct |
215 ms |
1320 KB |
Output is correct |
11 |
Correct |
208 ms |
1272 KB |
Output is correct |
12 |
Correct |
212 ms |
1324 KB |
Output is correct |
13 |
Correct |
85 ms |
1324 KB |
Output is correct |
14 |
Correct |
114 ms |
1320 KB |
Output is correct |
15 |
Correct |
118 ms |
1400 KB |
Output is correct |
16 |
Correct |
111 ms |
1320 KB |
Output is correct |
17 |
Correct |
454 ms |
4324 KB |
Output is correct |
18 |
Correct |
404 ms |
4352 KB |
Output is correct |
19 |
Correct |
452 ms |
4312 KB |
Output is correct |
20 |
Correct |
147 ms |
1272 KB |
Output is correct |
21 |
Correct |
128 ms |
1196 KB |
Output is correct |
22 |
Correct |
62 ms |
1272 KB |
Output is correct |
23 |
Correct |
28 ms |
1272 KB |
Output is correct |
24 |
Correct |
6 ms |
1016 KB |
Output is correct |
25 |
Correct |
376 ms |
4244 KB |
Output is correct |
26 |
Correct |
358 ms |
4168 KB |
Output is correct |
27 |
Correct |
409 ms |
4232 KB |
Output is correct |
28 |
Correct |
407 ms |
4192 KB |
Output is correct |
29 |
Correct |
406 ms |
4204 KB |
Output is correct |
30 |
Correct |
415 ms |
4320 KB |
Output is correct |
31 |
Correct |
113 ms |
1272 KB |
Output is correct |
32 |
Correct |
61 ms |
1144 KB |
Output is correct |
33 |
Correct |
345 ms |
3416 KB |
Output is correct |
34 |
Correct |
51 ms |
1144 KB |
Output is correct |
35 |
Correct |
380 ms |
4232 KB |
Output is correct |
36 |
Correct |
5 ms |
1016 KB |
Output is correct |
37 |
Correct |
5 ms |
1016 KB |
Output is correct |
38 |
Correct |
305 ms |
4208 KB |
Output is correct |
39 |
Correct |
357 ms |
4316 KB |
Output is correct |
40 |
Correct |
389 ms |
4188 KB |
Output is correct |
41 |
Correct |
381 ms |
4156 KB |
Output is correct |
42 |
Correct |
384 ms |
4284 KB |
Output is correct |
43 |
Correct |
5 ms |
1016 KB |
Output is correct |
44 |
Correct |
5 ms |
1016 KB |
Output is correct |
45 |
Correct |
6 ms |
1016 KB |
Output is correct |
46 |
Correct |
6 ms |
1016 KB |
Output is correct |
47 |
Correct |
6 ms |
1016 KB |
Output is correct |
48 |
Correct |
5 ms |
1016 KB |
Output is correct |
49 |
Correct |
378 ms |
4328 KB |
Output is correct |
50 |
Correct |
384 ms |
4220 KB |
Output is correct |
51 |
Correct |
378 ms |
4184 KB |
Output is correct |
52 |
Correct |
377 ms |
4148 KB |
Output is correct |
53 |
Correct |
432 ms |
4044 KB |
Output is correct |
54 |
Correct |
449 ms |
4180 KB |
Output is correct |
55 |
Correct |
433 ms |
4192 KB |
Output is correct |
56 |
Correct |
147 ms |
1276 KB |
Output is correct |
57 |
Correct |
149 ms |
1400 KB |
Output is correct |
58 |
Correct |
532 ms |
3452 KB |
Output is correct |
59 |
Correct |
558 ms |
3476 KB |
Output is correct |
60 |
Correct |
529 ms |
3584 KB |
Output is correct |
61 |
Correct |
502 ms |
3516 KB |
Output is correct |
62 |
Correct |
536 ms |
3564 KB |
Output is correct |
63 |
Correct |
535 ms |
3504 KB |
Output is correct |
64 |
Correct |
536 ms |
3620 KB |
Output is correct |
65 |
Correct |
529 ms |
3704 KB |
Output is correct |
66 |
Correct |
514 ms |
3576 KB |
Output is correct |
67 |
Correct |
32 ms |
4248 KB |
Output is correct |
68 |
Correct |
372 ms |
4128 KB |
Output is correct |
69 |
Correct |
366 ms |
4192 KB |
Output is correct |
70 |
Correct |
415 ms |
4184 KB |
Output is correct |
71 |
Correct |
406 ms |
4252 KB |
Output is correct |
72 |
Correct |
402 ms |
4300 KB |
Output is correct |
73 |
Correct |
438 ms |
4376 KB |
Output is correct |
74 |
Correct |
420 ms |
4216 KB |
Output is correct |