#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 201;
const int M = 50001;
const ll INF = 1e10;
struct edge{
int u, v, c, d, ind;
};
int n, m;
edge eds[M];
ll dist[N][N], f[2][M], minDist[N];
int trace[N][N];
vector <edge> adj[N];
void ReadInput(){
cin >> n >> m;
for(int i = 1; i <= 2 * n; i++)
for(int j = 1; j <= 2 * n; j++)
dist[i][j] = (i != j) * INF;
for(int i = 1; i <= m; i++){
cin >> eds[i].u >> eds[i].v >> eds[i].c >> eds[i].d;
eds[i].ind = i;
}
}
ll Dijkstra(int s, int t, int revInd = 0){
for(int i = 1; i <= n; i++)
minDist[i] = INF;
typedef pair<int, int> data;
priority_queue <data, vector<data>, greater<data>> pq;
minDist[s] = 0;
pq.push({0, s});
while (!pq.empty()){
int u = pq.top().second;
int d = pq.top().first;
pq.pop();
if (minDist[u] != d)
continue;
if (u == t)
return minDist[u];
for(auto e : adj[u])
if (e.ind != revInd && minDist[e.v] > minDist[e.u] + e.c){
minDist[e.v] = minDist[e.u] + e.c;
pq.push({minDist[e.v], e.v});
}
if (eds[revInd].v == u && minDist[eds[revInd].u] > minDist[u] + eds[revInd].c){
minDist[eds[revInd].u] = minDist[u] + eds[revInd].c;
pq.push({minDist[eds[revInd].u], eds[revInd].u});
}
}
return INF;
}
void Solve(){
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
dist[i][j] = (i != j) * INF;
for(int i = 1; i <= m; i++){
if (dist[eds[i].u][eds[i].v] > eds[i].c){
dist[eds[i].u][eds[i].v] = eds[i].c;
trace[eds[i].u][eds[i].v] = i;
}
adj[eds[i].u].push_back(eds[i]);
}
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if (dist[i][j] > dist[i][k] + dist[k][j]){
dist[i][j] = dist[i][k] + dist[k][j];
trace[i][j] = trace[i][k];
}
int s = 1, t = n;
for(int num = 0; num < 2; num++){
for(int i = 1; i <= m; i++)
f[num][i] = min(dist[s][t], dist[s][eds[i].v] + eds[i].c + dist[eds[i].u][t]);
if (dist[s][t] != INF){
cerr << num << endl;
int u = s;
while (u != t){
int ind = trace[u][t];
f[num][ind] = Dijkstra(s, t, ind);
u = eds[ind].v;
}
}
swap(s, t);
}
ll ans = dist[1][n] + dist[n][1];
for(int i = 1; i <= m; i++)
ans = min(ans, f[0][i] + f[1][i] + eds[i].d);
cout << (ans < INF? ans : -1);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ReadInput();
Solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
1260 KB |
Output is correct |
2 |
Correct |
9 ms |
1132 KB |
Output is correct |
3 |
Correct |
12 ms |
1260 KB |
Output is correct |
4 |
Correct |
12 ms |
1260 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
10 ms |
1132 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 |
25 ms |
1260 KB |
Output is correct |
11 |
Correct |
24 ms |
1260 KB |
Output is correct |
12 |
Correct |
26 ms |
1260 KB |
Output is correct |
13 |
Correct |
11 ms |
1260 KB |
Output is correct |
14 |
Correct |
11 ms |
1260 KB |
Output is correct |
15 |
Correct |
11 ms |
1260 KB |
Output is correct |
16 |
Correct |
11 ms |
1260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
5356 KB |
Output is correct |
2 |
Correct |
35 ms |
5228 KB |
Output is correct |
3 |
Correct |
41 ms |
5484 KB |
Output is correct |
4 |
Correct |
12 ms |
1260 KB |
Output is correct |
5 |
Correct |
11 ms |
1260 KB |
Output is correct |
6 |
Correct |
10 ms |
1152 KB |
Output is correct |
7 |
Correct |
9 ms |
1132 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
37 ms |
5356 KB |
Output is correct |
10 |
Correct |
39 ms |
5356 KB |
Output is correct |
11 |
Correct |
34 ms |
5356 KB |
Output is correct |
12 |
Correct |
36 ms |
5508 KB |
Output is correct |
13 |
Correct |
34 ms |
5504 KB |
Output is correct |
14 |
Correct |
34 ms |
5624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
1260 KB |
Output is correct |
2 |
Correct |
10 ms |
1132 KB |
Output is correct |
3 |
Correct |
25 ms |
4076 KB |
Output is correct |
4 |
Correct |
10 ms |
1132 KB |
Output is correct |
5 |
Correct |
29 ms |
5100 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
30 ms |
5184 KB |
Output is correct |
9 |
Correct |
29 ms |
5100 KB |
Output is correct |
10 |
Correct |
29 ms |
5100 KB |
Output is correct |
11 |
Correct |
30 ms |
5228 KB |
Output is correct |
12 |
Correct |
30 ms |
5124 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
30 ms |
5228 KB |
Output is correct |
20 |
Correct |
30 ms |
5100 KB |
Output is correct |
21 |
Correct |
34 ms |
5100 KB |
Output is correct |
22 |
Correct |
29 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
1260 KB |
Output is correct |
2 |
Correct |
9 ms |
1132 KB |
Output is correct |
3 |
Correct |
12 ms |
1260 KB |
Output is correct |
4 |
Correct |
12 ms |
1260 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
10 ms |
1132 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 |
25 ms |
1260 KB |
Output is correct |
11 |
Correct |
24 ms |
1260 KB |
Output is correct |
12 |
Correct |
26 ms |
1260 KB |
Output is correct |
13 |
Correct |
11 ms |
1260 KB |
Output is correct |
14 |
Correct |
11 ms |
1260 KB |
Output is correct |
15 |
Correct |
11 ms |
1260 KB |
Output is correct |
16 |
Correct |
11 ms |
1260 KB |
Output is correct |
17 |
Correct |
38 ms |
5356 KB |
Output is correct |
18 |
Correct |
35 ms |
5228 KB |
Output is correct |
19 |
Correct |
41 ms |
5484 KB |
Output is correct |
20 |
Correct |
12 ms |
1260 KB |
Output is correct |
21 |
Correct |
11 ms |
1260 KB |
Output is correct |
22 |
Correct |
10 ms |
1152 KB |
Output is correct |
23 |
Correct |
9 ms |
1132 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
37 ms |
5356 KB |
Output is correct |
26 |
Correct |
39 ms |
5356 KB |
Output is correct |
27 |
Correct |
34 ms |
5356 KB |
Output is correct |
28 |
Correct |
36 ms |
5508 KB |
Output is correct |
29 |
Correct |
34 ms |
5504 KB |
Output is correct |
30 |
Correct |
34 ms |
5624 KB |
Output is correct |
31 |
Correct |
11 ms |
1260 KB |
Output is correct |
32 |
Correct |
10 ms |
1132 KB |
Output is correct |
33 |
Correct |
25 ms |
4076 KB |
Output is correct |
34 |
Correct |
10 ms |
1132 KB |
Output is correct |
35 |
Correct |
29 ms |
5100 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
30 ms |
5184 KB |
Output is correct |
39 |
Correct |
29 ms |
5100 KB |
Output is correct |
40 |
Correct |
29 ms |
5100 KB |
Output is correct |
41 |
Correct |
30 ms |
5228 KB |
Output is correct |
42 |
Correct |
30 ms |
5124 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
492 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
30 ms |
5228 KB |
Output is correct |
50 |
Correct |
30 ms |
5100 KB |
Output is correct |
51 |
Correct |
34 ms |
5100 KB |
Output is correct |
52 |
Correct |
29 ms |
5100 KB |
Output is correct |
53 |
Correct |
36 ms |
5100 KB |
Output is correct |
54 |
Correct |
37 ms |
5100 KB |
Output is correct |
55 |
Correct |
35 ms |
5100 KB |
Output is correct |
56 |
Correct |
12 ms |
1260 KB |
Output is correct |
57 |
Correct |
12 ms |
1280 KB |
Output is correct |
58 |
Correct |
89 ms |
4204 KB |
Output is correct |
59 |
Correct |
91 ms |
4204 KB |
Output is correct |
60 |
Correct |
96 ms |
4204 KB |
Output is correct |
61 |
Correct |
75 ms |
4204 KB |
Output is correct |
62 |
Correct |
97 ms |
4332 KB |
Output is correct |
63 |
Correct |
92 ms |
4224 KB |
Output is correct |
64 |
Correct |
661 ms |
4572 KB |
Output is correct |
65 |
Correct |
596 ms |
4736 KB |
Output is correct |
66 |
Correct |
585 ms |
4564 KB |
Output is correct |
67 |
Correct |
16 ms |
3552 KB |
Output is correct |
68 |
Correct |
34 ms |
5356 KB |
Output is correct |
69 |
Correct |
34 ms |
5416 KB |
Output is correct |
70 |
Correct |
35 ms |
5356 KB |
Output is correct |
71 |
Correct |
34 ms |
5228 KB |
Output is correct |
72 |
Correct |
34 ms |
5228 KB |
Output is correct |
73 |
Correct |
34 ms |
5484 KB |
Output is correct |
74 |
Correct |
35 ms |
5356 KB |
Output is correct |