#include <bits/stdc++.h>
#define int ll
using namespace std;
#define ll long long
#define y1 zck_is_king
#define pii pair<ll, ll>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif
const int iinf = 1e9+10;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9+7 ;
void GG(){cout<<"0\n"; exit(0);}
ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
ll re=1;
while (n>0){
if (n&1) re = re*a %mo;
a = a*a %mo;
n>>=1;
}
return re;
}
ll inv (ll b, ll mo = mod){
if (b==1) return b;
return (mo-mo/b) * inv(mo%b,mo) % mo;
}
const int NN = 2e2+5;
const int MM = 5e4+5;
int w[NN][NN];
int who[NN][NN];
int from[NN];
int treeedge[MM];
bool done[NN];
ll dst[NN];
ll SD[NN], TD[NN]; // distances from the start and end, respectively
ll TMP[NN];
struct edge{
int u, v, w, fost, id; // fost = flipping cost
};
edge E[MM];
int n,m;
ll dij(/*vector<edge> &edges, */ll *d, int S, int T, bool tree) {
memset(w, 0x3f, sizeof w);
bug("yart");
REP(i,m) {
edge e = E[i];
if (w[e.u][e.v] > e.w) {
w[e.u][e.v] = e.w;
who[e.u][e.v] = e.id;
}
}
memset(done, 0, sizeof done);
memset(d, 0x3f, sizeof SD);
d[S] = 0;
while (1) {
int v = -1; ll bad = 0x3f3f3f3f3f3f3f3f;
REP(i,n) {
if (!done[i] && d[i] < bad) {
bad = d[i]; v = i;
}
}
if(v == -1) break;
done[v] = 1;
REP(u, n) {
if (!done[u] && d[u] > d[v] + w[v][u]) {
d[u] = d[v] + w[v][u];
from[u] = v;
}
}
}
if (tree) {
REP(i,n) {
if (done[i] && i != S) {
int f = from[i];
treeedge[who[f][i]] = 1;
bug("Tree!", who[f][i], E[who[f][i]].u, E[who[f][i]].v);
}
}
}
return d[T];
}
ll floy[NN][NN];
signed main(){
IOS();
cin>>n>>m;
memset(floy, 0x3f, sizeof floy);
REP(i,m) {
cin>>E[i].u>>E[i].v>>E[i].w>>E[i].fost; --E[i].u; --E[i].v;
E[i].id = i;
MN(floy[E[i].u][E[i].v], E[i].w);
}
REP(i,n) floy[i][i] = 0;
REP(k,n) REP(i,n) REP(j,n) {
MN(floy[i][j], floy[i][k] + floy[k][j]);
}
ll ftrip = dij(SD, 0, n-1, 1);
ll btrip = dij(TD, n-1, 0, 1);
ll re = ftrip + btrip;
bug(re);
REP(i,m) {
// try flipping this bit
bug(i, treeedge[i]);
if (treeedge[i]) {
// ok, just brute it
swap(E[i].u, E[i].v);
ll go = dij(TMP, 0, n-1, 0);
ll bk = dij(TMP, n-1, 0, 0);
bug(go, bk);
MN(re, go+bk+E[i].fost);
swap(E[i].u, E[i].v);
}else{
int u = E[i].u, v = E[i].v;
ll newf;
if (SD[v] == inf) newf = ftrip;
else newf = SD[v] + floy[u][n-1] + E[i].w;
ll newb;
if (TD[v] == inf) newb = btrip;
else newb = TD[v] + floy[u][0] + E[i].w;
MN(newf, ftrip); MN(newb, btrip);
bug(newf, newb);
MN(re, newf + newb + E[i].fost);
}
}
if (re >= inf-10) re = -1;
cout<<re<<endl;
}
/*
// fear: what if T cannot be reached by v?????
2 2
1 2 7 3
1 2 4 8
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
1356 KB |
Output is correct |
2 |
Correct |
9 ms |
1300 KB |
Output is correct |
3 |
Correct |
119 ms |
1356 KB |
Output is correct |
4 |
Correct |
125 ms |
1336 KB |
Output is correct |
5 |
Correct |
3 ms |
1100 KB |
Output is correct |
6 |
Correct |
9 ms |
1304 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
0 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
97 ms |
1356 KB |
Output is correct |
11 |
Correct |
123 ms |
1356 KB |
Output is correct |
12 |
Correct |
121 ms |
1356 KB |
Output is correct |
13 |
Correct |
38 ms |
1356 KB |
Output is correct |
14 |
Correct |
84 ms |
1356 KB |
Output is correct |
15 |
Correct |
76 ms |
1336 KB |
Output is correct |
16 |
Correct |
74 ms |
1356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
274 ms |
3620 KB |
Output is correct |
2 |
Correct |
284 ms |
3524 KB |
Output is correct |
3 |
Correct |
287 ms |
4780 KB |
Output is correct |
4 |
Correct |
110 ms |
1408 KB |
Output is correct |
5 |
Correct |
88 ms |
1372 KB |
Output is correct |
6 |
Correct |
9 ms |
1228 KB |
Output is correct |
7 |
Correct |
10 ms |
1316 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
129 ms |
4856 KB |
Output is correct |
10 |
Correct |
124 ms |
4784 KB |
Output is correct |
11 |
Correct |
229 ms |
4776 KB |
Output is correct |
12 |
Correct |
239 ms |
4788 KB |
Output is correct |
13 |
Correct |
213 ms |
4752 KB |
Output is correct |
14 |
Correct |
208 ms |
4656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
1356 KB |
Output is correct |
2 |
Correct |
17 ms |
1228 KB |
Output is correct |
3 |
Correct |
341 ms |
3120 KB |
Output is correct |
4 |
Correct |
11 ms |
1228 KB |
Output is correct |
5 |
Correct |
377 ms |
3624 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
210 ms |
3564 KB |
Output is correct |
9 |
Correct |
218 ms |
3572 KB |
Output is correct |
10 |
Correct |
258 ms |
3504 KB |
Output is correct |
11 |
Correct |
251 ms |
3736 KB |
Output is correct |
12 |
Correct |
247 ms |
3592 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
264 ms |
3620 KB |
Output is correct |
20 |
Correct |
233 ms |
3532 KB |
Output is correct |
21 |
Correct |
243 ms |
3492 KB |
Output is correct |
22 |
Correct |
251 ms |
3568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
1356 KB |
Output is correct |
2 |
Correct |
9 ms |
1300 KB |
Output is correct |
3 |
Correct |
119 ms |
1356 KB |
Output is correct |
4 |
Correct |
125 ms |
1336 KB |
Output is correct |
5 |
Correct |
3 ms |
1100 KB |
Output is correct |
6 |
Correct |
9 ms |
1304 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
0 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
97 ms |
1356 KB |
Output is correct |
11 |
Correct |
123 ms |
1356 KB |
Output is correct |
12 |
Correct |
121 ms |
1356 KB |
Output is correct |
13 |
Correct |
38 ms |
1356 KB |
Output is correct |
14 |
Correct |
84 ms |
1356 KB |
Output is correct |
15 |
Correct |
76 ms |
1336 KB |
Output is correct |
16 |
Correct |
74 ms |
1356 KB |
Output is correct |
17 |
Correct |
274 ms |
3620 KB |
Output is correct |
18 |
Correct |
284 ms |
3524 KB |
Output is correct |
19 |
Correct |
287 ms |
4780 KB |
Output is correct |
20 |
Correct |
110 ms |
1408 KB |
Output is correct |
21 |
Correct |
88 ms |
1372 KB |
Output is correct |
22 |
Correct |
9 ms |
1228 KB |
Output is correct |
23 |
Correct |
10 ms |
1316 KB |
Output is correct |
24 |
Correct |
1 ms |
972 KB |
Output is correct |
25 |
Correct |
129 ms |
4856 KB |
Output is correct |
26 |
Correct |
124 ms |
4784 KB |
Output is correct |
27 |
Correct |
229 ms |
4776 KB |
Output is correct |
28 |
Correct |
239 ms |
4788 KB |
Output is correct |
29 |
Correct |
213 ms |
4752 KB |
Output is correct |
30 |
Correct |
208 ms |
4656 KB |
Output is correct |
31 |
Correct |
66 ms |
1356 KB |
Output is correct |
32 |
Correct |
17 ms |
1228 KB |
Output is correct |
33 |
Correct |
341 ms |
3120 KB |
Output is correct |
34 |
Correct |
11 ms |
1228 KB |
Output is correct |
35 |
Correct |
377 ms |
3624 KB |
Output is correct |
36 |
Correct |
1 ms |
972 KB |
Output is correct |
37 |
Correct |
1 ms |
972 KB |
Output is correct |
38 |
Correct |
210 ms |
3564 KB |
Output is correct |
39 |
Correct |
218 ms |
3572 KB |
Output is correct |
40 |
Correct |
258 ms |
3504 KB |
Output is correct |
41 |
Correct |
251 ms |
3736 KB |
Output is correct |
42 |
Correct |
247 ms |
3592 KB |
Output is correct |
43 |
Correct |
1 ms |
972 KB |
Output is correct |
44 |
Correct |
1 ms |
972 KB |
Output is correct |
45 |
Correct |
1 ms |
972 KB |
Output is correct |
46 |
Correct |
1 ms |
972 KB |
Output is correct |
47 |
Correct |
1 ms |
972 KB |
Output is correct |
48 |
Correct |
1 ms |
972 KB |
Output is correct |
49 |
Correct |
264 ms |
3620 KB |
Output is correct |
50 |
Correct |
233 ms |
3532 KB |
Output is correct |
51 |
Correct |
243 ms |
3492 KB |
Output is correct |
52 |
Correct |
251 ms |
3568 KB |
Output is correct |
53 |
Correct |
367 ms |
4628 KB |
Output is correct |
54 |
Correct |
406 ms |
4548 KB |
Output is correct |
55 |
Correct |
383 ms |
4532 KB |
Output is correct |
56 |
Correct |
130 ms |
1372 KB |
Output is correct |
57 |
Correct |
114 ms |
1368 KB |
Output is correct |
58 |
Correct |
236 ms |
3988 KB |
Output is correct |
59 |
Correct |
275 ms |
4008 KB |
Output is correct |
60 |
Correct |
355 ms |
3936 KB |
Output is correct |
61 |
Correct |
242 ms |
3976 KB |
Output is correct |
62 |
Correct |
286 ms |
3988 KB |
Output is correct |
63 |
Correct |
366 ms |
3984 KB |
Output is correct |
64 |
Correct |
238 ms |
3976 KB |
Output is correct |
65 |
Correct |
281 ms |
3976 KB |
Output is correct |
66 |
Correct |
370 ms |
3984 KB |
Output is correct |
67 |
Correct |
14 ms |
3276 KB |
Output is correct |
68 |
Correct |
212 ms |
4816 KB |
Output is correct |
69 |
Correct |
189 ms |
4676 KB |
Output is correct |
70 |
Correct |
337 ms |
4780 KB |
Output is correct |
71 |
Correct |
330 ms |
4804 KB |
Output is correct |
72 |
Correct |
335 ms |
4776 KB |
Output is correct |
73 |
Correct |
342 ms |
4904 KB |
Output is correct |
74 |
Correct |
326 ms |
4684 KB |
Output is correct |