답안 #56186

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
56186 2018-07-10T07:55:04 Z 노영훈(#1580) Cities (BOI16_cities) C++11
74 / 100
1379 ms 63256 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX=100010, inf=2e9;
const ll linf=2e18;


struct edge{
    int to; ll co;
    bool operator < (const edge &e) const {
        return co>e.co;
    }
};
vector<edge> G0[MX], G1[MX*6];
int n, m, k;
int A[6];
ll dist[6][MX];

ll solve3(){
    ll ans=linf;
    for(int i=1; i<=n; i++){
        ll now=0;
        for(int j=1; j<=k; j++) now+=dist[j][i];
        ans=min(ans, now);
    }
    return ans;
}

ll S[MX];
ll ans[2*MX];
priority_queue<edge> Q;
ll doit(ll A[], ll B[]){
    int s=0, e=2*n+2;
    for(int i=s; i<=e; i++) G1[i].clear();
    for(int i=1; i<=n; i++){
        G1[0].push_back({i*2, A[i]+B[i]});
        G1[i*2+1].push_back({e, S[i]-A[i]-B[i]});

        for(edge &e:G0[i]){
            int x=e.to; ll c=e.co;
            G1[i*2].push_back({x*2+1, c});
            G1[i*2].push_back({x*2, c});
        }
        G1[i*2].push_back({i*2+1, 0});
    }
    fill(ans, ans+e+1, linf);
    while(!Q.empty()) Q.pop();
    ans[s]=0; Q.push({s,0});
    while(!Q.empty()){
        int v=Q.top().to; ll d=Q.top().co; Q.pop();
        if(ans[v]!=d) continue;
        for(edge &e:G1[v]){
            int x=e.to; ll c=e.co;
            if(d+c>=ans[x]) continue;
            ans[x]=d+c; Q.push({x,ans[x]});
        }
    }
    // for(int i=1; i<=n; i++) cout<<ans[i*2+1]<<' ';
    // cout<<'\n';
    return ans[e];
}

ll solve4(){
    ll ans=solve3();

    for(int i=1; i<=n; i++) S[i]=dist[1][i]+dist[2][i]+dist[3][i]+dist[4][i];

    ans=min({doit(dist[1], dist[2]), doit(dist[1], dist[3]), doit(dist[1], dist[4])});

    return ans;
}

ll solve5(){
    if(n>100) return 0;
    ll ans=linf;

    ll fl[101][101];
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            fl[i][j]=(i==j ? 0LL : linf);
    for(int i=1; i<=n; i++)
        for(edge &e:G0[i])
            fl[i][e.to]=fl[e.to][i]=e.co;
    
    for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                fl[i][j]=min(fl[i][j], fl[i][k]+fl[k][j]);

    for(int x=1; x<=n; x++)
        for(int y=1; y<=n; y++)
            for(int z=1; z<=n; z++){
                ll now=min({fl[x][y]+fl[y][z], fl[x][z]+fl[z][y], fl[z][x]+fl[x][y]});
                for(int i=1; i<=k; i++)
                    now+=min({dist[i][x], dist[i][y], dist[i][z]});
                // cout<<x<<' '<<y<<' '<<z<<": "<<now<<'\n';
                ans=min(ans, now);
            }
    return ans;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    cin>>n>>k>>m;
    for(int i=1; i<=k; i++) cin>>A[i];
    for(int i=1; i<=m; i++){
        int u,v,c; cin>>u>>v>>c;
        G0[u].push_back({v,c});
        G0[v].push_back({u,c});
    }

    for(int i=1; i<=k; i++){
        int st=A[i];
        ll *D=dist[i];
        for(int i=1; i<=n; i++) D[i]=linf;
        priority_queue<edge> Q;
        D[st]=0; Q.push({st, 0});
        while(!Q.empty()){
            int v=Q.top().to; ll d=Q.top().co; Q.pop();
            if(D[v]!=d) continue;
            for(edge &e:G0[v]){
                int x=e.to; ll c=e.co;
                if(d+c>=D[x]) continue;
                D[x]=d+c; Q.push({x,D[x]});
            }
        }
        // for(int i=1; i<=n; i++) cout<<D[i]<<' ';
        // cout<<'\n';
    }

    if(k<=3) cout<<solve3();
    else if(k<=4) cout<<solve4();
    else cout<<solve5();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 16760 KB Output is correct
2 Correct 16 ms 17000 KB Output is correct
3 Correct 16 ms 17000 KB Output is correct
4 Correct 19 ms 17000 KB Output is correct
5 Correct 19 ms 17004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 389 ms 31504 KB Output is correct
2 Correct 431 ms 32252 KB Output is correct
3 Correct 146 ms 32252 KB Output is correct
4 Correct 104 ms 32252 KB Output is correct
5 Correct 301 ms 32252 KB Output is correct
6 Correct 107 ms 32252 KB Output is correct
7 Correct 17 ms 32252 KB Output is correct
8 Correct 17 ms 32252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 32252 KB Output is correct
2 Correct 27 ms 32252 KB Output is correct
3 Correct 22 ms 32252 KB Output is correct
4 Correct 21 ms 32252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1362 ms 63256 KB Output is correct
2 Correct 1379 ms 63256 KB Output is correct
3 Correct 767 ms 63256 KB Output is correct
4 Correct 671 ms 63256 KB Output is correct
5 Correct 285 ms 63256 KB Output is correct
6 Correct 170 ms 63256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 562 ms 63256 KB Output isn't correct
2 Halted 0 ms 0 KB -