#include "bits/stdc++.h"
using namespace std;
long long d[222][222];
vector <int> g[222], t[222];
int l[50010], r[50010], c[50010], db[50010];
int n, m;
const long long inf = 1e16;
struct data {
int node;
long long dist;
data (int node, long long dist) : node(node), dist(dist) {}
data () {}
bool operator < (data d) const {
return dist > d.dist;
}
};
vector <long long> shortest_path(int src, int del) {
priority_queue <data> Q;
Q.push(data(src, 0));
vector <long long> dist (n + 1, inf);
dist[src] = 0;
while(!Q.empty()) {
data u = Q.top();
Q.pop();
for(auto e : g[u.node]) {
if(e == del) continue;
int y = r[e];
if(u.dist + c[e] < dist[y]) {
dist[y] = u.dist + c[e];
Q.push(data(y, dist[y]));
}
}
}
return dist;
}
vector <int> path(int u, int v) {
vector <long long> dist (n + 1, inf);
dist[u] = 0;
vector <int> prev (n + 1);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(dist[l[j]] + c[j] < dist[r[j]]) {
dist[r[j]] = dist[l[j]] + c[j];
prev[r[j]] = j;
}
}
}
vector <int> ans;
if(dist[v] >= inf) return ans;
while(u != v) {
ans.push_back(prev[v]);
v ^= l[prev[v]] ^ r[prev[v]];
}
return ans;
}
bool imp[50010];
vector <long long> delpath[50010], delsrc[50010];
long long delEdge(int u, int x, int e) {
long long opt = inf;
for(auto i : t[x]) {
if(i != e) {
opt = min(opt, c[i] + d[u][l[i]]);
}
}
return opt;
}
long long solve(int u, int v) {
long long ans = d[u][v] + d[v][u];
for(int j = 1; j <= m; j++) {
long long p1 = delsrc[j][r[j]] + c[j] + db[j] + d[l[j]][v];
long long p2 = delpath[j][r[j]] + c[j] + d[l[j]][u];
p2 = min(p2, delpath[j][u]);
ans = min(ans, p1 + p2);
}
return ans;
}
void build() {
int u = 1, v = n;
vector <int> can = path(v, u);
memset(imp, false, sizeof imp);
for(int i : can) imp[i] = true;
for(int i = 1; i <= m; i++) {
if(imp[i]) {
delpath[i] = shortest_path(v, i);
} else {
delpath[i].resize(n + 1);
for(int j = 1; j <= n; j++) {
delpath[i][j] = d[v][j];
}
}
}
can = path(u, v);
memset(imp, false, sizeof imp);
for(int i : can) imp[i] = true;
for(int i = 1; i <= m; i++) {
if(imp[i]) {
delsrc[i] = shortest_path(u, i);
} else {
delsrc[i].resize(n + 1);
for(int j = 1; j <= n; j++) {
delsrc[i][j] = d[u][j];
}
}
}
}
int main(int argc, char const *argv[])
{
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i != j) {
d[i][j] = inf;
}
}
}
for(int i = 1; i <= m; i++) {
scanf("%d %d %d %d", &l[i], &r[i], &c[i], &db[i]);
d[l[i]][r[i]] = min(d[l[i]][r[i]], (long long) c[i]);
g[l[i]].push_back(i);
t[r[i]].push_back(i);
}
for(int k = 1; k <= n; k++) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
build();
long long ans = solve(1, n);
for(int i = 1; i <= m; i++) {
delsrc[i].swap(delpath[i]);
}
ans = min(ans, solve(n, 1));
if(ans >= inf) ans = -1;
printf("%lld\n", ans);
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main(int, const char**)':
ho_t4.cpp:114:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
114 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
ho_t4.cpp:123:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
123 | scanf("%d %d %d %d", &l[i], &r[i], &c[i], &db[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
6272 KB |
Output is correct |
2 |
Correct |
12 ms |
3968 KB |
Output is correct |
3 |
Correct |
16 ms |
6352 KB |
Output is correct |
4 |
Correct |
16 ms |
6272 KB |
Output is correct |
5 |
Correct |
3 ms |
3328 KB |
Output is correct |
6 |
Correct |
12 ms |
3968 KB |
Output is correct |
7 |
Correct |
2 ms |
2816 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
3 ms |
2816 KB |
Output is correct |
10 |
Correct |
35 ms |
6264 KB |
Output is correct |
11 |
Correct |
29 ms |
6264 KB |
Output is correct |
12 |
Correct |
32 ms |
6264 KB |
Output is correct |
13 |
Correct |
15 ms |
6272 KB |
Output is correct |
14 |
Correct |
15 ms |
6272 KB |
Output is correct |
15 |
Correct |
15 ms |
6272 KB |
Output is correct |
16 |
Correct |
15 ms |
6272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
163576 KB |
Output is correct |
2 |
Correct |
206 ms |
163716 KB |
Output is correct |
3 |
Correct |
201 ms |
163832 KB |
Output is correct |
4 |
Correct |
19 ms |
9472 KB |
Output is correct |
5 |
Correct |
16 ms |
6272 KB |
Output is correct |
6 |
Correct |
16 ms |
4992 KB |
Output is correct |
7 |
Correct |
13 ms |
4352 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
195 ms |
163784 KB |
Output is correct |
10 |
Correct |
192 ms |
163800 KB |
Output is correct |
11 |
Correct |
214 ms |
163808 KB |
Output is correct |
12 |
Correct |
203 ms |
163832 KB |
Output is correct |
13 |
Correct |
203 ms |
163832 KB |
Output is correct |
14 |
Correct |
206 ms |
163832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
6272 KB |
Output is correct |
2 |
Correct |
12 ms |
3968 KB |
Output is correct |
3 |
Correct |
153 ms |
128248 KB |
Output is correct |
4 |
Correct |
12 ms |
3968 KB |
Output is correct |
5 |
Correct |
191 ms |
163576 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
196 ms |
163576 KB |
Output is correct |
9 |
Correct |
191 ms |
163516 KB |
Output is correct |
10 |
Correct |
192 ms |
163576 KB |
Output is correct |
11 |
Correct |
191 ms |
163512 KB |
Output is correct |
12 |
Correct |
195 ms |
163576 KB |
Output is correct |
13 |
Correct |
2 ms |
2688 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2740 KB |
Output is correct |
17 |
Correct |
2 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
196 ms |
163552 KB |
Output is correct |
20 |
Correct |
194 ms |
163504 KB |
Output is correct |
21 |
Correct |
196 ms |
163576 KB |
Output is correct |
22 |
Correct |
218 ms |
163576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
6272 KB |
Output is correct |
2 |
Correct |
12 ms |
3968 KB |
Output is correct |
3 |
Correct |
16 ms |
6352 KB |
Output is correct |
4 |
Correct |
16 ms |
6272 KB |
Output is correct |
5 |
Correct |
3 ms |
3328 KB |
Output is correct |
6 |
Correct |
12 ms |
3968 KB |
Output is correct |
7 |
Correct |
2 ms |
2816 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
3 ms |
2816 KB |
Output is correct |
10 |
Correct |
35 ms |
6264 KB |
Output is correct |
11 |
Correct |
29 ms |
6264 KB |
Output is correct |
12 |
Correct |
32 ms |
6264 KB |
Output is correct |
13 |
Correct |
15 ms |
6272 KB |
Output is correct |
14 |
Correct |
15 ms |
6272 KB |
Output is correct |
15 |
Correct |
15 ms |
6272 KB |
Output is correct |
16 |
Correct |
15 ms |
6272 KB |
Output is correct |
17 |
Correct |
201 ms |
163576 KB |
Output is correct |
18 |
Correct |
206 ms |
163716 KB |
Output is correct |
19 |
Correct |
201 ms |
163832 KB |
Output is correct |
20 |
Correct |
19 ms |
9472 KB |
Output is correct |
21 |
Correct |
16 ms |
6272 KB |
Output is correct |
22 |
Correct |
16 ms |
4992 KB |
Output is correct |
23 |
Correct |
13 ms |
4352 KB |
Output is correct |
24 |
Correct |
2 ms |
2688 KB |
Output is correct |
25 |
Correct |
195 ms |
163784 KB |
Output is correct |
26 |
Correct |
192 ms |
163800 KB |
Output is correct |
27 |
Correct |
214 ms |
163808 KB |
Output is correct |
28 |
Correct |
203 ms |
163832 KB |
Output is correct |
29 |
Correct |
203 ms |
163832 KB |
Output is correct |
30 |
Correct |
206 ms |
163832 KB |
Output is correct |
31 |
Correct |
19 ms |
6272 KB |
Output is correct |
32 |
Correct |
12 ms |
3968 KB |
Output is correct |
33 |
Correct |
153 ms |
128248 KB |
Output is correct |
34 |
Correct |
12 ms |
3968 KB |
Output is correct |
35 |
Correct |
191 ms |
163576 KB |
Output is correct |
36 |
Correct |
2 ms |
2688 KB |
Output is correct |
37 |
Correct |
2 ms |
2688 KB |
Output is correct |
38 |
Correct |
196 ms |
163576 KB |
Output is correct |
39 |
Correct |
191 ms |
163516 KB |
Output is correct |
40 |
Correct |
192 ms |
163576 KB |
Output is correct |
41 |
Correct |
191 ms |
163512 KB |
Output is correct |
42 |
Correct |
195 ms |
163576 KB |
Output is correct |
43 |
Correct |
2 ms |
2688 KB |
Output is correct |
44 |
Correct |
2 ms |
2688 KB |
Output is correct |
45 |
Correct |
2 ms |
2688 KB |
Output is correct |
46 |
Correct |
2 ms |
2740 KB |
Output is correct |
47 |
Correct |
2 ms |
2688 KB |
Output is correct |
48 |
Correct |
2 ms |
2688 KB |
Output is correct |
49 |
Correct |
196 ms |
163552 KB |
Output is correct |
50 |
Correct |
194 ms |
163504 KB |
Output is correct |
51 |
Correct |
196 ms |
163576 KB |
Output is correct |
52 |
Correct |
218 ms |
163576 KB |
Output is correct |
53 |
Correct |
204 ms |
163712 KB |
Output is correct |
54 |
Correct |
203 ms |
163608 KB |
Output is correct |
55 |
Correct |
205 ms |
163576 KB |
Output is correct |
56 |
Correct |
15 ms |
6272 KB |
Output is correct |
57 |
Correct |
15 ms |
6272 KB |
Output is correct |
58 |
Correct |
385 ms |
131428 KB |
Output is correct |
59 |
Correct |
419 ms |
131576 KB |
Output is correct |
60 |
Correct |
442 ms |
131452 KB |
Output is correct |
61 |
Correct |
324 ms |
131480 KB |
Output is correct |
62 |
Correct |
385 ms |
131448 KB |
Output is correct |
63 |
Correct |
413 ms |
131472 KB |
Output is correct |
64 |
Execution timed out |
1093 ms |
92444 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |