#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=205, INF=1e16;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<array<int, 3>>> g(n), g_rev(n);
vector<array<int, 3>> e(m);
vector<int> di(m);
vector<int> all[n][n];
int cnt =0;
for(auto &[u, v, c]: e){
cin >> u >> v >> c >> di[cnt];
u--; v--;
g[u].push_back({v, c, cnt});
g_rev[v].push_back({u, c, cnt});
all[u][v].push_back(cnt);
cnt++;
}
for(int i=0; i<n; i++){
for(int l=0; l<n; l++){
sort(all[i][l].begin(), all[i][l].end(), [&](int x, int y){
return e[x][2] < e[y][2];
});
}
}
// a = 1 -> i, b = i -> n
// c = i -> 1 without x
// d = n -> i without x
vector a(n+1, vector(n, INF)), b(n+1, vector(n, INF));
vector c(n+1, vector(n, INF)), d(n+1, vector(n, INF));
auto dijkstra=[&](int v, int skip, auto& dist, auto&&geey){
dist[v] = 0;
priority_queue<array<int, 2>> pq;
pq.push({0, v});
while(pq.size()){
auto [cost, v]=pq.top();
pq.pop(); cost*=-1;
if(v==skip||dist[v] != cost) continue;
for(auto [i, l, j]: geey[v]){
if(dist[i] > cost+l){
dist[i] = cost+l;
pq.push({-dist[i], i});
}
}
}
};
for(int i=0; i<=n; i++){
dijkstra(0, i, a[i], g);
dijkstra(n-1, i, b[i], g_rev);
dijkstra(0, i, c[i], g_rev);
dijkstra(n-1, i, d[i], g);
}
int ans=INF;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
//blocklanan edge i ve j arasında
// 0 -> i // swap j -> n
if(i==j) continue;
if(a[j][i]==INF||b[i][j]==INF) continue;
int sum=a[j][i]+b[i][j];
int tsum=sum;
for(auto k: all[j][i]){
sum=tsum;
sum+=e[k][2];
sum+=di[k];
if(all[j][i].size()>1){
if(k==all[j][i][0])
ans=min(ans, sum+d[i][j]+c[j][i]+e[all[j][i][1]][2]);
else
ans=min(ans, sum+d[i][j]+c[j][i]+e[all[j][i][0]][2]);
}
for(int g=0; g<n; g++){
if(g!=i&&g!=j){
ans=min(ans, sum+d[j][g]+c[i][g]);
ans=min(ans, sum+d[i][g]+c[j][g]);
}
}
ans=min(ans, sum+c[i][j]+d[j][i]+e[k][2]);
// diğer değer
if(all[i][j].size()>0){
ans=min(ans, sum+c[i][j]+d[j][i]+e[all[i][j][0]][2]);
}
}
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
//blocklanan edge i ve j arasında
if(i==j) continue;
if(d[j][i]==INF||c[i][j]==INF) continue;
int sum=d[j][i]+c[i][j];
// cout<<i<<" "<<j<<"\n";
//cout<<d[n][i]<<" "<<c[n][j]<<"\n";
int tsum=sum;
for(auto k: all[j][i]){
sum=tsum;
sum+=e[k][2];
sum+=di[k];
//cout<<j<<" "<<i<<"\n";
//j-> i di
//i -> j yaptı
if(all[j][i].size()>1){
if(k==all[j][i][0])
ans=min(ans, sum+b[j][i]+a[i][j]+e[all[j][i][1]][2]);
else
ans=min(ans, sum+b[j][i]+a[i][j]+e[all[j][i][0]][2]);
}
for(int g=0; g<n; g++){
if(g!=i&&g!=j){
ans=min(ans, sum+b[j][g]+a[i][g]);
ans=min(ans, sum+b[i][g]+a[j][g]);
}
}
//cout<<j<<" "<<i<<"\n";
ans=min(ans, sum+a[j][i]+b[i][j]+e[k][2]);
// diğer değer
if(all[i][j].size()>0){
ans=min(ans, sum+a[j][i]+b[i][j]+e[all[i][j][0]][2]);
}
}
}
}
ans = min(ans, a[n][n-1]+d[n][0]);
if(ans==INF) cout<<-1;
else cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2904 KB |
Output is correct |
3 |
Correct |
21 ms |
2652 KB |
Output is correct |
4 |
Correct |
24 ms |
2904 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
30 ms |
2652 KB |
Output is correct |
11 |
Correct |
29 ms |
2652 KB |
Output is correct |
12 |
Correct |
29 ms |
2652 KB |
Output is correct |
13 |
Correct |
6 ms |
2648 KB |
Output is correct |
14 |
Correct |
14 ms |
2652 KB |
Output is correct |
15 |
Correct |
13 ms |
2648 KB |
Output is correct |
16 |
Correct |
17 ms |
2860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
8408 KB |
Output is correct |
2 |
Correct |
164 ms |
8124 KB |
Output is correct |
3 |
Correct |
168 ms |
9472 KB |
Output is correct |
4 |
Correct |
24 ms |
2908 KB |
Output is correct |
5 |
Correct |
12 ms |
2764 KB |
Output is correct |
6 |
Correct |
3 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
73 ms |
9728 KB |
Output is correct |
10 |
Correct |
75 ms |
9564 KB |
Output is correct |
11 |
Correct |
114 ms |
9552 KB |
Output is correct |
12 |
Correct |
113 ms |
9308 KB |
Output is correct |
13 |
Correct |
132 ms |
9152 KB |
Output is correct |
14 |
Correct |
111 ms |
9560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
2652 KB |
Output is correct |
2 |
Correct |
3 ms |
2648 KB |
Output is correct |
3 |
Correct |
91 ms |
7044 KB |
Output is correct |
4 |
Correct |
3 ms |
2648 KB |
Output is correct |
5 |
Correct |
108 ms |
8028 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
48 ms |
8784 KB |
Output is correct |
9 |
Correct |
50 ms |
9564 KB |
Output is correct |
10 |
Correct |
72 ms |
9184 KB |
Output is correct |
11 |
Correct |
80 ms |
9080 KB |
Output is correct |
12 |
Correct |
75 ms |
9308 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
79 ms |
9456 KB |
Output is correct |
20 |
Correct |
86 ms |
9316 KB |
Output is correct |
21 |
Correct |
74 ms |
9048 KB |
Output is correct |
22 |
Correct |
73 ms |
9392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2904 KB |
Output is correct |
3 |
Correct |
21 ms |
2652 KB |
Output is correct |
4 |
Correct |
24 ms |
2904 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
30 ms |
2652 KB |
Output is correct |
11 |
Correct |
29 ms |
2652 KB |
Output is correct |
12 |
Correct |
29 ms |
2652 KB |
Output is correct |
13 |
Correct |
6 ms |
2648 KB |
Output is correct |
14 |
Correct |
14 ms |
2652 KB |
Output is correct |
15 |
Correct |
13 ms |
2648 KB |
Output is correct |
16 |
Correct |
17 ms |
2860 KB |
Output is correct |
17 |
Correct |
177 ms |
8408 KB |
Output is correct |
18 |
Correct |
164 ms |
8124 KB |
Output is correct |
19 |
Correct |
168 ms |
9472 KB |
Output is correct |
20 |
Correct |
24 ms |
2908 KB |
Output is correct |
21 |
Correct |
12 ms |
2764 KB |
Output is correct |
22 |
Correct |
3 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
73 ms |
9728 KB |
Output is correct |
26 |
Correct |
75 ms |
9564 KB |
Output is correct |
27 |
Correct |
114 ms |
9552 KB |
Output is correct |
28 |
Correct |
113 ms |
9308 KB |
Output is correct |
29 |
Correct |
132 ms |
9152 KB |
Output is correct |
30 |
Correct |
111 ms |
9560 KB |
Output is correct |
31 |
Correct |
14 ms |
2652 KB |
Output is correct |
32 |
Correct |
3 ms |
2648 KB |
Output is correct |
33 |
Correct |
91 ms |
7044 KB |
Output is correct |
34 |
Correct |
3 ms |
2648 KB |
Output is correct |
35 |
Correct |
108 ms |
8028 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
48 ms |
8784 KB |
Output is correct |
39 |
Correct |
50 ms |
9564 KB |
Output is correct |
40 |
Correct |
72 ms |
9184 KB |
Output is correct |
41 |
Correct |
80 ms |
9080 KB |
Output is correct |
42 |
Correct |
75 ms |
9308 KB |
Output is correct |
43 |
Correct |
0 ms |
344 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
344 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
79 ms |
9456 KB |
Output is correct |
50 |
Correct |
86 ms |
9316 KB |
Output is correct |
51 |
Correct |
74 ms |
9048 KB |
Output is correct |
52 |
Correct |
73 ms |
9392 KB |
Output is correct |
53 |
Correct |
170 ms |
9500 KB |
Output is correct |
54 |
Correct |
180 ms |
9572 KB |
Output is correct |
55 |
Correct |
184 ms |
9460 KB |
Output is correct |
56 |
Correct |
21 ms |
2648 KB |
Output is correct |
57 |
Correct |
22 ms |
2652 KB |
Output is correct |
58 |
Correct |
155 ms |
8096 KB |
Output is correct |
59 |
Correct |
157 ms |
8100 KB |
Output is correct |
60 |
Correct |
166 ms |
8028 KB |
Output is correct |
61 |
Correct |
146 ms |
8128 KB |
Output is correct |
62 |
Correct |
151 ms |
8108 KB |
Output is correct |
63 |
Correct |
157 ms |
8024 KB |
Output is correct |
64 |
Execution timed out |
1035 ms |
8736 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |