zoukankan      html  css  js  c++  java
  • 洛谷3959 宝藏(状压DP)

    题面

    原题

    Solution

    显然我们看到(n)这么小,可以状压一下对吧。
    然后考虑一个(DP)
    (dp[s])表示选的数为s这个集合的答案,那么可以这么转移:
    (dp[s|(1<<v-1)]=max(dp[s|1<<v-1)],dp[s]+w[i]*dep[u])
    然后就直接dfs算一下深度然后乱搞?

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    #define ll long long
    #define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
    using namespace std;
    inline int gi(){
    	int sum=0,f=1;char ch=getchar();
    	while(ch>'9' || ch<'0'){if(ch=='-')f=-f;ch=getchar();}
    	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    	return f*sum;
    }
    inline ll gl(){
    	ll sum=0,f=1;char ch=getchar();
    	while(ch>'9' || ch<'0'){if(ch=='-')f=-f;ch=getchar();}
    	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    	return f*sum;
    }
    const int maxn=20;
    ll dp[200010];
    int n,dep[maxn],g[maxn][maxn];
    void Dp(int s){//挖通的集合
    	for(int u=1;u<=n;u++)
    		if(s&(1<<u-1))
    			for(int v=1;v<=n;v++){
    				if(!(s&(1<<v-1)) && g[u][v]!=g[0][0] && dp[s]+g[u][v]*dep[u]<dp[s|(1<<v-1)]){
    					dp[s|(1<<v-1)]=dp[s]+g[u][v]*dep[u];int tmp=dep[v];dep[v]=dep[u]+1;
    					Dp(s|(1<<v-1));
    					dep[v]=tmp;
    				}
    			}
    }
    int main(){
    	int i,j,m,k;
    	n=gi();m=gi();memset(g,127,sizeof(g));
    	for(i=1;i<=m;i++){
    		int u=gi(),v=gi(),W=gi();
    		if(W<g[u][v])g[u][v]=g[v][u]=W;
    	}
    	ll ans=1000000000;
    	for(i=1;i<=n;i++){
    		memset(dp,127,sizeof(dp));memset(dep,0,sizeof(dep));
    		dp[1<<(i-1)]=0;dep[i]=1;
    		Dp(1<<i-1);
    		ans=min(ans,dp[(1<<n)-1]);
    	}
    	printf("%lld
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    Photoshop 基础七 位图 矢量图 栅格化
    Photoshop 基础六 图层
    Warfare And Logistics UVALive
    Walk Through the Forest UVA
    Airport Express UVA
    Guess UVALive
    Play on Words UVA
    The Necklace UVA
    Food Delivery ZOJ
    Brackets Sequence POJ
  • 原文地址:https://www.cnblogs.com/cjgjh/p/9813673.html
Copyright © 2011-2022 走看看