#include "bits/stdc++.h"
using namespace std;
const int maxn = 205;
using ll = long long;
ll inf = 1e16;
struct E {
int x, id;
ll c, d;
E(int _x, ll _c, ll _d, int _id){
x = _x, c = _c, d = _d;
id = _id;
}
E(){}
};
vector<E> edges[maxn], g[maxn], rev[maxn];
ll dis[2][maxn];
int par[2][maxn];
vector<int> candidates;
int n, m;
void dijkstra(int id, int s){
for(int i = 1; i <= n; ++i)dis[id][i] = -1;
dis[id][s] = 0;
using pii = pair<ll, int>;
vector<bool> vis(n + 1);
while(1){
pii src = {inf, -1};
for(int i = 1; i <= n; ++i){
if(vis[i])continue;
if(dis[id][i] < src.first && dis[id][i] != -1){
src = {dis[id][i], i};
}
}
if(src.first == inf)break;
if(vis[src.second])continue;
vis[src.second] = 1;
for(auto i : edges[src.second]){
ll cost = dis[id][src.second] + i.c;
if(dis[id][i.x] > cost or dis[id][i.x] == -1){
dis[id][i.x] = cost;
par[id][i.x] = i.id;
}
}
}
}
ll dis1[2][maxn];
void dijkstra1(int id, int s){
for(int i = 1; i <= n; ++i)dis1[id][i] = -1;
dis1[id][s] = 0;
using pii = pair<ll, int>;
vector<bool> vis(n + 1);
while(1){
pii src = {inf, -1};
for(int i = 1; i <= n; ++i){
if(vis[i])continue;
if(dis1[id][i] < src.first && dis1[id][i] != -1){
src = {dis1[id][i], i};
}
}
if(src.first == inf)break;
if(vis[src.second])continue;
vis[src.second] = 1;
for(auto i : g[src.second]){
ll cost = dis1[id][src.second] + i.c;
if(dis1[id][i.x] > cost or dis1[id][i.x] == -1){
dis1[id][i.x] = cost;
}
}
}
}
ll dis2[2][maxn];
void dijkstra2(int id, int s){
for(int i = 1; i <= n; ++i)dis2[id][i] = -1;
dis2[id][s] = 0;
using pii = pair<ll, int>;
vector<bool> vis(n + 1);
while(1){
pii src = {inf, -1};
for(int i = 1; i <= n; ++i){
if(vis[i])continue;
if(dis2[id][i] < src.first && dis2[id][i] != -1){
src = {dis2[id][i], i};
}
}
if(src.first == inf)break;
if(vis[src.second])continue;
vis[src.second] = 1;
for(auto i : rev[src.second]){
ll cost = dis2[id][src.second] + i.c;
if(dis2[id][i.x] > cost or dis2[id][i.x] == -1){
dis2[id][i.x] = cost;
}
}
}
}
ll ans = 1e18;
void check(int id){
ll D = 0;
for(int i = 1; i <= n; ++i)g[i].clear();
for(int i = 1; i <= n; ++i){
for(auto j : edges[i]){
if(j.id != id){
g[i].push_back(j);
}else {
g[j.x].push_back(E(i, j.c, j.d, j.id));
D = j.d;
}
}
}
dijkstra1(0, 1);
dijkstra1(1, n);
if(dis1[0][n] != -1 && dis1[1][1] != -1){
ans = min(ans, D + dis1[0][n] + dis1[1][1]);
}
}
int good[50005];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
using pii = pair<ll, ll>;
vector<pair<pii, pii>> inp;
for(int i = 0; i < m; ++i){
int u, v;
ll C, D;
cin >> u >> v >> C >> D;
edges[u].push_back(E(v, C, D, i));
rev[v].push_back(E(u, C, D, i));
inp.push_back({{u, v}, {C, D}});
}
memset(par, -1, sizeof par);
dijkstra(0, 1);
dijkstra(1, n);
dijkstra2(0, 1);
dijkstra2(1, n);
for(int i = 0; i < 2; ++i){
for(int j = 1; j <= n; ++j){
if(par[i][j] != -1){
candidates.push_back({par[i][j]});
}
}
}
sort(candidates.begin(), candidates.end());
candidates.erase(unique(candidates.begin(), candidates.end()), candidates.end());
ans = 1e18;
if(dis[0][n] != -1 && dis[1][1] != -1)ans = dis[0][n] + dis[1][1];
for(auto i : candidates){
check(i);
good[i] = 1;
}
for(int i = 0; i < m; ++i){
if(!good[i]){
ll x = 1e18, y = 1e18;
if(dis[0][n] != -1)x = dis[0][n];
if(dis[0][inp[i].first.second] != -1 && dis2[1][inp[i].first.first] != -1)x = min(x, dis[0][inp[i].first.second] + dis2[1][inp[i].first.first] + inp[i].second.first);
if(dis[1][1] != -1)y = dis[1][1];
if(dis[1][inp[i].first.second] != -1 && dis2[0][inp[i].first.first] != -1)y = min(y, dis[1][inp[i].first.second] + dis2[0][inp[i].first.first] + inp[i].second.first);
ans = min(ans, x + y + inp[i].second.second);
}
}
cout << (ans < inf ? ans : -1) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
90 ms |
492 KB |
Output is correct |
4 |
Correct |
97 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
69 ms |
492 KB |
Output is correct |
11 |
Correct |
93 ms |
572 KB |
Output is correct |
12 |
Correct |
90 ms |
620 KB |
Output is correct |
13 |
Correct |
21 ms |
492 KB |
Output is correct |
14 |
Correct |
55 ms |
492 KB |
Output is correct |
15 |
Correct |
51 ms |
492 KB |
Output is correct |
16 |
Correct |
47 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
216 ms |
7260 KB |
Output is correct |
2 |
Correct |
222 ms |
7260 KB |
Output is correct |
3 |
Correct |
218 ms |
7004 KB |
Output is correct |
4 |
Correct |
95 ms |
620 KB |
Output is correct |
5 |
Correct |
67 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
84 ms |
7644 KB |
Output is correct |
10 |
Correct |
80 ms |
7516 KB |
Output is correct |
11 |
Correct |
174 ms |
7388 KB |
Output is correct |
12 |
Correct |
157 ms |
7152 KB |
Output is correct |
13 |
Correct |
144 ms |
7396 KB |
Output is correct |
14 |
Correct |
130 ms |
7644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
620 KB |
Output is correct |
2 |
Correct |
5 ms |
364 KB |
Output is correct |
3 |
Correct |
179 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
213 ms |
7288 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
98 ms |
7648 KB |
Output is correct |
9 |
Correct |
103 ms |
7644 KB |
Output is correct |
10 |
Correct |
166 ms |
7132 KB |
Output is correct |
11 |
Correct |
161 ms |
7004 KB |
Output is correct |
12 |
Correct |
159 ms |
7516 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
192 ms |
7520 KB |
Output is correct |
20 |
Correct |
147 ms |
7004 KB |
Output is correct |
21 |
Correct |
152 ms |
7132 KB |
Output is correct |
22 |
Correct |
151 ms |
7392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
90 ms |
492 KB |
Output is correct |
4 |
Correct |
97 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
69 ms |
492 KB |
Output is correct |
11 |
Correct |
93 ms |
572 KB |
Output is correct |
12 |
Correct |
90 ms |
620 KB |
Output is correct |
13 |
Correct |
21 ms |
492 KB |
Output is correct |
14 |
Correct |
55 ms |
492 KB |
Output is correct |
15 |
Correct |
51 ms |
492 KB |
Output is correct |
16 |
Correct |
47 ms |
492 KB |
Output is correct |
17 |
Correct |
216 ms |
7260 KB |
Output is correct |
18 |
Correct |
222 ms |
7260 KB |
Output is correct |
19 |
Correct |
218 ms |
7004 KB |
Output is correct |
20 |
Correct |
95 ms |
620 KB |
Output is correct |
21 |
Correct |
67 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
84 ms |
7644 KB |
Output is correct |
26 |
Correct |
80 ms |
7516 KB |
Output is correct |
27 |
Correct |
174 ms |
7388 KB |
Output is correct |
28 |
Correct |
157 ms |
7152 KB |
Output is correct |
29 |
Correct |
144 ms |
7396 KB |
Output is correct |
30 |
Correct |
130 ms |
7644 KB |
Output is correct |
31 |
Correct |
52 ms |
620 KB |
Output is correct |
32 |
Correct |
5 ms |
364 KB |
Output is correct |
33 |
Correct |
179 ms |
5724 KB |
Output is correct |
34 |
Correct |
2 ms |
364 KB |
Output is correct |
35 |
Correct |
213 ms |
7288 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
0 ms |
364 KB |
Output is correct |
38 |
Correct |
98 ms |
7648 KB |
Output is correct |
39 |
Correct |
103 ms |
7644 KB |
Output is correct |
40 |
Correct |
166 ms |
7132 KB |
Output is correct |
41 |
Correct |
161 ms |
7004 KB |
Output is correct |
42 |
Correct |
159 ms |
7516 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
0 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
0 ms |
364 KB |
Output is correct |
47 |
Correct |
0 ms |
364 KB |
Output is correct |
48 |
Correct |
0 ms |
364 KB |
Output is correct |
49 |
Correct |
192 ms |
7520 KB |
Output is correct |
50 |
Correct |
147 ms |
7004 KB |
Output is correct |
51 |
Correct |
152 ms |
7132 KB |
Output is correct |
52 |
Correct |
151 ms |
7392 KB |
Output is correct |
53 |
Correct |
196 ms |
6712 KB |
Output is correct |
54 |
Correct |
221 ms |
7260 KB |
Output is correct |
55 |
Correct |
216 ms |
7264 KB |
Output is correct |
56 |
Correct |
98 ms |
492 KB |
Output is correct |
57 |
Correct |
82 ms |
492 KB |
Output is correct |
58 |
Correct |
134 ms |
5724 KB |
Output is correct |
59 |
Correct |
151 ms |
5724 KB |
Output is correct |
60 |
Correct |
216 ms |
5724 KB |
Output is correct |
61 |
Correct |
137 ms |
5724 KB |
Output is correct |
62 |
Correct |
153 ms |
5728 KB |
Output is correct |
63 |
Correct |
222 ms |
5852 KB |
Output is correct |
64 |
Correct |
200 ms |
5856 KB |
Output is correct |
65 |
Correct |
201 ms |
6108 KB |
Output is correct |
66 |
Correct |
260 ms |
5980 KB |
Output is correct |
67 |
Correct |
20 ms |
5932 KB |
Output is correct |
68 |
Correct |
83 ms |
7648 KB |
Output is correct |
69 |
Correct |
84 ms |
7904 KB |
Output is correct |
70 |
Correct |
163 ms |
6876 KB |
Output is correct |
71 |
Correct |
160 ms |
7132 KB |
Output is correct |
72 |
Correct |
152 ms |
6876 KB |
Output is correct |
73 |
Correct |
164 ms |
7132 KB |
Output is correct |
74 |
Correct |
166 ms |
7260 KB |
Output is correct |