AtCoder Beginner Contest 190 E-Magical Ornament (graph theory + state pressure dp)

Question:
Given n kinds of gems, there are m kinds of relationships. Now we need to place some gems in a row. Only the gems with relationships can be adjacent, and the gems in the c array must be placed. How many gems should be placed at least?

Solution:
I didn't expect this question to be more difficult than F question. . .
In fact, to transform the problem a bit is: build the edge of the given relationship, and then find a shortest path in the graph so that it can contain all the points needed.
First, we need to find the shortest path between the points that must be placed. Because k is very small, we can perform bfs k times to find the shortest distance between two points.
Then there is a problem of pressure dp. Let dp[i][j] denote the smallest path where state i is the last point j. The state i becomes binary, and the i-th bit is 1 indicating that c[i] has passed. Then the transfer equation can be derived:
dp[next_i][next_j]=min(dp[next_i][next_j],dp[i][j]+dis[j][next_j]).
At the end of the enumeration, min is the last point reached.

Code:

#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int>pii;
const int MAXN=1e5+200;
const int mod=77797;
const int inf=0x3f3f3f3f;
int n,m;
int k;
int c[20];
std::vector<int> v[MAXN];
ll dis[20][20];
int vis[MAXN];
ll d[MAXN];
ll dp[(1<<18)][20];
void bfs(int s)
{
    
    
    d[s]=0;
    dis[vis[s]][vis[s]]=0;
    queue<int >q;
    q.push(s);
    while(!q.empty())
    {
    
    
        int now=q.front();
        q.pop();
        int len=v[now].size();
        for(int i=0;i<len;i++)
        {
    
    
            if(d[v[now][i]]==inf)
            {
    
    
                d[v[now][i]]=d[now]+1;
                q.push(v[now][i]);
                if(vis[v[now][i]]) 
                {
    
    
                    //cout<<s<<" "<<v[now][i]<<endl;
                    dis[vis[s]][vis[v[now][i]]]=d[v[now][i]];
                }
            }
        }
    }
}
int main()
{
    
    
    memset(dis,inf,sizeof dis);
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
    
    
        int uu,vv;
        cin>>uu>>vv;
        v[uu].push_back(vv);
        v[vv].push_back(uu);
    }
    cin>>k;
    for(int i=1;i<=k;i++)
    {
    
    
        cin>>c[i];
        vis[c[i]]=i;
    }
    for(int i=1;i<=k;i++)
    {
    
    
        for(int j=1;j<=n;j++)
        {
    
    
            d[j]=inf;
        }
        bfs(c[i]);
    }
    /*for(int i=1;i<=k;i++)
    {
        for(int j=1;j<=k;j++)
        {
            cout<<c[i]<<" "<<c[j]<<" "<<dis[c[i]][c[j]]<<endl;
        }
    }*/
    memset(dp,inf,sizeof dp);
    for(int i=1;i<=k;i++)
    {
    
    
        dp[1<<(i-1)][i]=1;
    }
    for(int i=1;i<=(1<<k)-1;i++)
    {
    
    
        for(int j=1;j<=k;j++)
        {
    
    
            if(dp[i][j]==inf) continue;
            for(int z=1;z<=k;z++)
            {
    
    
                if(z==j) continue;
                if((1<<(z-1))&i) continue;
                dp[i|(1<<(z-1))][z]=min(dp[i|(1<<(z-1))][z],dp[i][j]+dis[j][z]);
            }
        }
    }
    ll ans=inf;
    for(int i=1;i<=k;i++)
    {
    
    
        //cout<<dp[(1<<k)-1][i]<<endl;
        ans=min(ans,dp[(1<<k)-1][i]);
    }
    if(ans==inf)printf("-1\n");
    else printf("%lld\n",ans);
}



Guess you like

Origin blog.csdn.net/weixin_45755679/article/details/113481300