#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 100005;
const ll inf = 1e18 + 5;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
int n, m;
array<int,4> niz[maxn];
bool used[maxn];
const int N = 205;
vector<int> g[N];
vector<int> rg[N];
ll dist[2][N]; // 0 - 1, 1 - n
int from[2][N]; // 0 - 1, 1 - n
void dij1(int s, ll dist[], int from[]){
ff(i,1,n)dist[i] = inf;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({dist[s] = 0, s});
while(!pq.empty()){
pii v = pq.top(); pq.pop();
if(dist[v.se] < v.fi)continue;
for(int c : g[v.se]){
int u = niz[c][1];
int w = niz[c][2];
if(v.se != niz[c][0])continue;
if(dist[u] > w + v.fi){
pq.push({dist[u] = w + v.fi, u});
from[u] = c;
}
}
}
}
ll rdist[2][N]; // 0 - 1, 1 - n
void dij2(int s, ll dist[]){
ff(i,1,n)dist[i] = inf;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({dist[s] = 0, s});
while(!pq.empty()){
pii v = pq.top(); pq.pop();
if(dist[v.se] < v.fi)continue;
for(int c : rg[v.se]){
int u = niz[c][0];
int w = niz[c][2];
if(dist[u] > w + v.fi){
pq.push({dist[u] = w + v.fi, u});
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
cin >> n >> m;
ff(i,1,m){
int u, v, c, d;
cin >> u >> v >> c >> d;
g[u].pb(i);
rg[v].pb(i);
niz[i] = {u, v, c, d};
}
dij1(1, dist[0], from[0]), dij1(n, dist[1], from[1]);
dij2(1, rdist[0]), dij2(n, rdist[1]);
int cur = n;
while(from[0][cur] && cur != 1){
used[from[0][cur]] = 1;
cur = niz[from[0][cur]][0];
}
cur = 1;
while(from[1][cur] && cur != n){
used[from[1][cur]] = 1;
cur = niz[from[1][cur]][0];
}
ll rez = inf;
rez = min(rez, dist[0][n] + dist[1][1]);
ff(i,1,m){
int u = niz[i][0];
int v = niz[i][1];
int c = niz[i][2];
int d = niz[i][3];
if(!used[i]){
ll A = min(dist[0][n], dist[0][v] + c + rdist[1][u]);
ll B = min(dist[1][1], dist[1][v] + c + rdist[0][u]);
rez = min(rez, A + B + d);
}
}
ff(i,1,m){
int u = niz[i][0];
int v = niz[i][1];
int c = niz[i][2];
int d = niz[i][3];
if(used[i]){
swap(niz[i][0], niz[i][1]);
g[v].pb(i);
dij1(1, dist[0], from[0]), dij1(n, dist[1], from[1]);
g[v].pop_back();
swap(niz[i][0], niz[i][1]);
rez = min(rez, dist[0][n] + d + dist[1][1]);
}
}
cout << (rez == inf ? -1 : rez);
return 0;
}
/**
4 4
1 2 0 4
1 3 0 1
4 3 0 2
4 1 0 1
4 4
1 2 1 0
2 3 1 0
2 4 3 0
3 4 2 0
// probati bojenje sahovski ili slicno
**/
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:127:13: warning: unused variable 'u' [-Wunused-variable]
127 | int u = niz[i][0];
| ^
ho_t4.cpp:129:13: warning: unused variable 'c' [-Wunused-variable]
129 | int c = niz[i][2];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
20 ms |
388 KB |
Output is correct |
11 |
Correct |
25 ms |
332 KB |
Output is correct |
12 |
Correct |
24 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
1728 KB |
Output is correct |
2 |
Correct |
27 ms |
1740 KB |
Output is correct |
3 |
Correct |
31 ms |
1868 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
23 ms |
1820 KB |
Output is correct |
10 |
Correct |
23 ms |
1716 KB |
Output is correct |
11 |
Correct |
28 ms |
1740 KB |
Output is correct |
12 |
Correct |
26 ms |
1828 KB |
Output is correct |
13 |
Correct |
27 ms |
1796 KB |
Output is correct |
14 |
Correct |
27 ms |
1824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
18 ms |
1420 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
22 ms |
1712 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
21 ms |
1720 KB |
Output is correct |
9 |
Correct |
21 ms |
1740 KB |
Output is correct |
10 |
Correct |
23 ms |
1816 KB |
Output is correct |
11 |
Correct |
22 ms |
1740 KB |
Output is correct |
12 |
Correct |
22 ms |
1780 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
23 ms |
1816 KB |
Output is correct |
20 |
Correct |
22 ms |
1740 KB |
Output is correct |
21 |
Correct |
23 ms |
1800 KB |
Output is correct |
22 |
Correct |
22 ms |
1740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
20 ms |
388 KB |
Output is correct |
11 |
Correct |
25 ms |
332 KB |
Output is correct |
12 |
Correct |
24 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
26 ms |
1728 KB |
Output is correct |
18 |
Correct |
27 ms |
1740 KB |
Output is correct |
19 |
Correct |
31 ms |
1868 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
23 ms |
1820 KB |
Output is correct |
26 |
Correct |
23 ms |
1716 KB |
Output is correct |
27 |
Correct |
28 ms |
1740 KB |
Output is correct |
28 |
Correct |
26 ms |
1828 KB |
Output is correct |
29 |
Correct |
27 ms |
1796 KB |
Output is correct |
30 |
Correct |
27 ms |
1824 KB |
Output is correct |
31 |
Correct |
2 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
18 ms |
1420 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
22 ms |
1712 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
21 ms |
1720 KB |
Output is correct |
39 |
Correct |
21 ms |
1740 KB |
Output is correct |
40 |
Correct |
23 ms |
1816 KB |
Output is correct |
41 |
Correct |
22 ms |
1740 KB |
Output is correct |
42 |
Correct |
22 ms |
1780 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
23 ms |
1816 KB |
Output is correct |
50 |
Correct |
22 ms |
1740 KB |
Output is correct |
51 |
Correct |
23 ms |
1800 KB |
Output is correct |
52 |
Correct |
22 ms |
1740 KB |
Output is correct |
53 |
Correct |
27 ms |
1748 KB |
Output is correct |
54 |
Correct |
31 ms |
1820 KB |
Output is correct |
55 |
Correct |
30 ms |
1784 KB |
Output is correct |
56 |
Correct |
2 ms |
332 KB |
Output is correct |
57 |
Correct |
2 ms |
332 KB |
Output is correct |
58 |
Correct |
127 ms |
1484 KB |
Output is correct |
59 |
Correct |
151 ms |
1484 KB |
Output is correct |
60 |
Correct |
210 ms |
1492 KB |
Output is correct |
61 |
Correct |
125 ms |
1480 KB |
Output is correct |
62 |
Correct |
153 ms |
1604 KB |
Output is correct |
63 |
Correct |
200 ms |
1484 KB |
Output is correct |
64 |
Correct |
761 ms |
1856 KB |
Output is correct |
65 |
Correct |
868 ms |
1848 KB |
Output is correct |
66 |
Execution timed out |
1052 ms |
1804 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |