• 洛谷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;
    }
    
  • 相关阅读:
    linux上搭建tingproxy服务
    windows上搭建linux系统
    PHP将图片流存为图片文件
    openLayer矩形框选要素,展示要素属性
    点击选中的要素,更换标注图片,并添加文本标注
    openLayer点击要素获取对应的属性信息
    openLayer实现放大缩小
    openLayer的切换中心点、定位功能
    使用openLayer加载arcgis中的地图
    openLayer实现两个地图联动
  • 原文地址:https://www.cnblogs.com/cjgjh/p/9813673.html
Copyright © 2020-2023  润新知