#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define int long long
using namespace std;
const int N = 205;
const int M = 5e4 + 5;
const int oo = 1e18;
vector <int> adj[N];
vector <int> newadj[N];
pair <int, int> edge[M];
int dis[N][N];
int newdis[N][N];
int d[N];
int f[M];
int g[M];
int par[N];
int cost[M];
int revcost[M];
int n,m;
bool vis[N];
bool flag[M];
template <typename T>
bool mini(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
void dfs(int u, int p) {
par[u] = p;
for (int id : newadj[u]) {
int v = edge[id].se;
d[v] = d[u] + 1;
dfs(v, id);
}
}
void solve(int f[], int s, int t) {
for (int i = 1; i <= n; i++) {
vis[i] = false;
newadj[i].clear();
}
for (int i = 1; i <= m; i++)
flag[i] = false;
queue <int> q({s});
vis[s] = true;
while (q.size()) {
int u = q.front();
q.pop();
for (int id : adj[u]) {
int v = edge[id].se;
if (!vis[v] && dis[s][u] + cost[id] == dis[s][v]) {
vis[v] = true;
q.push(v);
newadj[u].push_back(id);
//cerr << s << " " << t << " " << id << "\n";
}
}
}
if (!vis[t]) {
for (int i = 1; i <= m; i++)
f[i] = oo;
return;
}
d[s] = 0;
dfs(s, -1);
vector <int> node;
for (int u = t; u != s; u = edge[par[u]].fi) {
node.push_back(u);
flag[par[u]] = true;
}
node.push_back(s);
reverse(node.begin(), node.end());
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j)
newdis[i][j] = oo;
for (int i = 1; i <= m; i++) {
if (!flag[i]) {
mini(newdis[edge[i].fi][edge[i].se], cost[i]);
f[i] = dis[s][t];
} else
f[i] = oo;
}
for (int j = 1; j <= n; j++)
for (int i = 1; i <= n; i++)
for (int k = 1; k <= n; k++)
mini(newdis[i][k], newdis[i][j] + newdis[j][k]);
for (int l = 0; l < (int) node.size(); l++)
for (int r = l + 1; r < (int) node.size(); r++) {
int u = node[l];
int v = node[r];
int res = dis[s][u] + newdis[u][v] + dis[v][t];
for (; v != u; v = edge[par[v]].fi)
mini(f[par[v]], res);
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> edge[i].fi >> edge[i].se >> cost[i] >> revcost[i];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j)
dis[i][j] = oo;
for (int i = 1; i <= m; i++) {
int u = edge[i].fi;
int v = edge[i].se;
mini(dis[u][v], cost[i]);
adj[u].push_back(i);
}
for (int j = 1; j <= n; j++)
for (int i = 1; i <= n; i++)
for (int k = 1; k <= n; k++)
mini(dis[i][k], dis[i][j] + dis[j][k]);
// for (int i = 1; i <= n; i++)
// for (int j = 1; j <= n; j++)
// cerr << i << " " << j << " " << dis[i][j] << "\n"
int ans = 2 * oo;
if (dis[1][n] < oo && dis[n][1] < oo)
ans = min(ans, dis[1][n] + dis[n][1]);
solve(f, 1, n);
solve(g, n, 1);
// for (int i = 1; i <= m; i++)
// cerr << f[i] << " \n"[i == m];
// for (int i = 1; i <= m; i++)
// cerr << g[i] << " \n"[i == m];
for (int i = 1; i <= m; i++) {
int u = edge[i].fi;
int v = edge[i].se;
int l = f[i];
int r = g[i];
if (dis[1][n] > dis[1][v] + dis[u][n] + cost[i])
l = dis[1][v] + dis[u][n] + cost[i];
if (dis[n][1] > dis[n][v] + dis[u][1] + cost[i])
r = dis[n][v] + dis[u][1] + cost[i];
// cerr << i << " " << l << " " << r << "\n";
mini(ans, l + r + revcost[i]);
}
if (ans > 1e17)
ans = -1;
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
972 KB |
Output is correct |
2 |
Correct |
11 ms |
688 KB |
Output is correct |
3 |
Correct |
42 ms |
1176 KB |
Output is correct |
4 |
Correct |
43 ms |
1060 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
11 ms |
688 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 |
53 ms |
1072 KB |
Output is correct |
11 |
Correct |
53 ms |
1064 KB |
Output is correct |
12 |
Correct |
52 ms |
1056 KB |
Output is correct |
13 |
Correct |
14 ms |
716 KB |
Output is correct |
14 |
Correct |
24 ms |
1072 KB |
Output is correct |
15 |
Correct |
25 ms |
972 KB |
Output is correct |
16 |
Correct |
26 ms |
996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
4972 KB |
Output is correct |
2 |
Correct |
68 ms |
5092 KB |
Output is correct |
3 |
Correct |
71 ms |
5224 KB |
Output is correct |
4 |
Correct |
41 ms |
1156 KB |
Output is correct |
5 |
Correct |
37 ms |
972 KB |
Output is correct |
6 |
Correct |
10 ms |
696 KB |
Output is correct |
7 |
Correct |
9 ms |
688 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
35 ms |
3804 KB |
Output is correct |
10 |
Correct |
35 ms |
3756 KB |
Output is correct |
11 |
Correct |
49 ms |
4164 KB |
Output is correct |
12 |
Correct |
49 ms |
4144 KB |
Output is correct |
13 |
Correct |
50 ms |
4224 KB |
Output is correct |
14 |
Correct |
50 ms |
4160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
1088 KB |
Output is correct |
2 |
Correct |
27 ms |
1040 KB |
Output is correct |
3 |
Correct |
53 ms |
3244 KB |
Output is correct |
4 |
Correct |
9 ms |
588 KB |
Output is correct |
5 |
Correct |
58 ms |
4028 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
31 ms |
3640 KB |
Output is correct |
9 |
Correct |
31 ms |
3616 KB |
Output is correct |
10 |
Correct |
45 ms |
4012 KB |
Output is correct |
11 |
Correct |
44 ms |
4032 KB |
Output is correct |
12 |
Correct |
45 ms |
4052 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 |
44 ms |
4104 KB |
Output is correct |
20 |
Correct |
44 ms |
3932 KB |
Output is correct |
21 |
Correct |
44 ms |
4076 KB |
Output is correct |
22 |
Correct |
44 ms |
3996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
972 KB |
Output is correct |
2 |
Correct |
11 ms |
688 KB |
Output is correct |
3 |
Correct |
42 ms |
1176 KB |
Output is correct |
4 |
Correct |
43 ms |
1060 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
11 ms |
688 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 |
53 ms |
1072 KB |
Output is correct |
11 |
Correct |
53 ms |
1064 KB |
Output is correct |
12 |
Correct |
52 ms |
1056 KB |
Output is correct |
13 |
Correct |
14 ms |
716 KB |
Output is correct |
14 |
Correct |
24 ms |
1072 KB |
Output is correct |
15 |
Correct |
25 ms |
972 KB |
Output is correct |
16 |
Correct |
26 ms |
996 KB |
Output is correct |
17 |
Correct |
68 ms |
4972 KB |
Output is correct |
18 |
Correct |
68 ms |
5092 KB |
Output is correct |
19 |
Correct |
71 ms |
5224 KB |
Output is correct |
20 |
Correct |
41 ms |
1156 KB |
Output is correct |
21 |
Correct |
37 ms |
972 KB |
Output is correct |
22 |
Correct |
10 ms |
696 KB |
Output is correct |
23 |
Correct |
9 ms |
688 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
35 ms |
3804 KB |
Output is correct |
26 |
Correct |
35 ms |
3756 KB |
Output is correct |
27 |
Correct |
49 ms |
4164 KB |
Output is correct |
28 |
Correct |
49 ms |
4144 KB |
Output is correct |
29 |
Correct |
50 ms |
4224 KB |
Output is correct |
30 |
Correct |
50 ms |
4160 KB |
Output is correct |
31 |
Correct |
37 ms |
1088 KB |
Output is correct |
32 |
Correct |
27 ms |
1040 KB |
Output is correct |
33 |
Correct |
53 ms |
3244 KB |
Output is correct |
34 |
Correct |
9 ms |
588 KB |
Output is correct |
35 |
Correct |
58 ms |
4028 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
31 ms |
3640 KB |
Output is correct |
39 |
Correct |
31 ms |
3616 KB |
Output is correct |
40 |
Correct |
45 ms |
4012 KB |
Output is correct |
41 |
Correct |
44 ms |
4032 KB |
Output is correct |
42 |
Correct |
45 ms |
4052 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 |
44 ms |
4104 KB |
Output is correct |
50 |
Correct |
44 ms |
3932 KB |
Output is correct |
51 |
Correct |
44 ms |
4076 KB |
Output is correct |
52 |
Correct |
44 ms |
3996 KB |
Output is correct |
53 |
Correct |
71 ms |
4932 KB |
Output is correct |
54 |
Correct |
75 ms |
4936 KB |
Output is correct |
55 |
Correct |
78 ms |
4996 KB |
Output is correct |
56 |
Correct |
40 ms |
1100 KB |
Output is correct |
57 |
Correct |
40 ms |
972 KB |
Output is correct |
58 |
Correct |
80 ms |
4120 KB |
Output is correct |
59 |
Correct |
82 ms |
4152 KB |
Output is correct |
60 |
Correct |
84 ms |
4216 KB |
Output is correct |
61 |
Correct |
73 ms |
4164 KB |
Output is correct |
62 |
Correct |
81 ms |
4164 KB |
Output is correct |
63 |
Correct |
83 ms |
4100 KB |
Output is correct |
64 |
Correct |
83 ms |
4264 KB |
Output is correct |
65 |
Correct |
79 ms |
4220 KB |
Output is correct |
66 |
Correct |
79 ms |
4316 KB |
Output is correct |
67 |
Correct |
18 ms |
3532 KB |
Output is correct |
68 |
Correct |
35 ms |
4832 KB |
Output is correct |
69 |
Correct |
35 ms |
4816 KB |
Output is correct |
70 |
Correct |
49 ms |
5168 KB |
Output is correct |
71 |
Correct |
49 ms |
5168 KB |
Output is correct |
72 |
Correct |
50 ms |
5324 KB |
Output is correct |
73 |
Correct |
51 ms |
5200 KB |
Output is correct |
74 |
Correct |
52 ms |
5248 KB |
Output is correct |