#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int maxn = 200 + 10;
const int maxm = 5e4 + 10;
const int inf = 1e9;
int n, m;
int a[maxm], b[maxm], c[maxm], d[maxm];
vector<pair<int,int> > g[maxn];
int dp[maxn], pd[maxn], cn[maxn], pr[maxn];
bool mark[maxn];
int dijkstra(int src, int snk){
memset(dp, 63, sizeof dp);
memset(mark, 0, sizeof mark);
memset(cn, 0, sizeof cn);
dp[src] = 0;
for (int i = 0; i < n; i++){
int v = -1;
for (int j = 1; j <= n; j++)
if (!mark[j] and (v == -1 or dp[v] > dp[j]))
v = j;
if (v == -1)
break;
mark[v] = 1;
for (auto e : g[v]){
int idx = e.second;
int u = e.first, w = c[idx];
if (dp[u] > dp[v] + w){
dp[u] = dp[v] + w;
pr[u] = idx;
cn[u] = 1;
}
else if (dp[u] == dp[v] + w)
cn[u] ++;
}
}
return dp[snk];
}
int reshpa(int src, int snk){
for (int i = 0; i < m; i++)
g[b[i]].push_back({a[i], i});
for (int i = 1; i <= n; i++)
pd[i] = dp[i];
int ret = dijkstra(snk, src);
for (int i = 1; i <= n; i++){
swap(dp[i], pd[i]);
g[i].clear();
}
return ret;
}
int shpa(int src, int snk){
for (int i = 0; i < m; i++)
g[a[i]].push_back({b[i], i});
int ret = dijkstra(src, snk);
for (int i = 1; i <= n; i++)
g[i].clear();
return ret;
}
bool visited[maxm];
ll ans[maxm];
void solve(int src, int snk){
reshpa(src, snk);
int now = shpa(src, snk);
memset(visited, 0, sizeof visited);
if (now <= inf){
vector<int> sp;
int tmp = snk;
while (tmp != src){
sp.push_back(pr[tmp]);
tmp = a[pr[tmp]];
}
for (auto it : sp){
swap(a[it], b[it]);
ans[it] += shpa(src, snk);
visited[it] = 1;
swap(a[it], b[it]);
}
}
shpa(src, snk);
for (int i = 0; i < m; i++){
if (visited[i])
continue;
ll t = dp[b[i]], s = pd[a[i]];
ans[i] += min((ll)now, s + t + c[i]);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
ll answer = shpa(1, n) + shpa(n, 1);
solve(1, n);
solve(n, 1);
ll t = answer;
for (int i = 0; i < m; i++){
answer = min(answer, ans[i] + d[i]);
t = min(t, ans[i]);
}
if (t >= inf)
return cout << -1 << endl, 0;
cout << answer << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
508 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
10 ms |
504 KB |
Output is correct |
4 |
Correct |
8 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
57 ms |
504 KB |
Output is correct |
11 |
Correct |
55 ms |
504 KB |
Output is correct |
12 |
Correct |
55 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
8 ms |
504 KB |
Output is correct |
15 |
Correct |
7 ms |
504 KB |
Output is correct |
16 |
Correct |
8 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
2300 KB |
Output is correct |
2 |
Correct |
37 ms |
2296 KB |
Output is correct |
3 |
Correct |
40 ms |
2296 KB |
Output is correct |
4 |
Correct |
9 ms |
504 KB |
Output is correct |
5 |
Correct |
8 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
33 ms |
2424 KB |
Output is correct |
10 |
Correct |
35 ms |
2424 KB |
Output is correct |
11 |
Correct |
42 ms |
2296 KB |
Output is correct |
12 |
Correct |
42 ms |
2296 KB |
Output is correct |
13 |
Correct |
39 ms |
2296 KB |
Output is correct |
14 |
Correct |
39 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
30 ms |
1784 KB |
Output is correct |
4 |
Correct |
6 ms |
376 KB |
Output is correct |
5 |
Correct |
34 ms |
2296 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
33 ms |
2424 KB |
Output is correct |
9 |
Correct |
33 ms |
2424 KB |
Output is correct |
10 |
Correct |
33 ms |
2296 KB |
Output is correct |
11 |
Correct |
34 ms |
2296 KB |
Output is correct |
12 |
Correct |
35 ms |
2296 KB |
Output is correct |
13 |
Correct |
5 ms |
380 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
35 ms |
2296 KB |
Output is correct |
20 |
Correct |
35 ms |
2296 KB |
Output is correct |
21 |
Correct |
38 ms |
2296 KB |
Output is correct |
22 |
Correct |
34 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
508 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
10 ms |
504 KB |
Output is correct |
4 |
Correct |
8 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
57 ms |
504 KB |
Output is correct |
11 |
Correct |
55 ms |
504 KB |
Output is correct |
12 |
Correct |
55 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
8 ms |
504 KB |
Output is correct |
15 |
Correct |
7 ms |
504 KB |
Output is correct |
16 |
Correct |
8 ms |
504 KB |
Output is correct |
17 |
Correct |
39 ms |
2300 KB |
Output is correct |
18 |
Correct |
37 ms |
2296 KB |
Output is correct |
19 |
Correct |
40 ms |
2296 KB |
Output is correct |
20 |
Correct |
9 ms |
504 KB |
Output is correct |
21 |
Correct |
8 ms |
504 KB |
Output is correct |
22 |
Correct |
6 ms |
376 KB |
Output is correct |
23 |
Correct |
6 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
33 ms |
2424 KB |
Output is correct |
26 |
Correct |
35 ms |
2424 KB |
Output is correct |
27 |
Correct |
42 ms |
2296 KB |
Output is correct |
28 |
Correct |
42 ms |
2296 KB |
Output is correct |
29 |
Correct |
39 ms |
2296 KB |
Output is correct |
30 |
Correct |
39 ms |
2296 KB |
Output is correct |
31 |
Correct |
8 ms |
504 KB |
Output is correct |
32 |
Correct |
6 ms |
376 KB |
Output is correct |
33 |
Correct |
30 ms |
1784 KB |
Output is correct |
34 |
Correct |
6 ms |
376 KB |
Output is correct |
35 |
Correct |
34 ms |
2296 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
33 ms |
2424 KB |
Output is correct |
39 |
Correct |
33 ms |
2424 KB |
Output is correct |
40 |
Correct |
33 ms |
2296 KB |
Output is correct |
41 |
Correct |
34 ms |
2296 KB |
Output is correct |
42 |
Correct |
35 ms |
2296 KB |
Output is correct |
43 |
Correct |
5 ms |
380 KB |
Output is correct |
44 |
Correct |
5 ms |
376 KB |
Output is correct |
45 |
Correct |
5 ms |
376 KB |
Output is correct |
46 |
Correct |
5 ms |
376 KB |
Output is correct |
47 |
Correct |
5 ms |
376 KB |
Output is correct |
48 |
Correct |
5 ms |
376 KB |
Output is correct |
49 |
Correct |
35 ms |
2296 KB |
Output is correct |
50 |
Correct |
35 ms |
2296 KB |
Output is correct |
51 |
Correct |
38 ms |
2296 KB |
Output is correct |
52 |
Correct |
34 ms |
2296 KB |
Output is correct |
53 |
Correct |
39 ms |
2296 KB |
Output is correct |
54 |
Correct |
41 ms |
2296 KB |
Output is correct |
55 |
Correct |
40 ms |
2296 KB |
Output is correct |
56 |
Correct |
8 ms |
504 KB |
Output is correct |
57 |
Correct |
7 ms |
504 KB |
Output is correct |
58 |
Correct |
262 ms |
1848 KB |
Output is correct |
59 |
Correct |
262 ms |
1784 KB |
Output is correct |
60 |
Correct |
258 ms |
1784 KB |
Output is correct |
61 |
Correct |
218 ms |
1912 KB |
Output is correct |
62 |
Correct |
258 ms |
1784 KB |
Output is correct |
63 |
Correct |
257 ms |
1784 KB |
Output is correct |
64 |
Correct |
322 ms |
2168 KB |
Output is correct |
65 |
Correct |
309 ms |
2040 KB |
Output is correct |
66 |
Correct |
305 ms |
2296 KB |
Output is correct |
67 |
Correct |
26 ms |
2164 KB |
Output is correct |
68 |
Correct |
33 ms |
2424 KB |
Output is correct |
69 |
Correct |
33 ms |
2424 KB |
Output is correct |
70 |
Correct |
43 ms |
2296 KB |
Output is correct |
71 |
Correct |
40 ms |
2296 KB |
Output is correct |
72 |
Correct |
37 ms |
2296 KB |
Output is correct |
73 |
Correct |
38 ms |
2296 KB |
Output is correct |
74 |
Correct |
40 ms |
2296 KB |
Output is correct |