답안 #56179

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
56179 2018-07-10T07:44:58 Z 노영훈(#1580) Cities (BOI16_cities) C++11
29 / 100
739 ms 115776 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 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});
    }
    ll ans[MX];
    fill(ans, ans+e+1, linf);
    priority_queue<edge> Q;
    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(){
    return 0;
}

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 20 ms 16760 KB Output is correct
2 Correct 19 ms 16868 KB Output is correct
3 Correct 18 ms 17072 KB Output is correct
4 Correct 21 ms 17072 KB Output is correct
5 Incorrect 19 ms 17072 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 546 ms 31556 KB Output is correct
2 Correct 443 ms 32080 KB Output is correct
3 Correct 176 ms 32080 KB Output is correct
4 Correct 173 ms 32080 KB Output is correct
5 Correct 438 ms 32080 KB Output is correct
6 Correct 115 ms 32080 KB Output is correct
7 Correct 17 ms 32080 KB Output is correct
8 Correct 21 ms 32080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 32080 KB Output is correct
2 Correct 26 ms 32080 KB Output is correct
3 Correct 18 ms 32080 KB Output is correct
4 Correct 21 ms 32080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 739 ms 115776 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 673 ms 115776 KB Output isn't correct
2 Halted 0 ms 0 KB -