#include <bits/stdc++.h>
using namespace std;
#define scd(t) scanf("%d", &t)
#define sclld(t) scanf("%lld", &t)
#define forr(i, j, k) for (int i = j; i < k; i++)
#define frange(i, j) forr(i, 0, j)
#define all(cont) cont.begin(), cont.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long long int lli;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<lli> vll;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int, int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long double ld;
void usaco()
{
freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
// freopen("problem.out", "w", stdout);
}
struct Edge {
int u, v, c, d;
};
int main() {
// usaco();
int n, m;
scd(n);
scd(m);
vector<Edge> vec(m);
vector<seti> graph(n+1);
vector<vll> dist(n+1, vll(n+1, 1e17));
forr(i, 1, n+1) dist[i][i] = 0;
frange(i, m) {
int u, v, c, d;
scd(u);
scd(v);
scd(c);
scd(d);
vec[i].u = u;
vec[i].v = v;
vec[i].c = c;
vec[i].d = d;
dist[u][v] = min(dist[u][v], (lli)c);
graph[u].insert(i);
}
forr(k, 1, n+1) {
forr(i, 1, n+1) {
forr(j, 1, n+1) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
// vb inpath(m);
// queue<pii> q;
// q.push(mp(1, -10));
// vi par(n+1, -1);
// if(dist[1][n] < 1e16)
// {
// while(q.size()) {
// auto p = q.front();
// q.pop();
// if(par[p.f] != -1) continue;
// par[p.f] = p.s;
// if(p.f == n) {
// int curr = n;
// while(curr != 1) {
// // printf("%d ", curr);
// inpath[par[curr]] = true;
// curr = vec[par[curr]].u;
// }
// }
// for(auto e : graph[p.f]) {
// if(dist[1][vec[e].u] + vec[e].c + dist[vec[e].v][n] == dist[1][n]) {
// q.push(mp(vec[e].v, e));
// }
// }
// }
// }
lli mi = dist[1][n] + dist[n][1];
// frange(i, m) {
// auto p = vec[i];
// lli v = dist[n][p.v] + dist[p.u][1] + p.c + p.d;
// if(v + dist[1][n] < mi) {
// graph[p.u].erase(i);
// graph[p.v].insert(i);
// swap(vec[i].u, vec[i].v);
// vll dist2(n+1, 1e16);
// priority_queue<pair<lli, int>> pq;
// pq.push(mp(0, 1));
// dist2[1] = 0;
// vb vis(n+1);
// while(pq.size()) {
// auto p = pq.top();
// pq.pop();
// if(vis[p.s]) continue;
// vis[p.s] = true;
// for(auto j : graph[p.s]) {
// auto e = vec[j];
// if(dist2[p.s] + e.c < dist2[e.v]) {
// dist2[e.v] = dist2[p.s] + e.c;
// pq.push(mp(-dist2[e.v], e.v));
// }
// }
// }
// v += dist2[n];
// graph[p.v].erase(i);
// graph[p.u].insert(i);
// mi = min(mi, v);
// swap(vec[i].u, vec[i].v);
// }
// // else {
// // v += dist[1][n];
// // }
// // mi = min(mi, v);
// }
// inpath = vb(m);
// q = {};
// q.push(mp(n, -10));
// par = vi(n+1, -1);
// if(dist[n][1] < 1e16)
// {
// while(q.size()) {
// auto p = q.front();
// q.pop();
// if(par[p.f] != -1) continue;
// par[p.f] = p.s;
// if(p.f == 1) {
// int curr = 1;
// while(curr != n) {
// // printf("%d ", curr);
// inpath[par[curr]] = true;
// curr = vec[par[curr]].u;
// }
// }
// for(auto e : graph[p.f]) {
// if(dist[n][vec[e].u] + vec[e].c + dist[vec[e].v][1] == dist[n][1]) {
// q.push(mp(vec[e].v, e));
// }
// }
// }
// }
frange(i, m) {
auto p = vec[i];
// lli v = min(dist[1][n], dist[1][p.v] + dist[p.u][n] + p.c) + p.d;
lli d1 = min(dist[1][n], dist[1][p.v] + dist[p.u][n] + p.c);
lli d2 = min(dist[n][1], dist[n][p.v] + dist[p.u][1] + p.c);
if(d1 + d2 + p.d < mi) {
lli v = p.d;
graph[p.u].erase(i);
graph[p.v].insert(i);
swap(vec[i].u, vec[i].v);
vll dist2(n+1, 1e16);
priority_queue<pair<lli, int>> pq;
pq.push(mp(0, n));
dist2[n] = 0;
vb vis(n+1);
while(pq.size()) {
auto p = pq.top();
pq.pop();
if(vis[p.s]) continue;
vis[p.s] = true;
for(auto j : graph[p.s]) {
auto e = vec[j];
if(dist2[p.s] + e.c < dist2[e.v]) {
dist2[e.v] = dist2[p.s] + e.c;
pq.push(mp(-dist2[e.v], e.v));
}
}
}
v += dist2[1];
dist2 = vll(n+1, 1e16);
pq.push(mp(0, 1));
dist2[1] = 0;
vis=vb(n+1);
while(pq.size()) {
auto p = pq.top();
pq.pop();
if(vis[p.s]) continue;
vis[p.s] = true;
for(auto j : graph[p.s]) {
auto e = vec[j];
if(dist2[p.s] + e.c < dist2[e.v]) {
dist2[e.v] = dist2[p.s] + e.c;
pq.push(mp(-dist2[e.v], e.v));
}
}
}
v += dist2[n];
graph[p.v].erase(i);
graph[p.u].insert(i);
mi = min(mi, v);
swap(vec[i].u, vec[i].v);
}
// else {
// v += dist[n][1];
// }
// mi = min(mi, v);
}
// frange(i, m) {
// auto p = vec[i];
// lli v = dist[1][p.v] + dist[p.u][n] + p.c + p.d + dist[n][p.v] + dist[p.u][1] + p.c;
// mi = min(mi, v);
// }
if(mi < 1e16) printf("%lld", mi);
else printf("-1");
}
Compilation message
ho_t4.cpp: In function 'void usaco()':
ho_t4.cpp:30:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
ho_t4.cpp:41:5: note: in expansion of macro 'scd'
41 | scd(n);
| ^~~
ho_t4.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
ho_t4.cpp:42:5: note: in expansion of macro 'scd'
42 | scd(m);
| ^~~
ho_t4.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
ho_t4.cpp:54:9: note: in expansion of macro 'scd'
54 | scd(u);
| ^~~
ho_t4.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
ho_t4.cpp:55:9: note: in expansion of macro 'scd'
55 | scd(v);
| ^~~
ho_t4.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
ho_t4.cpp:56:9: note: in expansion of macro 'scd'
56 | scd(c);
| ^~~
ho_t4.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
ho_t4.cpp:57:9: note: in expansion of macro 'scd'
57 | scd(d);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
784 KB |
Output is correct |
3 |
Correct |
7 ms |
604 KB |
Output is correct |
4 |
Correct |
7 ms |
760 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
780 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
10 ms |
820 KB |
Output is correct |
11 |
Correct |
20 ms |
604 KB |
Output is correct |
12 |
Correct |
8 ms |
828 KB |
Output is correct |
13 |
Correct |
8 ms |
600 KB |
Output is correct |
14 |
Correct |
7 ms |
604 KB |
Output is correct |
15 |
Correct |
7 ms |
860 KB |
Output is correct |
16 |
Correct |
8 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
3676 KB |
Output is correct |
2 |
Correct |
30 ms |
3676 KB |
Output is correct |
3 |
Correct |
29 ms |
3676 KB |
Output is correct |
4 |
Correct |
8 ms |
860 KB |
Output is correct |
5 |
Correct |
7 ms |
824 KB |
Output is correct |
6 |
Correct |
7 ms |
796 KB |
Output is correct |
7 |
Correct |
7 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
47 ms |
3756 KB |
Output is correct |
10 |
Correct |
37 ms |
3932 KB |
Output is correct |
11 |
Correct |
31 ms |
3672 KB |
Output is correct |
12 |
Correct |
31 ms |
3676 KB |
Output is correct |
13 |
Correct |
30 ms |
3932 KB |
Output is correct |
14 |
Correct |
31 ms |
3820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
784 KB |
Output is correct |
3 |
Correct |
25 ms |
3004 KB |
Output is correct |
4 |
Correct |
7 ms |
604 KB |
Output is correct |
5 |
Correct |
34 ms |
3888 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
52 ms |
3736 KB |
Output is correct |
9 |
Correct |
55 ms |
4700 KB |
Output is correct |
10 |
Correct |
37 ms |
4684 KB |
Output is correct |
11 |
Correct |
32 ms |
4956 KB |
Output is correct |
12 |
Correct |
33 ms |
4620 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
440 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
40 ms |
5136 KB |
Output is correct |
20 |
Correct |
33 ms |
4688 KB |
Output is correct |
21 |
Correct |
32 ms |
4696 KB |
Output is correct |
22 |
Correct |
32 ms |
4728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
784 KB |
Output is correct |
3 |
Correct |
7 ms |
604 KB |
Output is correct |
4 |
Correct |
7 ms |
760 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
780 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
10 ms |
820 KB |
Output is correct |
11 |
Correct |
20 ms |
604 KB |
Output is correct |
12 |
Correct |
8 ms |
828 KB |
Output is correct |
13 |
Correct |
8 ms |
600 KB |
Output is correct |
14 |
Correct |
7 ms |
604 KB |
Output is correct |
15 |
Correct |
7 ms |
860 KB |
Output is correct |
16 |
Correct |
8 ms |
860 KB |
Output is correct |
17 |
Correct |
28 ms |
3676 KB |
Output is correct |
18 |
Correct |
30 ms |
3676 KB |
Output is correct |
19 |
Correct |
29 ms |
3676 KB |
Output is correct |
20 |
Correct |
8 ms |
860 KB |
Output is correct |
21 |
Correct |
7 ms |
824 KB |
Output is correct |
22 |
Correct |
7 ms |
796 KB |
Output is correct |
23 |
Correct |
7 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
47 ms |
3756 KB |
Output is correct |
26 |
Correct |
37 ms |
3932 KB |
Output is correct |
27 |
Correct |
31 ms |
3672 KB |
Output is correct |
28 |
Correct |
31 ms |
3676 KB |
Output is correct |
29 |
Correct |
30 ms |
3932 KB |
Output is correct |
30 |
Correct |
31 ms |
3820 KB |
Output is correct |
31 |
Correct |
8 ms |
604 KB |
Output is correct |
32 |
Correct |
7 ms |
784 KB |
Output is correct |
33 |
Correct |
25 ms |
3004 KB |
Output is correct |
34 |
Correct |
7 ms |
604 KB |
Output is correct |
35 |
Correct |
34 ms |
3888 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
52 ms |
3736 KB |
Output is correct |
39 |
Correct |
55 ms |
4700 KB |
Output is correct |
40 |
Correct |
37 ms |
4684 KB |
Output is correct |
41 |
Correct |
32 ms |
4956 KB |
Output is correct |
42 |
Correct |
33 ms |
4620 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
600 KB |
Output is correct |
46 |
Correct |
0 ms |
440 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
40 ms |
5136 KB |
Output is correct |
50 |
Correct |
33 ms |
4688 KB |
Output is correct |
51 |
Correct |
32 ms |
4696 KB |
Output is correct |
52 |
Correct |
32 ms |
4728 KB |
Output is correct |
53 |
Correct |
30 ms |
4700 KB |
Output is correct |
54 |
Correct |
30 ms |
4764 KB |
Output is correct |
55 |
Correct |
30 ms |
4688 KB |
Output is correct |
56 |
Correct |
7 ms |
600 KB |
Output is correct |
57 |
Correct |
7 ms |
604 KB |
Output is correct |
58 |
Correct |
33 ms |
4108 KB |
Output is correct |
59 |
Correct |
32 ms |
3924 KB |
Output is correct |
60 |
Correct |
35 ms |
3992 KB |
Output is correct |
61 |
Correct |
35 ms |
3928 KB |
Output is correct |
62 |
Correct |
137 ms |
3932 KB |
Output is correct |
63 |
Correct |
290 ms |
3928 KB |
Output is correct |
64 |
Correct |
87 ms |
4496 KB |
Output is correct |
65 |
Correct |
223 ms |
4524 KB |
Output is correct |
66 |
Correct |
415 ms |
4492 KB |
Output is correct |
67 |
Correct |
23 ms |
3676 KB |
Output is correct |
68 |
Correct |
46 ms |
4956 KB |
Output is correct |
69 |
Correct |
41 ms |
4956 KB |
Output is correct |
70 |
Correct |
32 ms |
4948 KB |
Output is correct |
71 |
Correct |
34 ms |
4948 KB |
Output is correct |
72 |
Correct |
32 ms |
4944 KB |
Output is correct |
73 |
Correct |
32 ms |
4956 KB |
Output is correct |
74 |
Correct |
34 ms |
5048 KB |
Output is correct |