#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
}
void ran() {
srand(chrono::steady_clock::now().time_since_epoch().count());
}
long long get_rand() {
long long a = rand();
long long b = rand();
return a * (RAND_MAX + 1ll) + b;
}
void usaco() {
freopen("problem.in", "r", stdin);
freopen("problem.out", "w", stdout);
}
template<typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
#define endl '\n'
// #define double long double
#define int long long
// const int MOD = 1000 * 1000 * 1000 + 7;
// const int MOD = 998244353;
// #define cerr if(0) cerr
#define debug(x) cerr << #x << ": " << x << endl;
#define int long long
int n, m;
const int MXN = 205, MXM = 50005;
int apsp[MXN][MXN];
int eds[MXM][4];
vector<int> adj[MXN];
int mxh = INT_MAX * 1000ll;
int dist(int src, int dest, int skip) {
int dists[MXN];
for (int i=0; i<MXN; i++) dists[i] = mxh;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> proc;
proc.push({0, src});
while (!proc.empty()) {
auto x = proc.top(); proc.pop();
int di = x.first, no = x.second;
if (dists[no] < di) continue;
dists[no] = di;
for (auto ed: adj[no]) {
if (ed != skip) {
int ot = eds[ed][0] + eds[ed][1] - no;
int we = eds[ed][2];
if (dists[ot] > dists[no] + we) {
dists[ot] = dists[no] + we;
proc.push({dists[ot], ot});
}
}
}
if (no == eds[skip][1]) {
int ot = eds[skip][0];
int od = dists[no] + eds[skip][2];
if (dists[ot] > od) {
dists[ot] = od;
proc.push({od, ot});
}
}
}
return dists[dest];
}
signed main() {
ran(); fast();
for (int i=0; i<MXN; i++) {
for (int j=0; j<MXN; j++) {
apsp[i][j] = mxh;
}
apsp[i][i] = 0;
}
cin >> n >> m;
for (int i=0; i<m; i++) {
int a, b, w, c;
cin >> a >> b >> w >> c;
eds[i][0] = a;
eds[i][1] = b;
eds[i][2] = w;
eds[i][3] = c;
adj[a].push_back(i);
apsp[a][b] = min(apsp[a][b], w);
}
for (int k=1; k<=n; k++) {
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
if (apsp[i][k] + apsp[k][j] < apsp[i][j]) {
apsp[i][j] = apsp[i][k] + apsp[k][j];
}
}
}
}
int res = apsp[1][n] + apsp[n][1];
for (int i=0; i<m; i++) {
int a = eds[i][0], b = eds[i][1];
int w = eds[i][2], c = eds[i][3];
int d1 = min(apsp[1][n], apsp[1][b] + w + apsp[a][n]);
int d2 = min(apsp[n][1], apsp[n][b] + w + apsp[a][1]);
if (d1 + c + d2 < res) {
int actual = dist(1, n, i) + dist(n, 1, i) + c;
res = min(res, actual);
}
}
if (res >= mxh) cout << -1 << endl;
else cout << res << endl;
}
Compilation message
ho_t4.cpp: In function 'void usaco()':
ho_t4.cpp:15:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen("problem.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:16:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen("problem.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
724 KB |
Output is correct |
2 |
Correct |
7 ms |
588 KB |
Output is correct |
3 |
Correct |
8 ms |
716 KB |
Output is correct |
4 |
Correct |
8 ms |
720 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
7 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
13 ms |
724 KB |
Output is correct |
11 |
Correct |
20 ms |
716 KB |
Output is correct |
12 |
Correct |
10 ms |
716 KB |
Output is correct |
13 |
Correct |
8 ms |
716 KB |
Output is correct |
14 |
Correct |
8 ms |
660 KB |
Output is correct |
15 |
Correct |
8 ms |
664 KB |
Output is correct |
16 |
Correct |
8 ms |
720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
3880 KB |
Output is correct |
2 |
Correct |
27 ms |
3872 KB |
Output is correct |
3 |
Correct |
26 ms |
3972 KB |
Output is correct |
4 |
Correct |
9 ms |
792 KB |
Output is correct |
5 |
Correct |
8 ms |
660 KB |
Output is correct |
6 |
Correct |
8 ms |
588 KB |
Output is correct |
7 |
Correct |
7 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
31 ms |
4036 KB |
Output is correct |
10 |
Correct |
29 ms |
3956 KB |
Output is correct |
11 |
Correct |
26 ms |
4028 KB |
Output is correct |
12 |
Correct |
27 ms |
3976 KB |
Output is correct |
13 |
Correct |
25 ms |
3980 KB |
Output is correct |
14 |
Correct |
27 ms |
4100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
716 KB |
Output is correct |
2 |
Correct |
7 ms |
588 KB |
Output is correct |
3 |
Correct |
19 ms |
3076 KB |
Output is correct |
4 |
Correct |
7 ms |
656 KB |
Output is correct |
5 |
Correct |
22 ms |
3772 KB |
Output is correct |
6 |
Correct |
0 ms |
588 KB |
Output is correct |
7 |
Correct |
0 ms |
588 KB |
Output is correct |
8 |
Correct |
24 ms |
3788 KB |
Output is correct |
9 |
Correct |
26 ms |
3760 KB |
Output is correct |
10 |
Correct |
22 ms |
3924 KB |
Output is correct |
11 |
Correct |
21 ms |
3784 KB |
Output is correct |
12 |
Correct |
21 ms |
3764 KB |
Output is correct |
13 |
Correct |
0 ms |
656 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
0 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
22 ms |
3780 KB |
Output is correct |
20 |
Correct |
23 ms |
3732 KB |
Output is correct |
21 |
Correct |
22 ms |
3788 KB |
Output is correct |
22 |
Correct |
22 ms |
3788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
724 KB |
Output is correct |
2 |
Correct |
7 ms |
588 KB |
Output is correct |
3 |
Correct |
8 ms |
716 KB |
Output is correct |
4 |
Correct |
8 ms |
720 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
7 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
13 ms |
724 KB |
Output is correct |
11 |
Correct |
20 ms |
716 KB |
Output is correct |
12 |
Correct |
10 ms |
716 KB |
Output is correct |
13 |
Correct |
8 ms |
716 KB |
Output is correct |
14 |
Correct |
8 ms |
660 KB |
Output is correct |
15 |
Correct |
8 ms |
664 KB |
Output is correct |
16 |
Correct |
8 ms |
720 KB |
Output is correct |
17 |
Correct |
26 ms |
3880 KB |
Output is correct |
18 |
Correct |
27 ms |
3872 KB |
Output is correct |
19 |
Correct |
26 ms |
3972 KB |
Output is correct |
20 |
Correct |
9 ms |
792 KB |
Output is correct |
21 |
Correct |
8 ms |
660 KB |
Output is correct |
22 |
Correct |
8 ms |
588 KB |
Output is correct |
23 |
Correct |
7 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
31 ms |
4036 KB |
Output is correct |
26 |
Correct |
29 ms |
3956 KB |
Output is correct |
27 |
Correct |
26 ms |
4028 KB |
Output is correct |
28 |
Correct |
27 ms |
3976 KB |
Output is correct |
29 |
Correct |
25 ms |
3980 KB |
Output is correct |
30 |
Correct |
27 ms |
4100 KB |
Output is correct |
31 |
Correct |
7 ms |
716 KB |
Output is correct |
32 |
Correct |
7 ms |
588 KB |
Output is correct |
33 |
Correct |
19 ms |
3076 KB |
Output is correct |
34 |
Correct |
7 ms |
656 KB |
Output is correct |
35 |
Correct |
22 ms |
3772 KB |
Output is correct |
36 |
Correct |
0 ms |
588 KB |
Output is correct |
37 |
Correct |
0 ms |
588 KB |
Output is correct |
38 |
Correct |
24 ms |
3788 KB |
Output is correct |
39 |
Correct |
26 ms |
3760 KB |
Output is correct |
40 |
Correct |
22 ms |
3924 KB |
Output is correct |
41 |
Correct |
21 ms |
3784 KB |
Output is correct |
42 |
Correct |
21 ms |
3764 KB |
Output is correct |
43 |
Correct |
0 ms |
656 KB |
Output is correct |
44 |
Correct |
1 ms |
588 KB |
Output is correct |
45 |
Correct |
1 ms |
588 KB |
Output is correct |
46 |
Correct |
1 ms |
588 KB |
Output is correct |
47 |
Correct |
0 ms |
588 KB |
Output is correct |
48 |
Correct |
1 ms |
588 KB |
Output is correct |
49 |
Correct |
22 ms |
3780 KB |
Output is correct |
50 |
Correct |
23 ms |
3732 KB |
Output is correct |
51 |
Correct |
22 ms |
3788 KB |
Output is correct |
52 |
Correct |
22 ms |
3788 KB |
Output is correct |
53 |
Correct |
25 ms |
3884 KB |
Output is correct |
54 |
Correct |
26 ms |
3788 KB |
Output is correct |
55 |
Correct |
25 ms |
3736 KB |
Output is correct |
56 |
Correct |
8 ms |
716 KB |
Output is correct |
57 |
Correct |
12 ms |
724 KB |
Output is correct |
58 |
Correct |
23 ms |
3204 KB |
Output is correct |
59 |
Correct |
24 ms |
3204 KB |
Output is correct |
60 |
Correct |
25 ms |
3192 KB |
Output is correct |
61 |
Correct |
24 ms |
3200 KB |
Output is correct |
62 |
Correct |
56 ms |
3220 KB |
Output is correct |
63 |
Correct |
101 ms |
3268 KB |
Output is correct |
64 |
Correct |
69 ms |
3640 KB |
Output is correct |
65 |
Correct |
178 ms |
3708 KB |
Output is correct |
66 |
Correct |
316 ms |
3684 KB |
Output is correct |
67 |
Correct |
14 ms |
3152 KB |
Output is correct |
68 |
Correct |
32 ms |
3984 KB |
Output is correct |
69 |
Correct |
27 ms |
4044 KB |
Output is correct |
70 |
Correct |
26 ms |
4044 KB |
Output is correct |
71 |
Correct |
26 ms |
4024 KB |
Output is correct |
72 |
Correct |
26 ms |
4068 KB |
Output is correct |
73 |
Correct |
26 ms |
4096 KB |
Output is correct |
74 |
Correct |
26 ms |
4084 KB |
Output is correct |