#include <bits/stdc++.h>
#define pb push_back
#define sz(x) (int)(x).size()
#define trav(x, a) for (const auto& x: a)
#define pii pair<int,int>
#define st first
#define nd second
using namespace std;
typedef long long ll;
const int N = 5e4 + 5;
const ll inf = 0x1f1f1f1f1f1f1f1f;
vector<int> e[2][N], adj[N];
int U[N], V[N], C[N], D[N];
ll dp[2][2][N], dp2[2][205][205];
int n, m, par[2][2][N];
int mrk[2][2][N];
vector<int> path[2][2];
void solve(int s, int t, int i) {
priority_queue<pii,vector<pii>,greater<pii>> pq;
pq.push({0, s});
for (int j = 1; j <= n; j++)
dp[i][s>1][j] = inf;
dp[i][s>1][s] = 0;
while (!pq.empty()) {
auto c = pq.top(); pq.pop();
if (dp[i][s>1][c.nd] != c.st)
continue;
for (int j: e[i][c.nd]) {
int v = U[j]^V[j]^c.nd;
if (dp[i][s>1][v] > c.st + C[j]) {
dp[i][s>1][v] = c.st + C[j];
par[i][s>1][v] = j;
pq.push({dp[i][s>1][v], v});
}
}
}
if (dp[i][s>1][t] == inf)
return;
int u = t;
while (u != s) {
int j = par[i][s>1][u];
mrk[i][s>1][j] = 1;
path[i][s>1].pb(u);
u = U[j]^V[j]^u;
}
path[i][s>1].pb(u);
reverse(path[i][s>1].begin(), path[i][s>1].end());
}
ll calc(int j, int i) {
int mi = n+1;
for (int l = 0; l+1 < sz(path[0][j]); l++) {
if (U[i] == path[0][j][l] && path[0][j][l+1] == V[i]) {
mi = l;
break;
}
}
ll ret = inf;
for (int l = 0; l <= mi; l++)
for (int r = mi+1; r < sz(path[0][j]); r++)
ret = min(ret, dp[0][j][path[0][j][l]] + dp2[j][path[0][j][l]][path[0][j][r]] + dp[1][j^1][path[0][j][r]]);
return ret;
}
int main() {
memset(dp, 0x1f, sizeof dp);
memset(dp2, 0x1f, sizeof dp2);
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d %d %d", U+i, V+i, C+i, D+i);
e[0][U[i]].pb(i);
e[1][V[i]].pb(i);
}
for (int i = 0; i < 2; i++) {
solve(1, n, i);
solve(n, 1, i);
}
for (int t = 0; t < 2; t++) {
for (int i = 1; i <= m; i++) if (!mrk[0][t][i])
dp2[t][U[i]][V[i]] = min(dp2[t][U[i]][V[i]], 1ll*C[i]);
for (int i = 1; i <= n; i++)
dp2[t][i][i] = 0;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dp2[t][i][j] = min(dp2[t][i][j], dp2[t][i][k] + dp2[t][k][j]);
}
ll x1 = dp[0][0][n], x2 = dp[0][1][1];
ll ans = x1 + x2;
for (int i = 1; i <= m; i++) {
bool f1 = mrk[0][0][i], f2 = mrk[0][1][i];
ll c1 = min(x1, dp[0][0][V[i]] + C[i] + dp[1][1][U[i]]);
ll c2 = min(x2, dp[0][1][V[i]] + C[i] + dp[1][0][U[i]]);
if (f1 && f2)
ans = min(ans, calc(0,i) + calc(1,i) + D[i]);
else if (f1)
ans = min(ans, calc(0,i) + c2 + D[i]);
else if (f2)
ans = min(ans, calc(1,i) + c1 + D[i]);
else
ans = min(ans, c1 + c2 + D[i]);
}
if (ans >= inf)
ans = -1;
printf("%I64d\n", ans);
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:106:24: warning: format '%d' expects argument of type 'int', but argument 2 has type 'll {aka long long int}' [-Wformat=]
printf("%I64d\n", ans);
^
ho_t4.cpp:69:8: 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:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d", U+i, V+i, C+i, D+i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
6136 KB |
Output is correct |
2 |
Correct |
26 ms |
6136 KB |
Output is correct |
3 |
Correct |
26 ms |
6136 KB |
Output is correct |
4 |
Correct |
27 ms |
6008 KB |
Output is correct |
5 |
Correct |
10 ms |
6136 KB |
Output is correct |
6 |
Correct |
26 ms |
6136 KB |
Output is correct |
7 |
Correct |
8 ms |
6136 KB |
Output is correct |
8 |
Correct |
7 ms |
6136 KB |
Output is correct |
9 |
Correct |
8 ms |
6136 KB |
Output is correct |
10 |
Correct |
32 ms |
6136 KB |
Output is correct |
11 |
Correct |
32 ms |
6136 KB |
Output is correct |
12 |
Correct |
32 ms |
6136 KB |
Output is correct |
13 |
Correct |
27 ms |
6136 KB |
Output is correct |
14 |
Correct |
27 ms |
6136 KB |
Output is correct |
15 |
Correct |
27 ms |
6136 KB |
Output is correct |
16 |
Correct |
27 ms |
6136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
7672 KB |
Output is correct |
2 |
Correct |
54 ms |
7544 KB |
Output is correct |
3 |
Correct |
65 ms |
7548 KB |
Output is correct |
4 |
Correct |
28 ms |
6136 KB |
Output is correct |
5 |
Correct |
30 ms |
6136 KB |
Output is correct |
6 |
Correct |
26 ms |
6136 KB |
Output is correct |
7 |
Correct |
26 ms |
6136 KB |
Output is correct |
8 |
Correct |
8 ms |
6136 KB |
Output is correct |
9 |
Correct |
55 ms |
7544 KB |
Output is correct |
10 |
Correct |
55 ms |
7544 KB |
Output is correct |
11 |
Correct |
54 ms |
7544 KB |
Output is correct |
12 |
Correct |
55 ms |
7672 KB |
Output is correct |
13 |
Correct |
55 ms |
7544 KB |
Output is correct |
14 |
Correct |
55 ms |
7672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
6136 KB |
Output is correct |
2 |
Correct |
26 ms |
6136 KB |
Output is correct |
3 |
Correct |
47 ms |
7164 KB |
Output is correct |
4 |
Correct |
27 ms |
6136 KB |
Output is correct |
5 |
Correct |
52 ms |
7544 KB |
Output is correct |
6 |
Correct |
8 ms |
6136 KB |
Output is correct |
7 |
Correct |
6 ms |
6264 KB |
Output is correct |
8 |
Correct |
51 ms |
7544 KB |
Output is correct |
9 |
Correct |
54 ms |
7544 KB |
Output is correct |
10 |
Correct |
51 ms |
7544 KB |
Output is correct |
11 |
Correct |
51 ms |
7544 KB |
Output is correct |
12 |
Correct |
52 ms |
7544 KB |
Output is correct |
13 |
Correct |
8 ms |
6136 KB |
Output is correct |
14 |
Correct |
8 ms |
6136 KB |
Output is correct |
15 |
Correct |
9 ms |
6136 KB |
Output is correct |
16 |
Correct |
8 ms |
6136 KB |
Output is correct |
17 |
Correct |
8 ms |
6136 KB |
Output is correct |
18 |
Correct |
8 ms |
6136 KB |
Output is correct |
19 |
Correct |
53 ms |
7544 KB |
Output is correct |
20 |
Correct |
51 ms |
7544 KB |
Output is correct |
21 |
Correct |
53 ms |
7544 KB |
Output is correct |
22 |
Correct |
51 ms |
7544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
6136 KB |
Output is correct |
2 |
Correct |
26 ms |
6136 KB |
Output is correct |
3 |
Correct |
26 ms |
6136 KB |
Output is correct |
4 |
Correct |
27 ms |
6008 KB |
Output is correct |
5 |
Correct |
10 ms |
6136 KB |
Output is correct |
6 |
Correct |
26 ms |
6136 KB |
Output is correct |
7 |
Correct |
8 ms |
6136 KB |
Output is correct |
8 |
Correct |
7 ms |
6136 KB |
Output is correct |
9 |
Correct |
8 ms |
6136 KB |
Output is correct |
10 |
Correct |
32 ms |
6136 KB |
Output is correct |
11 |
Correct |
32 ms |
6136 KB |
Output is correct |
12 |
Correct |
32 ms |
6136 KB |
Output is correct |
13 |
Correct |
27 ms |
6136 KB |
Output is correct |
14 |
Correct |
27 ms |
6136 KB |
Output is correct |
15 |
Correct |
27 ms |
6136 KB |
Output is correct |
16 |
Correct |
27 ms |
6136 KB |
Output is correct |
17 |
Correct |
54 ms |
7672 KB |
Output is correct |
18 |
Correct |
54 ms |
7544 KB |
Output is correct |
19 |
Correct |
65 ms |
7548 KB |
Output is correct |
20 |
Correct |
28 ms |
6136 KB |
Output is correct |
21 |
Correct |
30 ms |
6136 KB |
Output is correct |
22 |
Correct |
26 ms |
6136 KB |
Output is correct |
23 |
Correct |
26 ms |
6136 KB |
Output is correct |
24 |
Correct |
8 ms |
6136 KB |
Output is correct |
25 |
Correct |
55 ms |
7544 KB |
Output is correct |
26 |
Correct |
55 ms |
7544 KB |
Output is correct |
27 |
Correct |
54 ms |
7544 KB |
Output is correct |
28 |
Correct |
55 ms |
7672 KB |
Output is correct |
29 |
Correct |
55 ms |
7544 KB |
Output is correct |
30 |
Correct |
55 ms |
7672 KB |
Output is correct |
31 |
Correct |
27 ms |
6136 KB |
Output is correct |
32 |
Correct |
26 ms |
6136 KB |
Output is correct |
33 |
Correct |
47 ms |
7164 KB |
Output is correct |
34 |
Correct |
27 ms |
6136 KB |
Output is correct |
35 |
Correct |
52 ms |
7544 KB |
Output is correct |
36 |
Correct |
8 ms |
6136 KB |
Output is correct |
37 |
Correct |
6 ms |
6264 KB |
Output is correct |
38 |
Correct |
51 ms |
7544 KB |
Output is correct |
39 |
Correct |
54 ms |
7544 KB |
Output is correct |
40 |
Correct |
51 ms |
7544 KB |
Output is correct |
41 |
Correct |
51 ms |
7544 KB |
Output is correct |
42 |
Correct |
52 ms |
7544 KB |
Output is correct |
43 |
Correct |
8 ms |
6136 KB |
Output is correct |
44 |
Correct |
8 ms |
6136 KB |
Output is correct |
45 |
Correct |
9 ms |
6136 KB |
Output is correct |
46 |
Correct |
8 ms |
6136 KB |
Output is correct |
47 |
Correct |
8 ms |
6136 KB |
Output is correct |
48 |
Correct |
8 ms |
6136 KB |
Output is correct |
49 |
Correct |
53 ms |
7544 KB |
Output is correct |
50 |
Correct |
51 ms |
7544 KB |
Output is correct |
51 |
Correct |
53 ms |
7544 KB |
Output is correct |
52 |
Correct |
51 ms |
7544 KB |
Output is correct |
53 |
Correct |
53 ms |
7676 KB |
Output is correct |
54 |
Correct |
55 ms |
7544 KB |
Output is correct |
55 |
Correct |
53 ms |
7544 KB |
Output is correct |
56 |
Correct |
27 ms |
6136 KB |
Output is correct |
57 |
Correct |
27 ms |
6136 KB |
Output is correct |
58 |
Correct |
54 ms |
7800 KB |
Output is correct |
59 |
Correct |
53 ms |
7800 KB |
Output is correct |
60 |
Correct |
51 ms |
7800 KB |
Output is correct |
61 |
Correct |
50 ms |
7804 KB |
Output is correct |
62 |
Correct |
53 ms |
7800 KB |
Output is correct |
63 |
Correct |
52 ms |
7800 KB |
Output is correct |
64 |
Correct |
58 ms |
8108 KB |
Output is correct |
65 |
Correct |
57 ms |
8088 KB |
Output is correct |
66 |
Correct |
59 ms |
8108 KB |
Output is correct |
67 |
Correct |
28 ms |
7284 KB |
Output is correct |
68 |
Correct |
54 ms |
7544 KB |
Output is correct |
69 |
Correct |
55 ms |
7544 KB |
Output is correct |
70 |
Correct |
54 ms |
7544 KB |
Output is correct |
71 |
Correct |
55 ms |
7672 KB |
Output is correct |
72 |
Correct |
56 ms |
7544 KB |
Output is correct |
73 |
Correct |
55 ms |
7600 KB |
Output is correct |
74 |
Correct |
55 ms |
7544 KB |
Output is correct |