답안 #975046

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
975046 2024-05-04T10:37:11 Z berr Olympic Bus (JOI20_ho_t4) C++17
100 / 100
921 ms 10648 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<vector<int>> a, b, c, d;

vector<vector<array<int, 3>>> g(N), g_rev(N);

vector<int> dijkstra_rev(int v, int skip){

    vector<int> dist(N, INF);
    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]: g_rev[v]){
            if(dist[i] > cost+l){
                dist[i] = cost+l;
                pq.push({-dist[i], i});
            }
        }
    }
    return dist;
}

vector<int> dijkstra(int v, int skip){
    vector<int> dist(N, INF);
    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]: g[v]){
            if(dist[i] > cost+l){
                dist[i] = cost+l;
                pq.push({-dist[i], i});
            }
        }
    }
    return dist;
}

 
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    int n, m; cin >> n >> m;
    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++){
        a.push_back(dijkstra(0, i));
        b.push_back(dijkstra_rev(n-1, i));
        c.push_back(dijkstra_rev(0, i));
        d.push_back(dijkstra(n-1, i));
    }
 
    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;
} 
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 3932 KB Output is correct
2 Correct 3 ms 3676 KB Output is correct
3 Correct 48 ms 3932 KB Output is correct
4 Correct 49 ms 3932 KB Output is correct
5 Correct 2 ms 2648 KB Output is correct
6 Correct 3 ms 3676 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 56 ms 3876 KB Output is correct
11 Correct 56 ms 3788 KB Output is correct
12 Correct 70 ms 4180 KB Output is correct
13 Correct 13 ms 3932 KB Output is correct
14 Correct 37 ms 3932 KB Output is correct
15 Correct 30 ms 3932 KB Output is correct
16 Correct 32 ms 3928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 154 ms 10144 KB Output is correct
2 Correct 175 ms 9536 KB Output is correct
3 Correct 147 ms 9808 KB Output is correct
4 Correct 51 ms 3932 KB Output is correct
5 Correct 36 ms 3672 KB Output is correct
6 Correct 4 ms 3672 KB Output is correct
7 Correct 3 ms 3676 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 69 ms 10240 KB Output is correct
10 Correct 69 ms 10068 KB Output is correct
11 Correct 109 ms 9696 KB Output is correct
12 Correct 110 ms 9552 KB Output is correct
13 Correct 115 ms 10068 KB Output is correct
14 Correct 109 ms 10388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 3928 KB Output is correct
2 Correct 7 ms 3676 KB Output is correct
3 Correct 94 ms 9044 KB Output is correct
4 Correct 4 ms 3672 KB Output is correct
5 Correct 106 ms 10648 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 51 ms 10616 KB Output is correct
9 Correct 53 ms 10512 KB Output is correct
10 Correct 80 ms 10068 KB Output is correct
11 Correct 79 ms 10320 KB Output is correct
12 Correct 77 ms 10576 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 75 ms 10324 KB Output is correct
20 Correct 76 ms 10192 KB Output is correct
21 Correct 79 ms 10068 KB Output is correct
22 Correct 77 ms 9936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 3932 KB Output is correct
2 Correct 3 ms 3676 KB Output is correct
3 Correct 48 ms 3932 KB Output is correct
4 Correct 49 ms 3932 KB Output is correct
5 Correct 2 ms 2648 KB Output is correct
6 Correct 3 ms 3676 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 56 ms 3876 KB Output is correct
11 Correct 56 ms 3788 KB Output is correct
12 Correct 70 ms 4180 KB Output is correct
13 Correct 13 ms 3932 KB Output is correct
14 Correct 37 ms 3932 KB Output is correct
15 Correct 30 ms 3932 KB Output is correct
16 Correct 32 ms 3928 KB Output is correct
17 Correct 154 ms 10144 KB Output is correct
18 Correct 175 ms 9536 KB Output is correct
19 Correct 147 ms 9808 KB Output is correct
20 Correct 51 ms 3932 KB Output is correct
21 Correct 36 ms 3672 KB Output is correct
22 Correct 4 ms 3672 KB Output is correct
23 Correct 3 ms 3676 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 69 ms 10240 KB Output is correct
26 Correct 69 ms 10068 KB Output is correct
27 Correct 109 ms 9696 KB Output is correct
28 Correct 110 ms 9552 KB Output is correct
29 Correct 115 ms 10068 KB Output is correct
30 Correct 109 ms 10388 KB Output is correct
31 Correct 42 ms 3928 KB Output is correct
32 Correct 7 ms 3676 KB Output is correct
33 Correct 94 ms 9044 KB Output is correct
34 Correct 4 ms 3672 KB Output is correct
35 Correct 106 ms 10648 KB Output is correct
36 Correct 1 ms 2396 KB Output is correct
37 Correct 1 ms 2396 KB Output is correct
38 Correct 51 ms 10616 KB Output is correct
39 Correct 53 ms 10512 KB Output is correct
40 Correct 80 ms 10068 KB Output is correct
41 Correct 79 ms 10320 KB Output is correct
42 Correct 77 ms 10576 KB Output is correct
43 Correct 2 ms 2396 KB Output is correct
44 Correct 1 ms 2396 KB Output is correct
45 Correct 1 ms 2396 KB Output is correct
46 Correct 1 ms 2396 KB Output is correct
47 Correct 1 ms 2396 KB Output is correct
48 Correct 1 ms 2396 KB Output is correct
49 Correct 75 ms 10324 KB Output is correct
50 Correct 76 ms 10192 KB Output is correct
51 Correct 79 ms 10068 KB Output is correct
52 Correct 77 ms 9936 KB Output is correct
53 Correct 164 ms 10064 KB Output is correct
54 Correct 165 ms 10552 KB Output is correct
55 Correct 152 ms 10576 KB Output is correct
56 Correct 52 ms 3936 KB Output is correct
57 Correct 59 ms 3928 KB Output is correct
58 Correct 155 ms 9028 KB Output is correct
59 Correct 146 ms 9080 KB Output is correct
60 Correct 151 ms 9044 KB Output is correct
61 Correct 137 ms 9048 KB Output is correct
62 Correct 148 ms 9072 KB Output is correct
63 Correct 148 ms 9064 KB Output is correct
64 Correct 921 ms 9456 KB Output is correct
65 Correct 869 ms 9464 KB Output is correct
66 Correct 813 ms 9652 KB Output is correct
67 Correct 24 ms 6796 KB Output is correct
68 Correct 70 ms 10492 KB Output is correct
69 Correct 72 ms 10576 KB Output is correct
70 Correct 116 ms 10056 KB Output is correct
71 Correct 112 ms 10116 KB Output is correct
72 Correct 112 ms 10064 KB Output is correct
73 Correct 122 ms 10544 KB Output is correct
74 Correct 114 ms 10452 KB Output is correct