Submission #975041

# Submission time Handle Problem Language Result Execution time Memory
975041 2024-05-04T10:32:39 Z berr Olympic Bus (JOI20_ho_t4) C++17
100 / 100
934 ms 11980 KB
#include <bits/stdc++.h>
using namespace std; 
#define int long long
const int N=205, INF=1e16;
vector<int> all[N][N], all2[N][N];
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));
 


void dijkstra(int v, int skip, vector<int> &dist, vector<vector<array<int, 3>>> &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});
            }
        }
    }
}
 
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);
 
    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);
        all2[u][v].push_back(cnt);
        cnt++;
    }


    for(int i=0; i<n; i++){

        sort(g[i].begin(), g[i].end(), [&](auto x, auto y){
            return x[1] < y[1];
        });
        sort(g_rev[i].begin(), g_rev[i].end(), [&](auto x, auto y){
            return x[1] < y[1];
        });

        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];
            });
            sort(all2[i][l].begin(), all2[i][l].end(), [&](int x, int y){
                return e[x][2]+di[x] < e[y][2]+di[y];
            });
        }
    }
 
    // a = 1 -> i, b = i -> n
    // c = i -> 1 without x
    // d = n -> i without x
 
    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, ksum=INF;
            int g=0;
            for(auto k: all2[j][i]){
                g++;
                if(g==4) break;
                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]);
 
                }
 
                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]);
                }
                ksum=min(ksum, sum);

            }   

            for(int g=0; g<n; g++){
                if(g!=i&&g!=j){
                    ans=min(ans, ksum+d[j][g]+c[i][g]);
                    ans=min(ans, ksum+d[i][g]+c[j][g]);
                }
            }         

        }
    }
 
    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, ksum=INF;;
            int g=0;
            for(auto k: all2[j][i]){
                g++;
                if(g==3) break;
                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]);
                }
                //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]);
                }
                ksum=min(ksum, sum);
 
            }
            for(int g=0; g<n; g++){
                if(g!=i&&g!=j){
                    ans=min(ans, ksum+b[j][g]+a[i][g]);
                    ans=min(ans, ksum+b[i][g]+a[j][g]);
                }
            }
        }
    }
    ans = min(ans, a[n][n-1]+d[n][0]);
    if(ans==INF) cout<<-1;
    else cout<<ans;
} 
# Verdict Execution time Memory Grader output
1 Correct 44 ms 3932 KB Output is correct
2 Correct 3 ms 3672 KB Output is correct
3 Correct 49 ms 3932 KB Output is correct
4 Correct 50 ms 3932 KB Output is correct
5 Correct 3 ms 3932 KB Output is correct
6 Correct 4 ms 3792 KB Output is correct
7 Correct 2 ms 3676 KB Output is correct
8 Correct 2 ms 3676 KB Output is correct
9 Correct 3 ms 3932 KB Output is correct
10 Correct 55 ms 3964 KB Output is correct
11 Correct 56 ms 3932 KB Output is correct
12 Correct 55 ms 3964 KB Output is correct
13 Correct 13 ms 3928 KB Output is correct
14 Correct 31 ms 3980 KB Output is correct
15 Correct 31 ms 3932 KB Output is correct
16 Correct 32 ms 3932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 10840 KB Output is correct
2 Correct 152 ms 11192 KB Output is correct
3 Correct 159 ms 11240 KB Output is correct
4 Correct 50 ms 3932 KB Output is correct
5 Correct 36 ms 3932 KB Output is correct
6 Correct 5 ms 4180 KB Output is correct
7 Correct 3 ms 3676 KB Output is correct
8 Correct 2 ms 3788 KB Output is correct
9 Correct 70 ms 10924 KB Output is correct
10 Correct 73 ms 10844 KB Output is correct
11 Correct 113 ms 11060 KB Output is correct
12 Correct 133 ms 11156 KB Output is correct
13 Correct 116 ms 11152 KB Output is correct
14 Correct 111 ms 11324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 3932 KB Output is correct
2 Correct 8 ms 3676 KB Output is correct
3 Correct 98 ms 9824 KB Output is correct
4 Correct 5 ms 3672 KB Output is correct
5 Correct 105 ms 11316 KB Output is correct
6 Correct 2 ms 3676 KB Output is correct
7 Correct 2 ms 3672 KB Output is correct
8 Correct 51 ms 11356 KB Output is correct
9 Correct 50 ms 11344 KB Output is correct
10 Correct 77 ms 11428 KB Output is correct
11 Correct 81 ms 11348 KB Output is correct
12 Correct 78 ms 11528 KB Output is correct
13 Correct 2 ms 3676 KB Output is correct
14 Correct 2 ms 3580 KB Output is correct
15 Correct 2 ms 3672 KB Output is correct
16 Correct 2 ms 3676 KB Output is correct
17 Correct 2 ms 3612 KB Output is correct
18 Correct 2 ms 3676 KB Output is correct
19 Correct 83 ms 11664 KB Output is correct
20 Correct 77 ms 10844 KB Output is correct
21 Correct 80 ms 11088 KB Output is correct
22 Correct 78 ms 11356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 3932 KB Output is correct
2 Correct 3 ms 3672 KB Output is correct
3 Correct 49 ms 3932 KB Output is correct
4 Correct 50 ms 3932 KB Output is correct
5 Correct 3 ms 3932 KB Output is correct
6 Correct 4 ms 3792 KB Output is correct
7 Correct 2 ms 3676 KB Output is correct
8 Correct 2 ms 3676 KB Output is correct
9 Correct 3 ms 3932 KB Output is correct
10 Correct 55 ms 3964 KB Output is correct
11 Correct 56 ms 3932 KB Output is correct
12 Correct 55 ms 3964 KB Output is correct
13 Correct 13 ms 3928 KB Output is correct
14 Correct 31 ms 3980 KB Output is correct
15 Correct 31 ms 3932 KB Output is correct
16 Correct 32 ms 3932 KB Output is correct
17 Correct 155 ms 10840 KB Output is correct
18 Correct 152 ms 11192 KB Output is correct
19 Correct 159 ms 11240 KB Output is correct
20 Correct 50 ms 3932 KB Output is correct
21 Correct 36 ms 3932 KB Output is correct
22 Correct 5 ms 4180 KB Output is correct
23 Correct 3 ms 3676 KB Output is correct
24 Correct 2 ms 3788 KB Output is correct
25 Correct 70 ms 10924 KB Output is correct
26 Correct 73 ms 10844 KB Output is correct
27 Correct 113 ms 11060 KB Output is correct
28 Correct 133 ms 11156 KB Output is correct
29 Correct 116 ms 11152 KB Output is correct
30 Correct 111 ms 11324 KB Output is correct
31 Correct 44 ms 3932 KB Output is correct
32 Correct 8 ms 3676 KB Output is correct
33 Correct 98 ms 9824 KB Output is correct
34 Correct 5 ms 3672 KB Output is correct
35 Correct 105 ms 11316 KB Output is correct
36 Correct 2 ms 3676 KB Output is correct
37 Correct 2 ms 3672 KB Output is correct
38 Correct 51 ms 11356 KB Output is correct
39 Correct 50 ms 11344 KB Output is correct
40 Correct 77 ms 11428 KB Output is correct
41 Correct 81 ms 11348 KB Output is correct
42 Correct 78 ms 11528 KB Output is correct
43 Correct 2 ms 3676 KB Output is correct
44 Correct 2 ms 3580 KB Output is correct
45 Correct 2 ms 3672 KB Output is correct
46 Correct 2 ms 3676 KB Output is correct
47 Correct 2 ms 3612 KB Output is correct
48 Correct 2 ms 3676 KB Output is correct
49 Correct 83 ms 11664 KB Output is correct
50 Correct 77 ms 10844 KB Output is correct
51 Correct 80 ms 11088 KB Output is correct
52 Correct 78 ms 11356 KB Output is correct
53 Correct 160 ms 11188 KB Output is correct
54 Correct 158 ms 11296 KB Output is correct
55 Correct 154 ms 11616 KB Output is correct
56 Correct 58 ms 3972 KB Output is correct
57 Correct 50 ms 3932 KB Output is correct
58 Correct 142 ms 9964 KB Output is correct
59 Correct 159 ms 9820 KB Output is correct
60 Correct 154 ms 9964 KB Output is correct
61 Correct 136 ms 9816 KB Output is correct
62 Correct 146 ms 9816 KB Output is correct
63 Correct 152 ms 9964 KB Output is correct
64 Correct 934 ms 10424 KB Output is correct
65 Correct 867 ms 10516 KB Output is correct
66 Correct 831 ms 10356 KB Output is correct
67 Correct 25 ms 8848 KB Output is correct
68 Correct 74 ms 11868 KB Output is correct
69 Correct 72 ms 11860 KB Output is correct
70 Correct 112 ms 11352 KB Output is correct
71 Correct 125 ms 11684 KB Output is correct
72 Correct 134 ms 11568 KB Output is correct
73 Correct 113 ms 11980 KB Output is correct
74 Correct 130 ms 11892 KB Output is correct