#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
const int INF = INT_MAX;
const int LNF = 1e18;
const int MOD = 1000000007;
const int mod = 998244353;
const double eps = 1e-12;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
const int VMAX = 210;
const int EMAX = 50010;
int n, m;
int from[EMAX], to[EMAX], c[EMAX], d[EMAX]; // information of edges
vi edge[VMAX];
vi spe; // edges on shortest path s -> t
bool isspe[EMAX];
int dis1[VMAX][VMAX], dis2[VMAX][VMAX]; // dis2 : dis. without using edges in sp
int res[EMAX]; // res[i] = optimal answer if we choose to reverse edge i
void Dijkstra(int s, int t){
int disDij[VMAX];
bool vis[VMAX];
int pv[VMAX], pe[VMAX]; // id. of previous vertex, edge
FOR(i, 1, n, 1){
disDij[i] = LNF;
vis[i] = 0;
}
disDij[s] = 0;
FOR(owo, 1, n, 1){
int Min = LNF, u = s;
FOR(i, 1, n, 1){
if(vis[i] == 0 and Min > disDij[i]){
Min = disDij[i];
u = i;
}
}
for(int e : edge[u]){
int v = to[e];
if(disDij[u] + c[e] < disDij[v]){
disDij[v] = disDij[u] + c[e];
pv[v] = u;
pe[v] = e;
}
}
vis[u] = 1;
}
// find shortest path s -> t
spe.clear();
FOR(i, 1, m, 1){
isspe[i] = 0;
}
if(disDij[t] >= LNF) return;
int ptr = t;
while(ptr != s){
spe.pb(pe[ptr]);
isspe[pe[ptr]] = 1;
ptr = pv[ptr];
}
reverse(spe.begin(), spe.end());
}
void FloydWarshall(){
// init. dis1, dis2
FOR(i, 1, n, 1){
FOR(j, 1, n, 1){
dis1[i][j] = dis2[i][j] = LNF;
}
dis1[i][i] = dis2[i][i] = 0;
}
FOR(e, 1, m, 1){
int u = from[e], v = to[e];
dis1[u][v] = min(dis1[u][v], c[e]);
if(!isspe[e]) dis2[u][v] = min(dis2[u][v], c[e]);
}
FOR(k, 1, n, 1){
FOR(i, 1, n, 1){
FOR(j, 1, n, 1){
dis1[i][j] = min(dis1[i][j], dis1[i][k] + dis1[k][j]);
dis2[i][j] = min(dis2[i][j], dis2[i][k] + dis2[k][j]);
}
}
}
}
void solve(int s, int t){
// (s, t) = (1, n) or (n, 1)
Dijkstra(s, t);
FloydWarshall();
// e not on sp
FOR(e, 1, m, 1){
if(isspe[e]) continue;
int u = from[e], v = to[e];
res[e] += min(dis1[s][t], dis1[s][v] + c[e] + dis1[u][t]);
}
// e on sp
int sz = szof(spe);
FOR(i, 0, sz-1, 1){
int e = spe[i];
int Min = LNF;
FOR(j, 0, i, 1){
FOR(k, i, sz-1, 1){
int u = from[spe[j]], v = to[spe[k]];
Min = min(Min, dis1[s][u] + dis2[u][v] + dis1[v][t]);
}
}
res[e] += Min;
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
FOR(i, 1, m, 1){
cin>>from[i]>>to[i]>>c[i]>>d[i];
edge[from[i]].pb(i);
}
solve(1, n);
solve(n, 1);
int RES = dis1[1][n] + dis1[n][1]; // don't flip edges
FOR(e, 1, m, 1){
res[e] += d[e];
RES = min(RES, res[e]); // flip e
}
if(RES < LNF) cout<<RES<<'\n';
else cout<<-1<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
972 KB |
Output is correct |
2 |
Correct |
34 ms |
1012 KB |
Output is correct |
3 |
Correct |
31 ms |
972 KB |
Output is correct |
4 |
Correct |
30 ms |
984 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
29 ms |
972 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 |
388 KB |
Output is correct |
10 |
Correct |
40 ms |
984 KB |
Output is correct |
11 |
Correct |
36 ms |
980 KB |
Output is correct |
12 |
Correct |
37 ms |
972 KB |
Output is correct |
13 |
Correct |
30 ms |
1068 KB |
Output is correct |
14 |
Correct |
30 ms |
980 KB |
Output is correct |
15 |
Correct |
30 ms |
984 KB |
Output is correct |
16 |
Correct |
37 ms |
1092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
4336 KB |
Output is correct |
2 |
Correct |
54 ms |
4336 KB |
Output is correct |
3 |
Correct |
54 ms |
4328 KB |
Output is correct |
4 |
Correct |
33 ms |
1104 KB |
Output is correct |
5 |
Correct |
30 ms |
972 KB |
Output is correct |
6 |
Correct |
29 ms |
964 KB |
Output is correct |
7 |
Correct |
33 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
51 ms |
4392 KB |
Output is correct |
10 |
Correct |
50 ms |
4432 KB |
Output is correct |
11 |
Correct |
53 ms |
4432 KB |
Output is correct |
12 |
Correct |
52 ms |
4440 KB |
Output is correct |
13 |
Correct |
49 ms |
4476 KB |
Output is correct |
14 |
Correct |
52 ms |
4604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
1068 KB |
Output is correct |
2 |
Correct |
33 ms |
1012 KB |
Output is correct |
3 |
Correct |
47 ms |
3784 KB |
Output is correct |
4 |
Correct |
30 ms |
972 KB |
Output is correct |
5 |
Correct |
48 ms |
4456 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
48 ms |
4536 KB |
Output is correct |
9 |
Correct |
46 ms |
4548 KB |
Output is correct |
10 |
Correct |
48 ms |
4512 KB |
Output is correct |
11 |
Correct |
49 ms |
4548 KB |
Output is correct |
12 |
Correct |
45 ms |
4560 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 |
332 KB |
Output is correct |
19 |
Correct |
49 ms |
4600 KB |
Output is correct |
20 |
Correct |
48 ms |
4572 KB |
Output is correct |
21 |
Correct |
49 ms |
4684 KB |
Output is correct |
22 |
Correct |
52 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
972 KB |
Output is correct |
2 |
Correct |
34 ms |
1012 KB |
Output is correct |
3 |
Correct |
31 ms |
972 KB |
Output is correct |
4 |
Correct |
30 ms |
984 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
29 ms |
972 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 |
388 KB |
Output is correct |
10 |
Correct |
40 ms |
984 KB |
Output is correct |
11 |
Correct |
36 ms |
980 KB |
Output is correct |
12 |
Correct |
37 ms |
972 KB |
Output is correct |
13 |
Correct |
30 ms |
1068 KB |
Output is correct |
14 |
Correct |
30 ms |
980 KB |
Output is correct |
15 |
Correct |
30 ms |
984 KB |
Output is correct |
16 |
Correct |
37 ms |
1092 KB |
Output is correct |
17 |
Correct |
53 ms |
4336 KB |
Output is correct |
18 |
Correct |
54 ms |
4336 KB |
Output is correct |
19 |
Correct |
54 ms |
4328 KB |
Output is correct |
20 |
Correct |
33 ms |
1104 KB |
Output is correct |
21 |
Correct |
30 ms |
972 KB |
Output is correct |
22 |
Correct |
29 ms |
964 KB |
Output is correct |
23 |
Correct |
33 ms |
1024 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
51 ms |
4392 KB |
Output is correct |
26 |
Correct |
50 ms |
4432 KB |
Output is correct |
27 |
Correct |
53 ms |
4432 KB |
Output is correct |
28 |
Correct |
52 ms |
4440 KB |
Output is correct |
29 |
Correct |
49 ms |
4476 KB |
Output is correct |
30 |
Correct |
52 ms |
4604 KB |
Output is correct |
31 |
Correct |
45 ms |
1068 KB |
Output is correct |
32 |
Correct |
33 ms |
1012 KB |
Output is correct |
33 |
Correct |
47 ms |
3784 KB |
Output is correct |
34 |
Correct |
30 ms |
972 KB |
Output is correct |
35 |
Correct |
48 ms |
4456 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
328 KB |
Output is correct |
38 |
Correct |
48 ms |
4536 KB |
Output is correct |
39 |
Correct |
46 ms |
4548 KB |
Output is correct |
40 |
Correct |
48 ms |
4512 KB |
Output is correct |
41 |
Correct |
49 ms |
4548 KB |
Output is correct |
42 |
Correct |
45 ms |
4560 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 |
332 KB |
Output is correct |
49 |
Correct |
49 ms |
4600 KB |
Output is correct |
50 |
Correct |
48 ms |
4572 KB |
Output is correct |
51 |
Correct |
49 ms |
4684 KB |
Output is correct |
52 |
Correct |
52 ms |
4432 KB |
Output is correct |
53 |
Correct |
49 ms |
4648 KB |
Output is correct |
54 |
Correct |
52 ms |
4564 KB |
Output is correct |
55 |
Correct |
50 ms |
4600 KB |
Output is correct |
56 |
Correct |
33 ms |
1076 KB |
Output is correct |
57 |
Correct |
30 ms |
1028 KB |
Output is correct |
58 |
Correct |
66 ms |
3908 KB |
Output is correct |
59 |
Correct |
73 ms |
3968 KB |
Output is correct |
60 |
Correct |
50 ms |
3792 KB |
Output is correct |
61 |
Correct |
47 ms |
3868 KB |
Output is correct |
62 |
Correct |
52 ms |
3844 KB |
Output is correct |
63 |
Correct |
54 ms |
3772 KB |
Output is correct |
64 |
Correct |
57 ms |
3912 KB |
Output is correct |
65 |
Correct |
53 ms |
4036 KB |
Output is correct |
66 |
Correct |
50 ms |
3908 KB |
Output is correct |
67 |
Correct |
21 ms |
3140 KB |
Output is correct |
68 |
Correct |
63 ms |
4804 KB |
Output is correct |
69 |
Correct |
50 ms |
4836 KB |
Output is correct |
70 |
Correct |
51 ms |
4796 KB |
Output is correct |
71 |
Correct |
52 ms |
4676 KB |
Output is correct |
72 |
Correct |
57 ms |
4756 KB |
Output is correct |
73 |
Correct |
54 ms |
4776 KB |
Output is correct |
74 |
Correct |
48 ms |
4728 KB |
Output is correct |