一類哈密頓路徑/迴路為背景的狀壓dp

potential-star發表於2024-03-29

https://codeforces.com/contest/1950/problem/G

在非連通圖上找到一條包含點最多的路徑,dp陣列維護可達性

// Problem: G. Shuffling Songs
// Contest: Codeforces - Codeforces Round 937 (Div. 4)
// URL: https://codeforces.com/contest/1950/problem/G
// Memory Limit: 256 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
//# define int long long
#define ull unsigned long long
#define pii pair<int,int>

#define baoliu(x, y) cout << fixed << setprecision(y) << x
#define endl  "\n"
#define debug1(x) cerr<<x<<" "
#define  debug2(x) cerr<<x<<endl
const int N = 2e5 + 10;
const int M = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
int n, m;
int a[N];

void solve(){
	vector<string>v1;
	vector<string>v2;
		cin>>n;
	vector<vector<int>>w(n+1,vector<int>(n+1,0));
	
	for(int i=0;i<n;i++){
		string s1,s2;cin>>s1>>s2;
		v1.push_back(s1);
		v2.push_back(s2);
	}
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			//if(j==i)continue;
			if(v1[i]==v1[j]||v2[i]==v2[j]){
				w[i][j]=1;w[j][i]=1;
			}
		}
	}
	// for(int i=0;i<n;i++){
		// for(int j=0;j<n;j++){
			// cerr<<w[i][j];
		// }
		// cerr<<endl;
	// }
	//尋找答案狀態:列舉最終的狀態和終點,從而計算答案
	int ans=0;
	vector<vector<int>>dp((1<<n)+1,vector<int>(n+1,0));
	//fill(dp[0].begin(),dp[0].end(),1);
	for(int i=0;i<n;i++)dp[1<<i][i]=1;
	for(int i=0;i<(1<<n);i++){
		for(int j=0;j<n;j++){
			int u=(i>>j)&1;
			if(u==0)continue;
			for(int k=0;k<n;k++){
				//為什麼不能判斷終點j和k轉移重複
				//這會導致初始只有一個0基礎態無法轉移
				int v=(i>>k)&1;
				if(v==0)continue;
				int last=i-(1<<j);
	dp[i][j]|=dp[last][k]&w[k][j];
//	cerr<<i<<" "<<j<<endl;
	if(dp[i][j]){
		ans=max(ans,__builtin_popcount(i));
	}
	
			}
		}
	}
	
	cout<<n-ans<<endl;
}
int main() {
    cin.tie(0);
    
    ios::sync_with_stdio(false);

    int t;
   cin>>t;
     //t=1;
    while (t--) {
solve();
    }
    return 0;
}

最短哈密頓路徑

https://www.acwing.com/problem/content/93/

主要就是需要不重不漏的走一遍並且維護最短路,這無法在多項式時間內完成,在n不大的時候我們用二級制列舉所有狀態,列舉終點作為狀態,轉移是透過列舉上一個點。

時間複雜度:O(\(2^n*n^2\))

// Problem: 最短Hamilton路徑
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/93/
// Memory Limit: 256 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
//# define int long long
#define ull unsigned long long
#define pii pair<int,int>

#define baoliu(x, y) cout << fixed << setprecision(y) << x
#define endl  "\n"
#define debug1(x) cerr<<x<<" "
#define  debug2(x) cerr<<x<<endl
const int N = 21;
const int M = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
int n, m;
int w[N][N];
//首先思考為什麼這裡最短路不能用普通的最短路
//首先它需要把所有點都遍歷到,而普通最短路根本不在乎這個
int dp[1<<20][N];
void solve(){
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>w[i][j];
		}
	}
	
	memset(dp,0x3f ,sizeof dp);
	dp[1][0]=0;
	for(int i=0;i<(1<<n);i++){
		for(int j=0;j<n;j++){//列舉當前終點
			if(((i>>j)&1)==0)continue;
			for(int k=0;k<n;k++){//列舉上一次轉移點
				if(((i>>k)&1)==0)continue;
				int pre=i-(1<<j);
				if(k==j)continue;
				dp[i][j]=min(dp[i][j],dp[pre][k]+w[k][j]);
			}
		}
	}
	int ed=(1<<n)-1;
	cout<<dp[ed][n-1];
}
int main() {
    cin.tie(0);
    
    ios::sync_with_stdio(false);

    int t;
   //cin>>t;
     t=1;
    while (t--) {
solve();
    }
    return 0;
}

最短哈密頓迴路

https://www.luogu.com.cn/problem/P1171

// Problem: P1171 售貨員的難題
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1171
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
//# define int long long
#define ull unsigned long long
#define pii pair<int,int>

#define baoliu(x, y) cout << fixed << setprecision(y) << x
#define endl  "\n"
#define debug1(x) cerr<<x<<" "
#define  debug2(x) cerr<<x<<endl
const int N =21 ;
const int M = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
int n, m;
int a[N];

int w[N][N];
//首先思考為什麼這裡最短路不能用普通的最短路
//首先它需要把所有點都遍歷到,而普通最短路根本不在乎這個
int dp[1<<20][N];
void solve(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>w[i][j];
        }
    }

    memset(dp,0x3f ,sizeof dp);
    dp[1][0]=0;
    for(int i=0;i<(1<<n);i++){
        for(int j=0;j<n;j++){//列舉當前終點
            if(((i>>j)&1)==0)continue;
            for(int k=0;k<n;k++){//列舉上一次轉移點
                if(((i>>k)&1)==0)continue;
                int pre=i-(1<<j);
                if(k==j)continue;
                dp[i][j]=min(dp[i][j],dp[pre][k]+w[k][j]);
            }
        }
    }
    int ed=(1<<n)-1;
    int ans=inf;
    for(int i=1;i<n;i++){
    	ans=min(ans,dp[ed][i]+w[i][0]);
    }
    cout<<ans<<endl;
}


int main() {
    cin.tie(0);
    
    ios::sync_with_stdio(false);

    int t;
   //cin>>t;
     t=1;
    while (t--) {
solve();
    }
    return 0;
}

相關文章