#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e18;
int dis[205][205];
array<int, 4> Edge[50005];
vector<array<int, 3>> adj1[205];
int dist[205];
bool vis[205];
int adj[205][205];
int opt[205];
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N, M;
cin >> N >> M;
int i, j;
clock_t st = clock();
for(i=0;i<N;i++) {
for(j=0;j<N;j++) dis[i][j] = INF;
dis[i][i] = 0;
}
for(i=0;i<M;i++) {
int a, b, c, d;
//a = rand()%N+1, b = rand()%N+1, c = rand(), d = rand();
cin >> a >> b >> c >> d;
Edge[i] = {a-1, b-1, c, d};
dis[a-1][b-1] = min(dis[a-1][b-1], c);
}
for(int k = 0; k < N; k++) {
for(i=0;i<N;i++) {
for(j=0;j<N;j++) {
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
for(i=0;i<M;i++) {
adj1[Edge[i][0]].push_back({Edge[i][1], i});
}
set<int> S;
for(i=0;i<N;i++) dist[i] = INF;
for(i=0;i<N;i++) vis[i] = false;
dist[0] = 0;
priority_queue<array<int, 2>, vector<array<int, 2>>, greater<array<int,2>>> PQ;
PQ.push({0, 0});
while(!PQ.empty()) {
auto k = PQ.top();
PQ.pop();
int id = k[1];
if(vis[id]) continue;
vis[id] = true;
for(auto n : adj1[id]) {
if(dist[n[0]] > dist[id] + Edge[n[1]][2]) {
dist[n[0]] = dist[id] + Edge[n[1]][2];
opt[n[0]] = n[1];
PQ.push({dist[n[0]], n[0]});
}
}
}
for(i=0;i<N;i++) vis[i] = false;
int pt = N-1;
if(dist[N-1]!=INF) {
while(pt != 0) {
S.insert(opt[pt]);
pt = Edge[opt[pt]][0];
}
}
for(i=0;i<N;i++) dist[i] = INF;
for(i=0;i<N;i++) vis[i] = false;
dist[N-1] = 0;
PQ = priority_queue<array<int, 2>, vector<array<int, 2>>, greater<array<int,2>>> {};
PQ.push({0, N-1});
while(!PQ.empty()) {
auto k = PQ.top();
PQ.pop();
int id = k[1];
if(vis[id]) continue;
vis[id] = true;
for(auto n : adj1[id]) {
if(dist[n[0]] > dist[id] + Edge[n[1]][2]) {
dist[n[0]] = dist[id] + Edge[n[1]][2];
opt[n[0]] = n[1];
PQ.push({dist[n[0]], n[0]});
}
}
}
for(i=0;i<N;i++) vis[i] = false;
pt = 0;
if(dist[0]!=INF) {
while(pt != N-1) {
S.insert(opt[pt]);
pt = Edge[opt[pt]][0];
}
}
int mi = dis[0][N-1] + dis[N-1][0];
for(i=0;i<M;i++) {
if(S.find(i) != S.end()) continue;
int val = Edge[i][3];
int d1 = dis[0][N-1], d2 = dis[N-1][0];
int a = Edge[i][0], b = Edge[i][1], c = Edge[i][2];
d1 = min(d1, dis[0][b] + c + dis[a][N-1]);
d2 = min(d2, dis[N-1][b] + c + dis[a][0]);
mi = min(mi, val + d1 + d2);
//cout << i << " : " << val << ' ' << d1 << ' ' << d2 << '\n';
}
assert(S.size() < 2*N);
for(int n : S) {
int val = Edge[n][3];
for(i=0;i<N;i++) {
for(j=0;j<N;j++) adj[i][j] = INF;
adj[i][i] = 0;
}
for(i=0;i<M;i++) {
if(i==n) {
adj[Edge[i][1]][Edge[i][0]] = min(adj[Edge[i][1]][Edge[i][0]], Edge[i][2]);
}
else adj[Edge[i][0]][Edge[i][1]] = min(adj[Edge[i][0]][Edge[i][1]], Edge[i][2]);
}
for(i=0;i<N;i++) vis[i] = false;
for(i=0;i<N;i++) dist[i] = INF;
dist[0] = 0;
while(true) {
int mi = INF, pt = -1;
for(i=0;i<N;i++) {
if(!vis[i] && mi > dist[i]) {
mi = dist[i];
pt = i;
}
}
if(pt == -1) break;
vis[pt] = true;
for(i=0;i<N;i++) {
if(dist[i] > dist[pt] + adj[pt][i]) {
dist[i] = dist[pt] + adj[pt][i];
}
}
}
if(dist[N-1]==INF) continue;
val += dist[N-1];
for(i=0;i<N;i++) vis[i] = false;
for(i=0;i<N;i++) dist[i] = INF;
dist[N-1] = 0;
while(true) {
int mi = INF, pt = -1;
for(i=0;i<N;i++) {
if(!vis[i] && mi > dist[i]) {
mi = dist[i];
pt = i;
}
}
if(pt == -1) break;
vis[pt] = true;
for(i=0;i<N;i++) {
if(dist[i] > dist[pt] + adj[pt][i]) {
dist[i] = dist[pt] + adj[pt][i];
}
}
}
if(dist[0]==INF) continue;
val += dist[0];
mi = min(mi, val);
}
cout << (mi <= 1e17 ? mi : -1);
//cout << '\n' << clock() - st <<"ms";
}
Compilation message
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from ho_t4.cpp:1:
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:107:21: warning: comparison of integer expressions of different signedness: 'std::set<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
107 | assert(S.size() < 2*N);
| ~~~~~~~~~^~~~~
ho_t4.cpp:19:13: warning: unused variable 'st' [-Wunused-variable]
19 | clock_t st = clock();
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
980 KB |
Output is correct |
2 |
Correct |
8 ms |
596 KB |
Output is correct |
3 |
Correct |
11 ms |
980 KB |
Output is correct |
4 |
Correct |
14 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
11 ms |
676 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
70 ms |
1100 KB |
Output is correct |
11 |
Correct |
96 ms |
1076 KB |
Output is correct |
12 |
Correct |
103 ms |
1072 KB |
Output is correct |
13 |
Correct |
10 ms |
716 KB |
Output is correct |
14 |
Correct |
11 ms |
988 KB |
Output is correct |
15 |
Correct |
9 ms |
980 KB |
Output is correct |
16 |
Correct |
11 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
4356 KB |
Output is correct |
2 |
Correct |
30 ms |
4424 KB |
Output is correct |
3 |
Correct |
30 ms |
4308 KB |
Output is correct |
4 |
Correct |
11 ms |
1108 KB |
Output is correct |
5 |
Correct |
13 ms |
980 KB |
Output is correct |
6 |
Correct |
11 ms |
680 KB |
Output is correct |
7 |
Correct |
12 ms |
672 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
25 ms |
4092 KB |
Output is correct |
10 |
Correct |
25 ms |
4148 KB |
Output is correct |
11 |
Correct |
29 ms |
4308 KB |
Output is correct |
12 |
Correct |
30 ms |
4400 KB |
Output is correct |
13 |
Correct |
30 ms |
4528 KB |
Output is correct |
14 |
Correct |
29 ms |
4652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
952 KB |
Output is correct |
2 |
Correct |
8 ms |
980 KB |
Output is correct |
3 |
Correct |
23 ms |
4184 KB |
Output is correct |
4 |
Correct |
12 ms |
596 KB |
Output is correct |
5 |
Correct |
33 ms |
5272 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
24 ms |
4920 KB |
Output is correct |
9 |
Correct |
23 ms |
5076 KB |
Output is correct |
10 |
Correct |
25 ms |
5324 KB |
Output is correct |
11 |
Correct |
25 ms |
5148 KB |
Output is correct |
12 |
Correct |
26 ms |
5432 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
29 ms |
5432 KB |
Output is correct |
20 |
Correct |
32 ms |
5220 KB |
Output is correct |
21 |
Correct |
25 ms |
5320 KB |
Output is correct |
22 |
Correct |
25 ms |
5148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
980 KB |
Output is correct |
2 |
Correct |
8 ms |
596 KB |
Output is correct |
3 |
Correct |
11 ms |
980 KB |
Output is correct |
4 |
Correct |
14 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
11 ms |
676 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
70 ms |
1100 KB |
Output is correct |
11 |
Correct |
96 ms |
1076 KB |
Output is correct |
12 |
Correct |
103 ms |
1072 KB |
Output is correct |
13 |
Correct |
10 ms |
716 KB |
Output is correct |
14 |
Correct |
11 ms |
988 KB |
Output is correct |
15 |
Correct |
9 ms |
980 KB |
Output is correct |
16 |
Correct |
11 ms |
972 KB |
Output is correct |
17 |
Correct |
28 ms |
4356 KB |
Output is correct |
18 |
Correct |
30 ms |
4424 KB |
Output is correct |
19 |
Correct |
30 ms |
4308 KB |
Output is correct |
20 |
Correct |
11 ms |
1108 KB |
Output is correct |
21 |
Correct |
13 ms |
980 KB |
Output is correct |
22 |
Correct |
11 ms |
680 KB |
Output is correct |
23 |
Correct |
12 ms |
672 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
25 ms |
4092 KB |
Output is correct |
26 |
Correct |
25 ms |
4148 KB |
Output is correct |
27 |
Correct |
29 ms |
4308 KB |
Output is correct |
28 |
Correct |
30 ms |
4400 KB |
Output is correct |
29 |
Correct |
30 ms |
4528 KB |
Output is correct |
30 |
Correct |
29 ms |
4652 KB |
Output is correct |
31 |
Correct |
11 ms |
952 KB |
Output is correct |
32 |
Correct |
8 ms |
980 KB |
Output is correct |
33 |
Correct |
23 ms |
4184 KB |
Output is correct |
34 |
Correct |
12 ms |
596 KB |
Output is correct |
35 |
Correct |
33 ms |
5272 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
24 ms |
4920 KB |
Output is correct |
39 |
Correct |
23 ms |
5076 KB |
Output is correct |
40 |
Correct |
25 ms |
5324 KB |
Output is correct |
41 |
Correct |
25 ms |
5148 KB |
Output is correct |
42 |
Correct |
26 ms |
5432 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
29 ms |
5432 KB |
Output is correct |
50 |
Correct |
32 ms |
5220 KB |
Output is correct |
51 |
Correct |
25 ms |
5320 KB |
Output is correct |
52 |
Correct |
25 ms |
5148 KB |
Output is correct |
53 |
Correct |
29 ms |
5260 KB |
Output is correct |
54 |
Correct |
35 ms |
5340 KB |
Output is correct |
55 |
Correct |
32 ms |
5452 KB |
Output is correct |
56 |
Correct |
11 ms |
992 KB |
Output is correct |
57 |
Correct |
10 ms |
980 KB |
Output is correct |
58 |
Correct |
119 ms |
4460 KB |
Output is correct |
59 |
Correct |
137 ms |
4396 KB |
Output is correct |
60 |
Correct |
189 ms |
4464 KB |
Output is correct |
61 |
Correct |
141 ms |
4408 KB |
Output is correct |
62 |
Correct |
137 ms |
4456 KB |
Output is correct |
63 |
Correct |
181 ms |
4432 KB |
Output is correct |
64 |
Correct |
141 ms |
4716 KB |
Output is correct |
65 |
Correct |
153 ms |
4700 KB |
Output is correct |
66 |
Correct |
184 ms |
4640 KB |
Output is correct |
67 |
Correct |
16 ms |
4160 KB |
Output is correct |
68 |
Correct |
26 ms |
5300 KB |
Output is correct |
69 |
Correct |
27 ms |
5196 KB |
Output is correct |
70 |
Correct |
31 ms |
5580 KB |
Output is correct |
71 |
Correct |
30 ms |
5300 KB |
Output is correct |
72 |
Correct |
29 ms |
5392 KB |
Output is correct |
73 |
Correct |
29 ms |
5516 KB |
Output is correct |
74 |
Correct |
37 ms |
5452 KB |
Output is correct |