#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define pb push_back
#define mp make_pair
#define ss(x) (int) x.size()
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)
using namespace std;
const int nax = 50005;
const LL INF = 1e18;
int n, m;
int e[nax], f[nax], c[nax], d[nax];
bool na[nax];
multiset <pair<LL, int>> s[210][210];
LL cost[210][210];
pair <LL, int> dp[210][210];
LL dp2[210][210];
vector <int> path;
vector <int> ind;
void add(int x) {
if (ss(path) && path.back() == x) return;
path.pb(x);
}
void gao(int a, int b) {
assert(a != b);
if (dp[a][b].fi == cost[a][b]) {
add(a);
add(b);
return;
}
gao(a, dp[a][b].se);
gao(dp[a][b].se, b);
}
LL out[nax];
LL in[nax];
LL fe;
void solve(int skad, int dokad) {
rep(i, 1, n) rep(j, 1, n) {
s[i][j].clear();
cost[i][j] = INF;
}
rep(i, 1, m) {
s[e[i]][f[i]].insert({c[i], i});
cost[e[i]][f[i]] = min(cost[e[i]][f[i]], (LL) c[i]);
na[i] = 0;
in[i] = INF;
}
rep(i, 1, n)
rep(j, 1, n) {
if (i == j) dp[i][j] = {0LL, 0};
else if (!s[i][j].empty()) dp[i][j] = {s[i][j].begin()->fi, 0};
else dp[i][j] = {INF, 0};
}
rep(k, 1, n) rep(i, 1, n) rep(j, 1, n)
dp[i][j] = min(dp[i][j], {dp[i][k].fi + dp[k][j].fi, k});
fe += dp[skad][dokad].fi;
path.clear();
ind.clear();
if (dp[skad][dokad].fi != INF) {
gao(skad, dokad);
}
for (int i = 0; i + 1 < ss(path); ++i) {
int a = path[i];
int b = path[i + 1];
int id = s[a][b].begin()->se;
s[a][b].erase(s[a][b].begin());
na[id] = 1;
ind.pb(id);
}
rep(i, 1, n) rep(j, 1, n) {
if (i == j) dp2[i][j] = 0LL;
else if(ss(s[i][j])) dp2[i][j] = s[i][j].begin()->fi;
else dp2[i][j] = INF;
}
rep(k, 1, n) rep(i, 1, n) rep(j, 1, n)
dp2[i][j] = min(dp2[i][j], dp2[i][k] + dp2[k][j]);
rep(i, 1, m) {
if (!na[i]) {
out[i] += min(dp[skad][dokad].fi, c[i] + dp[skad][f[i]].fi + dp[e[i]][dokad].fi);
}
}
for (int i = 0; i < ss(path); ++i)
for (int j = i + 1; j < ss(path); ++j) {
int a = path[i];
int b = path[j];
LL val = dp2[a][b] + dp[skad][a].fi + dp[b][dokad].fi;
for (int k = i; k < j; ++k)
in[ind[k]] = min(in[ind[k]], val);
}
rep(i, 1, m)
if (na[i])
out[i] += in[i];
}
int main() {
scanf ("%d%d", &n, &m);
rep(i, 1, m) {
scanf ("%d%d%d%d", e + i, f + i, c + i, d + i);
}
solve(1, n);
solve(n, 1);
LL best = fe;
rep(i, 1, m) best = min(best, out[i] + d[i]);
if (best >= INF) printf ("-1\n");
else printf ("%lld", best);
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:123: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:125:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d%d%d%d", e + i, f + i, c + i, d + i);
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
3832 KB |
Output is correct |
2 |
Correct |
52 ms |
3704 KB |
Output is correct |
3 |
Correct |
52 ms |
3832 KB |
Output is correct |
4 |
Correct |
52 ms |
3832 KB |
Output is correct |
5 |
Correct |
7 ms |
2808 KB |
Output is correct |
6 |
Correct |
48 ms |
3704 KB |
Output is correct |
7 |
Correct |
6 ms |
2424 KB |
Output is correct |
8 |
Correct |
6 ms |
2424 KB |
Output is correct |
9 |
Correct |
7 ms |
2552 KB |
Output is correct |
10 |
Correct |
63 ms |
3800 KB |
Output is correct |
11 |
Correct |
63 ms |
3832 KB |
Output is correct |
12 |
Correct |
64 ms |
3832 KB |
Output is correct |
13 |
Correct |
55 ms |
3960 KB |
Output is correct |
14 |
Correct |
51 ms |
3832 KB |
Output is correct |
15 |
Correct |
53 ms |
3832 KB |
Output is correct |
16 |
Correct |
50 ms |
3836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
8440 KB |
Output is correct |
2 |
Correct |
94 ms |
8568 KB |
Output is correct |
3 |
Correct |
99 ms |
8440 KB |
Output is correct |
4 |
Correct |
55 ms |
4088 KB |
Output is correct |
5 |
Correct |
50 ms |
3832 KB |
Output is correct |
6 |
Correct |
50 ms |
3832 KB |
Output is correct |
7 |
Correct |
47 ms |
3832 KB |
Output is correct |
8 |
Correct |
6 ms |
2424 KB |
Output is correct |
9 |
Correct |
96 ms |
8440 KB |
Output is correct |
10 |
Correct |
98 ms |
8440 KB |
Output is correct |
11 |
Correct |
94 ms |
8568 KB |
Output is correct |
12 |
Correct |
93 ms |
8440 KB |
Output is correct |
13 |
Correct |
96 ms |
8568 KB |
Output is correct |
14 |
Correct |
91 ms |
8444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
3832 KB |
Output is correct |
2 |
Correct |
56 ms |
3812 KB |
Output is correct |
3 |
Correct |
83 ms |
7416 KB |
Output is correct |
4 |
Correct |
55 ms |
3832 KB |
Output is correct |
5 |
Correct |
92 ms |
8440 KB |
Output is correct |
6 |
Correct |
6 ms |
2424 KB |
Output is correct |
7 |
Correct |
6 ms |
2424 KB |
Output is correct |
8 |
Correct |
99 ms |
8440 KB |
Output is correct |
9 |
Correct |
96 ms |
8440 KB |
Output is correct |
10 |
Correct |
99 ms |
8508 KB |
Output is correct |
11 |
Correct |
98 ms |
8568 KB |
Output is correct |
12 |
Correct |
93 ms |
8440 KB |
Output is correct |
13 |
Correct |
7 ms |
2424 KB |
Output is correct |
14 |
Correct |
6 ms |
2424 KB |
Output is correct |
15 |
Correct |
6 ms |
2424 KB |
Output is correct |
16 |
Correct |
7 ms |
2424 KB |
Output is correct |
17 |
Correct |
6 ms |
2424 KB |
Output is correct |
18 |
Correct |
6 ms |
2424 KB |
Output is correct |
19 |
Correct |
100 ms |
8440 KB |
Output is correct |
20 |
Correct |
98 ms |
8440 KB |
Output is correct |
21 |
Correct |
95 ms |
8440 KB |
Output is correct |
22 |
Correct |
99 ms |
8436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
3832 KB |
Output is correct |
2 |
Correct |
52 ms |
3704 KB |
Output is correct |
3 |
Correct |
52 ms |
3832 KB |
Output is correct |
4 |
Correct |
52 ms |
3832 KB |
Output is correct |
5 |
Correct |
7 ms |
2808 KB |
Output is correct |
6 |
Correct |
48 ms |
3704 KB |
Output is correct |
7 |
Correct |
6 ms |
2424 KB |
Output is correct |
8 |
Correct |
6 ms |
2424 KB |
Output is correct |
9 |
Correct |
7 ms |
2552 KB |
Output is correct |
10 |
Correct |
63 ms |
3800 KB |
Output is correct |
11 |
Correct |
63 ms |
3832 KB |
Output is correct |
12 |
Correct |
64 ms |
3832 KB |
Output is correct |
13 |
Correct |
55 ms |
3960 KB |
Output is correct |
14 |
Correct |
51 ms |
3832 KB |
Output is correct |
15 |
Correct |
53 ms |
3832 KB |
Output is correct |
16 |
Correct |
50 ms |
3836 KB |
Output is correct |
17 |
Correct |
92 ms |
8440 KB |
Output is correct |
18 |
Correct |
94 ms |
8568 KB |
Output is correct |
19 |
Correct |
99 ms |
8440 KB |
Output is correct |
20 |
Correct |
55 ms |
4088 KB |
Output is correct |
21 |
Correct |
50 ms |
3832 KB |
Output is correct |
22 |
Correct |
50 ms |
3832 KB |
Output is correct |
23 |
Correct |
47 ms |
3832 KB |
Output is correct |
24 |
Correct |
6 ms |
2424 KB |
Output is correct |
25 |
Correct |
96 ms |
8440 KB |
Output is correct |
26 |
Correct |
98 ms |
8440 KB |
Output is correct |
27 |
Correct |
94 ms |
8568 KB |
Output is correct |
28 |
Correct |
93 ms |
8440 KB |
Output is correct |
29 |
Correct |
96 ms |
8568 KB |
Output is correct |
30 |
Correct |
91 ms |
8444 KB |
Output is correct |
31 |
Correct |
55 ms |
3832 KB |
Output is correct |
32 |
Correct |
56 ms |
3812 KB |
Output is correct |
33 |
Correct |
83 ms |
7416 KB |
Output is correct |
34 |
Correct |
55 ms |
3832 KB |
Output is correct |
35 |
Correct |
92 ms |
8440 KB |
Output is correct |
36 |
Correct |
6 ms |
2424 KB |
Output is correct |
37 |
Correct |
6 ms |
2424 KB |
Output is correct |
38 |
Correct |
99 ms |
8440 KB |
Output is correct |
39 |
Correct |
96 ms |
8440 KB |
Output is correct |
40 |
Correct |
99 ms |
8508 KB |
Output is correct |
41 |
Correct |
98 ms |
8568 KB |
Output is correct |
42 |
Correct |
93 ms |
8440 KB |
Output is correct |
43 |
Correct |
7 ms |
2424 KB |
Output is correct |
44 |
Correct |
6 ms |
2424 KB |
Output is correct |
45 |
Correct |
6 ms |
2424 KB |
Output is correct |
46 |
Correct |
7 ms |
2424 KB |
Output is correct |
47 |
Correct |
6 ms |
2424 KB |
Output is correct |
48 |
Correct |
6 ms |
2424 KB |
Output is correct |
49 |
Correct |
100 ms |
8440 KB |
Output is correct |
50 |
Correct |
98 ms |
8440 KB |
Output is correct |
51 |
Correct |
95 ms |
8440 KB |
Output is correct |
52 |
Correct |
99 ms |
8436 KB |
Output is correct |
53 |
Correct |
99 ms |
8444 KB |
Output is correct |
54 |
Correct |
99 ms |
8440 KB |
Output is correct |
55 |
Correct |
103 ms |
8440 KB |
Output is correct |
56 |
Correct |
51 ms |
3836 KB |
Output is correct |
57 |
Correct |
51 ms |
3832 KB |
Output is correct |
58 |
Correct |
93 ms |
7544 KB |
Output is correct |
59 |
Correct |
96 ms |
7544 KB |
Output is correct |
60 |
Correct |
96 ms |
7548 KB |
Output is correct |
61 |
Correct |
92 ms |
7672 KB |
Output is correct |
62 |
Correct |
94 ms |
7544 KB |
Output is correct |
63 |
Correct |
94 ms |
7544 KB |
Output is correct |
64 |
Correct |
99 ms |
7544 KB |
Output is correct |
65 |
Correct |
94 ms |
7544 KB |
Output is correct |
66 |
Correct |
96 ms |
7556 KB |
Output is correct |
67 |
Correct |
50 ms |
6268 KB |
Output is correct |
68 |
Correct |
98 ms |
8568 KB |
Output is correct |
69 |
Correct |
101 ms |
8440 KB |
Output is correct |
70 |
Correct |
95 ms |
8440 KB |
Output is correct |
71 |
Correct |
102 ms |
8568 KB |
Output is correct |
72 |
Correct |
102 ms |
8592 KB |
Output is correct |
73 |
Correct |
105 ms |
8440 KB |
Output is correct |
74 |
Correct |
97 ms |
8440 KB |
Output is correct |