zoukankan      html  css  js  c++  java
  • CF #321 (Div. 2) D

    不说了,爆内存好几次,后来醒起状态有重复。。。

    状压+TSP

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #define LL long long
    using namespace std;
    //#pragma comment(linker, "/STACK:102400000,102400000")
    const int MAX=100005;
    
    int pre[20][20];
    int dish[20];
    
    LL dp[20][1<<20];
    
    template<class T>
    inline T IN(T &num){
        num = 0;
        char c = getchar(), f = 0;
        while(c != '-' && (c < '0' || c > '9')) c = getchar();
        if(c == '-') f = 1, c = getchar();
        while('0' <= c && c <= '9') num = num * 10 + c - '0', c = getchar();
        if(f) num = -num;
        return num;
    }
    
    struct Status{
    	int l,s;
    	Status(){};
    	Status(int ll,int ss){l=ll,s=ss;}
    };
    
    int main(){
    	int n,m,k;
    	int x,y,c;
    	
    	while(scanf("%d%d%d",&n,&m,&k)!=EOF){
    		queue<Status>que;
    		memset(pre,0,sizeof(pre));
    		memset(dp,-1,sizeof(dp));
    		for(int i=1;i<=n;i++)
    			IN(dish[i]);
    		for(int i=1;i<=k;i++){
    			IN(x),IN(y),IN(c);
    			pre[x][y]=c;
    		}
    		LL ans=0;
    		for(int i=1;i<=n;i++){
    			dp[i][1<<i]=dish[i];
    			ans=max(ans,(LL)dish[i]);
    			que.push(Status(i,1<<i));
    		}
    		Status tmp; int e;
    		for(int i=2;i<m;i++){
    			int sz=que.size();
    			while(sz--){
    				tmp=que.front();
    				que.pop();
    				for(int j=1;j<=n;j++){
    					if(tmp.s&(1<<j)){
    					}
    					else{
    						k=tmp.l; e=tmp.s|(1<<j);
    						if(dp[j][e]==-1) que.push(Status(j,e));
    						dp[j][e]=max(dp[j][e],dp[k][tmp.s]+pre[k][j]+dish[j]);
    					}
    				}
    			}
    		}
    		if(m>=2){
    			while(!que.empty()){
    				tmp=que.front();
    				que.pop();
    				for(int j=1;j<=n;j++){
    					if(tmp.s&(1<<j)){
    					}
    					else{
    						k=tmp.l; e=tmp.s|(1<<j);
    						dp[j][e]=max(dp[j][e],dp[k][tmp.s]+pre[k][j]+dish[j]);
    						ans=max(ans,dp[j][e]);
    					}
    				}
    			}
    		}
    		cout<<ans<<endl;
    		
    	}
    	return 0;
    }
    
  • 相关阅读:
    c# 设计模式(一) 工厂模式
    微信开发
    一款非常好用的 Windows 服务开发框架,开源项目Topshelf
    基础语法
    C++环境设置
    c++简介
    使用查询分析器和SQLCMD分别登录远程的SQL2005的1434端口
    ps-如何去水印
    html/css/js-横向滚动条的实现
    java中如何给控件设置颜色
  • 原文地址:https://www.cnblogs.com/jie-dcai/p/4831593.html
Copyright © 2011-2022 走看看