8C - Looking for Order - CodeForces Solution


bitmasks dp *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
//#define int long long
//vector<vector<vector<int> > >dp(n+1,vector<vector<int> >(63,vector<int>(8,0)));
const long long inf=1e12;
long long dp[1<<24|1];
long long pre[1<<24|1];
struct node
{
    int x;
    int y;
}a[30];
long long dis[26][26];
void solve()
{
    cin>>a[0].x>>a[0].y;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;
    }
    for(int i=0;i<=(1<<n);i++){
        dp[i]=inf;
    }
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            dis[i][j]=(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y);
        }
    }
    pre[0]=0;
    dp[0]=0;
    for(int i=0;i<(1<<n);i++){
        if(dp[i]==inf){
            continue;
        }
        for(int j=1;j<=n;j++){
            if((i>>(j-1))&1){
                continue;
            }
            for(int k=1;k<=n;k++){
                if((i>>(k-1))&1){
                    continue;
                }
                if(dp[i]+dis[0][j]+dis[j][k]+dis[k][0]<dp[i|(1<<(j-1))|(1<<(k-1))]){
                    dp[i|(1<<(j-1))|(1<<(k-1))]=min(dp[i|(1<<(j-1))|(1<<(k-1))],dp[i]+dis[0][j]+dis[j][k]+dis[k][0]);
                    pre[i|(1<<(j-1))|(1<<(k-1))]=i;
                }
            }
            break;
        }
    }
    cout<<dp[(1<<n)-1]<<endl;
    long long cur=(1<<n)-1;
    while(cur){
        cout<<0<<' ';
        long long x=cur^pre[cur];
        for(int i=1;i<=n;i++){
            if((x>>(i-1))&1){
                cout<<i<<' ';
            }
        }
        cur=pre[cur];
    }
    cout<<0<<endl;
    return ;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    T=1;
    //cin>>T;
    while(T--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom