Submission #281790

# Submission time Handle Problem Language Result Execution time Memory
281790 2020-08-23T13:22:39 Z 3zp Cities (BOI16_cities) C++14
100 / 100
1300 ms 34688 KB
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mN = 2e5 + 9;
int Sp[mN];
ll 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(){
    scanf("%d %d %d", &n, &k, &m);
    for(int i = 0; i < k; i++){
        scanf("%d", &Sp[i]);
    }
    for(int i = 0; i < m; i++){
        int a, b, c;
        scanf("%d %d %d",&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:37:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   37 | main(){
      |      ^
cities.cpp: In function 'int main()':
cities.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   38 |     scanf("%d %d %d", &n, &k, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   40 |         scanf("%d", &Sp[i]);
      |         ~~~~~^~~~~~~~~~~~~~
cities.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   44 |         scanf("%d %d %d",&a,&b,&c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 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 585 ms 27340 KB Output is correct
2 Correct 562 ms 26776 KB Output is correct
3 Correct 327 ms 19352 KB Output is correct
4 Correct 113 ms 14164 KB Output is correct
5 Correct 368 ms 22860 KB Output is correct
6 Correct 98 ms 14008 KB Output is correct
7 Correct 6 ms 5248 KB Output is correct
8 Correct 5 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5376 KB Output is correct
2 Correct 8 ms 5376 KB Output is correct
3 Correct 5 ms 5248 KB Output is correct
4 Correct 6 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 866 ms 30628 KB Output is correct
2 Correct 876 ms 29904 KB Output is correct
3 Correct 528 ms 22560 KB Output is correct
4 Correct 569 ms 23240 KB Output is correct
5 Correct 187 ms 15636 KB Output is correct
6 Correct 114 ms 16248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1300 ms 34688 KB Output is correct
2 Correct 1257 ms 34480 KB Output is correct
3 Correct 1198 ms 33936 KB Output is correct
4 Correct 754 ms 26692 KB Output is correct
5 Correct 678 ms 25236 KB Output is correct
6 Correct 217 ms 16156 KB Output is correct
7 Correct 124 ms 16248 KB Output is correct