• 【洛谷3959】 宝藏


    Solution

    考虑这题(n)这么小,肯定是什么状压或者搜索。
    考虑状压:
    (f_i)表示现在选的数的集合为(i)的最小费用,显然我们可以根据遍历点的顺序来确定点的深度。
    长度的话每一次选一个当前集合内的点向外更新,如果到达点不在集合内直接加进来判一下就好了。
    这个东西用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;
    }
    
  • 相关阅读:
    14-定时器
    13-JS中的面向对象
    12-关于DOM操作的相关案例
    11-DOM介绍
    10-关于DOM的事件操作
    09-伪数组 arguments
    08-函数
    07-常用内置对象
    06-流程控制
    05-数据类型转换
  • 原文地址:https://www.cnblogs.com/mleautomaton/p/10290499.html
Copyright © 2020-2023  润新知