#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=2e5+5;
const ll INF=1e15;
int n,m;
int l[mxn], r[mxn];
ll c[mxn], d[mxn];
vector<int> adj[mxn];
bool blocked[mxn] = {};
bool used[mxn] = {};
void dijkstra(int s, vector<ll> &d, vector<int> & p) {
d.resize(n + 1);
p.resize(n + 1);
for(int i = 1; i <= n; i++) {
d[i] = INF;
p[i] = -1;
}
vector<bool> u(n, false);
d[s] = 0;
for(int i = 1; i <= n; i++) {
int v = -1;
for (int j = 1; j <= n; j++) {
if (!u[j] && (v == -1 || d[j] < d[v]))
v = j;
}
if(d[v] == INF)
break;
u[v] = true;
for (auto edge : adj[v]) {
if(blocked[edge]) continue;
int to = r[edge];
ll len = c[edge];
if (d[v] + len < d[to]) {
d[to] = d[v] + len;
p[to] = edge;
}
}
}
}
int main() {
cin >> n >> m;
ll w[n + 1][n + 1];
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) {
if(i == j) w[i][j] = 0;
else w[i][j] = INF;
}
for(int i = 1; i <= m; i++) {
scanf("%d %d %lld %lld",&l[i],&r[i],&c[i],&d[i]);
adj[l[i]].push_back(i);
w[l[i]][r[i]] = min(w[l[i]][r[i]], c[i]);
}
for(int k = 1; k <= n; k++) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
w[i][j] = min(w[i][j], w[i][k] + w[k][j]);
}
}
}
ll ans = w[1][n] + w[n][1];
int source = 1;
int target = n;
vector<ll> dist1, dist2;
vector<int> last1, last2;
vector<int> sedg;
dijkstra(source, dist1, last1);
dijkstra(target, dist2, last2);
if(dist1[target] < INF) {
int nd = target;
while(nd != source) {
used[last1[nd]] = true;
sedg.push_back(last1[nd]);
nd = l[last1[nd]];
}
}
if(dist2[source] < INF) {
int nd = source;
while(nd != target) {
used[last2[nd]] = true;
sedg.push_back(last2[nd]);
nd = l[last2[nd]];
}
}
for(int i = 1; i <= m; i++) {
if(used[i]) continue;
ans = min(ans, w[source][target] + w[target][r[i]] + d[i] + c[i] + w[l[i]][source]);
ans = min(ans, d[i] + w[source][r[i]] + c[i] + w[l[i]][target] + w[target][source]);
ans = min(ans, d[i] + w[source][r[i]] + c[i] + w[l[i]][target] + w[target][r[i]] + c[i] + w[l[i]][source]);
}
for(int i : sedg) {
blocked[i] = true;
vector<int> cl;
l[m + 1] = r[i];
r[m + 1] = l[i];
c[m + 1] = c[i];
adj[l[m + 1]].push_back(m + 1);
dijkstra(source, dist1, cl);
dijkstra(target, dist2, cl);
ans = min(ans, d[i] + dist1[target] + dist2[source]);
blocked[i] = false;
adj[l[m + 1]].pop_back();
}
if(ans >= INF) cout<<-1<<endl;
else cout<<ans<<endl;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d %d %lld %lld",&l[i],&r[i],&c[i],&d[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
5332 KB |
Output is correct |
2 |
Correct |
12 ms |
5348 KB |
Output is correct |
3 |
Correct |
14 ms |
5376 KB |
Output is correct |
4 |
Correct |
15 ms |
5332 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
11 ms |
5348 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
5 ms |
4948 KB |
Output is correct |
10 |
Correct |
93 ms |
5368 KB |
Output is correct |
11 |
Correct |
95 ms |
5372 KB |
Output is correct |
12 |
Correct |
92 ms |
5368 KB |
Output is correct |
13 |
Correct |
12 ms |
5332 KB |
Output is correct |
14 |
Correct |
14 ms |
5332 KB |
Output is correct |
15 |
Correct |
13 ms |
5332 KB |
Output is correct |
16 |
Correct |
15 ms |
5352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
6868 KB |
Output is correct |
2 |
Correct |
40 ms |
7812 KB |
Output is correct |
3 |
Correct |
40 ms |
8052 KB |
Output is correct |
4 |
Correct |
20 ms |
5436 KB |
Output is correct |
5 |
Correct |
15 ms |
5384 KB |
Output is correct |
6 |
Correct |
17 ms |
5332 KB |
Output is correct |
7 |
Correct |
12 ms |
5268 KB |
Output is correct |
8 |
Correct |
5 ms |
4948 KB |
Output is correct |
9 |
Correct |
36 ms |
7940 KB |
Output is correct |
10 |
Correct |
34 ms |
8016 KB |
Output is correct |
11 |
Correct |
39 ms |
7968 KB |
Output is correct |
12 |
Correct |
41 ms |
8060 KB |
Output is correct |
13 |
Correct |
42 ms |
8008 KB |
Output is correct |
14 |
Correct |
46 ms |
8080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
5332 KB |
Output is correct |
2 |
Correct |
16 ms |
5248 KB |
Output is correct |
3 |
Correct |
33 ms |
6504 KB |
Output is correct |
4 |
Correct |
12 ms |
5360 KB |
Output is correct |
5 |
Correct |
36 ms |
7172 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
36 ms |
7188 KB |
Output is correct |
9 |
Correct |
34 ms |
7168 KB |
Output is correct |
10 |
Correct |
34 ms |
7188 KB |
Output is correct |
11 |
Correct |
35 ms |
7180 KB |
Output is correct |
12 |
Correct |
36 ms |
7184 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5008 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4980 KB |
Output is correct |
19 |
Correct |
36 ms |
7784 KB |
Output is correct |
20 |
Correct |
36 ms |
7724 KB |
Output is correct |
21 |
Correct |
41 ms |
7776 KB |
Output is correct |
22 |
Correct |
36 ms |
7768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
5332 KB |
Output is correct |
2 |
Correct |
12 ms |
5348 KB |
Output is correct |
3 |
Correct |
14 ms |
5376 KB |
Output is correct |
4 |
Correct |
15 ms |
5332 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
11 ms |
5348 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
5 ms |
4948 KB |
Output is correct |
10 |
Correct |
93 ms |
5368 KB |
Output is correct |
11 |
Correct |
95 ms |
5372 KB |
Output is correct |
12 |
Correct |
92 ms |
5368 KB |
Output is correct |
13 |
Correct |
12 ms |
5332 KB |
Output is correct |
14 |
Correct |
14 ms |
5332 KB |
Output is correct |
15 |
Correct |
13 ms |
5332 KB |
Output is correct |
16 |
Correct |
15 ms |
5352 KB |
Output is correct |
17 |
Correct |
39 ms |
6868 KB |
Output is correct |
18 |
Correct |
40 ms |
7812 KB |
Output is correct |
19 |
Correct |
40 ms |
8052 KB |
Output is correct |
20 |
Correct |
20 ms |
5436 KB |
Output is correct |
21 |
Correct |
15 ms |
5384 KB |
Output is correct |
22 |
Correct |
17 ms |
5332 KB |
Output is correct |
23 |
Correct |
12 ms |
5268 KB |
Output is correct |
24 |
Correct |
5 ms |
4948 KB |
Output is correct |
25 |
Correct |
36 ms |
7940 KB |
Output is correct |
26 |
Correct |
34 ms |
8016 KB |
Output is correct |
27 |
Correct |
39 ms |
7968 KB |
Output is correct |
28 |
Correct |
41 ms |
8060 KB |
Output is correct |
29 |
Correct |
42 ms |
8008 KB |
Output is correct |
30 |
Correct |
46 ms |
8080 KB |
Output is correct |
31 |
Correct |
22 ms |
5332 KB |
Output is correct |
32 |
Correct |
16 ms |
5248 KB |
Output is correct |
33 |
Correct |
33 ms |
6504 KB |
Output is correct |
34 |
Correct |
12 ms |
5360 KB |
Output is correct |
35 |
Correct |
36 ms |
7172 KB |
Output is correct |
36 |
Correct |
4 ms |
4948 KB |
Output is correct |
37 |
Correct |
4 ms |
4948 KB |
Output is correct |
38 |
Correct |
36 ms |
7188 KB |
Output is correct |
39 |
Correct |
34 ms |
7168 KB |
Output is correct |
40 |
Correct |
34 ms |
7188 KB |
Output is correct |
41 |
Correct |
35 ms |
7180 KB |
Output is correct |
42 |
Correct |
36 ms |
7184 KB |
Output is correct |
43 |
Correct |
3 ms |
4948 KB |
Output is correct |
44 |
Correct |
3 ms |
4948 KB |
Output is correct |
45 |
Correct |
3 ms |
5008 KB |
Output is correct |
46 |
Correct |
4 ms |
4948 KB |
Output is correct |
47 |
Correct |
3 ms |
4948 KB |
Output is correct |
48 |
Correct |
4 ms |
4980 KB |
Output is correct |
49 |
Correct |
36 ms |
7784 KB |
Output is correct |
50 |
Correct |
36 ms |
7724 KB |
Output is correct |
51 |
Correct |
41 ms |
7776 KB |
Output is correct |
52 |
Correct |
36 ms |
7768 KB |
Output is correct |
53 |
Correct |
54 ms |
7876 KB |
Output is correct |
54 |
Correct |
44 ms |
7764 KB |
Output is correct |
55 |
Correct |
43 ms |
7764 KB |
Output is correct |
56 |
Correct |
15 ms |
5400 KB |
Output is correct |
57 |
Correct |
19 ms |
5332 KB |
Output is correct |
58 |
Correct |
211 ms |
7412 KB |
Output is correct |
59 |
Correct |
248 ms |
7408 KB |
Output is correct |
60 |
Correct |
213 ms |
7400 KB |
Output is correct |
61 |
Correct |
175 ms |
7436 KB |
Output is correct |
62 |
Correct |
272 ms |
7372 KB |
Output is correct |
63 |
Correct |
260 ms |
7408 KB |
Output is correct |
64 |
Correct |
390 ms |
7472 KB |
Output is correct |
65 |
Correct |
376 ms |
7372 KB |
Output is correct |
66 |
Correct |
349 ms |
7464 KB |
Output is correct |
67 |
Correct |
20 ms |
6968 KB |
Output is correct |
68 |
Correct |
42 ms |
7956 KB |
Output is correct |
69 |
Correct |
37 ms |
7916 KB |
Output is correct |
70 |
Correct |
46 ms |
8008 KB |
Output is correct |
71 |
Correct |
37 ms |
8012 KB |
Output is correct |
72 |
Correct |
46 ms |
7984 KB |
Output is correct |
73 |
Correct |
40 ms |
8012 KB |
Output is correct |
74 |
Correct |
44 ms |
7972 KB |
Output is correct |