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;
}

 

posted @ 2015-09-23 11:01  chenjunjie1994  阅读(164)  评论(0编辑  收藏  举报