Submission #281789

# Submission time Handle Problem Language Result Execution time Memory
281789 2020-08-23T13:18:39 Z 3zp Cities (BOI16_cities) C++14
100 / 100
1703 ms 39032 KB
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mN = 2e5 + 9;
ll Sp[mN], D[5][mN], F[5][5][mN], Vis[mN];
vector<pair<int,ll> > V[mN];
int n, k, m;
void dij(int a, int b, ll *Ans){
    priority_queue<pair<ll, int> > Q;
    for(int i = 1; i <= n; i++){
        Vis[i] = 0;
        if(b == 0) Ans[i] = 1e18;
        else {
            Ans[i] = D[a][i] + D[b][i];
            Q.push({-Ans[i], i});
        }
    }
    if(b == 0){
        Ans[a] = 0;
        Q.push({0, a});
    }
    while(Q.size()){
        int x = Q.top().second;
        Q.pop();
        if(Vis[x]) continue;
        Vis[x] = 1;
        for(auto E : V[x]){
            int y = E.first, l = E.second;
            if(!Vis[y] && Ans[y] > Ans[x] + l){
                Ans[y] = Ans[x] + l;
                Q.push({-Ans[y], y});
            }
        }
    }
}
main(){
    cin >> n >> k >> m;
    for(int i = 0; i < k; i++){
        cin >> Sp[i];
    }
    for(int i = 0; i < m; i++){
        int a, b, c;
        cin >> a >> b >> c;
        V[a].push_back({b, c});
        V[b].push_back({a, c});
    }
    for(int i =0; i < k; i++){
        dij(Sp[i], 0, D[i]);
    }
    for(int i = 0; i < k; i++){
        for(int j = i+1; j < k; j++){
            dij(i, j, F[i][j]);
        }
    }
    if(k == 2){
        cout << D[0][Sp[1]] << endl;
    }
    if(k == 3){
        cout << F[0][1][Sp[2]]<<endl;
    }
    if(k == 4){
        ll ans = 1e18;
        vector<int> P = {0,1,2,3};
        do{
            if(P[0] > P[1] || P[2] > P[3]) continue;
            for(int i = 1; i <= n; i++)
                ans = min(ans, D[P[0]][i] + D[P[1]][i] + F[P[2]][P[3]][i]);
        }while(next_permutation(P.begin(), P.end()));
        cout<<ans<<endl;
    }
    if(k == 5){
        ll ans = 1e18;
        vector<int> P = {0,1,2,3,4};
        do{
            if(P[1] > P[2] || P[3] > P[4]) continue;
            for(int i = 1; i <= n; i++)
                ans = min(ans, D[P[0]][i] + F[P[1]][P[2]][i] +F[P[3]][P[4]][i]);
        }while(next_permutation(P.begin(), P.end()));
        cout<<ans<<endl;
    }

}

Compilation message

cities.cpp:36:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   36 | main(){
      |      ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 906 ms 31676 KB Output is correct
2 Correct 891 ms 31196 KB Output is correct
3 Correct 473 ms 21528 KB Output is correct
4 Correct 361 ms 17784 KB Output is correct
5 Correct 702 ms 27088 KB Output is correct
6 Correct 364 ms 17592 KB Output is correct
7 Correct 10 ms 5376 KB Output is correct
8 Correct 9 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 5376 KB Output is correct
2 Correct 10 ms 5376 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 9 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1240 ms 34800 KB Output is correct
2 Correct 1192 ms 34612 KB Output is correct
3 Correct 706 ms 24664 KB Output is correct
4 Correct 897 ms 27676 KB Output is correct
5 Correct 488 ms 19408 KB Output is correct
6 Correct 367 ms 19832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1627 ms 39032 KB Output is correct
2 Correct 1703 ms 38840 KB Output is correct
3 Correct 1592 ms 38120 KB Output is correct
4 Correct 960 ms 28832 KB Output is correct
5 Correct 1008 ms 29396 KB Output is correct
6 Correct 510 ms 20056 KB Output is correct
7 Correct 380 ms 19680 KB Output is correct