MnZn求助 记忆化搜索T了#10
查看原帖
MnZn求助 记忆化搜索T了#10
752021
i099i1aa6c楼主2022/7/29 22:53
#include <iostream>
#include <cstring>
#include <bitset>
#include <cstdio>
using namespace std;
//#define FIO //开启加速模式 将不能使用键盘读入
struct IO{
#ifdef FIO
	const static int BUFSIZE=1<<20;char buf[BUFSIZE],obuf[BUFSIZE],*p1,*p2,*pp;inline char gc(){return(p1==p2&&(p2=(p1=buf)+fread(buf,1,BUFSIZE,stdin),p1==p2)?EOF:*p1++);}inline void pc(char x){((pp-obuf==BUFSIZE&&(fwrite(obuf,1,BUFSIZE,stdout),pp=obuf)),*pp=x,pp++);}inline void flush(){fwrite(obuf,1,pp-obuf,stdout);}IO(){p1=buf,p2=buf,pp=obuf;}~IO(){fwrite(obuf,1,pp-obuf,stdout);}
#else
	int(*gc)()=&getchar;int(*pc)(int)=&putchar;inline void flush(){};
#endif
	template<typename Tp=int>inline Tp read(){Tp s=0;int f=1;char ch=gc();while(!isdigit(ch))f=(ch=='-'?-1:1),ch=gc();while(isdigit(ch))s=s*10+(ch^48),ch=gc();return s*f;}template<typename Tp>void read(Tp&x){x=read();}template<typename Tp,typename...Ts>void read(Tp&x,Ts&...val){x=read<Tp>();read(val...);}template<typename Tp>void write(Tp x){if(x<0)pc('-'),x=-x;static char sta[20];int top=0;do sta[top++]=x%10+'0',x/=10;while(x);while(top)pc(sta[--top]);}template<typename Tp,typename...Ts>void write(Tp x,Ts...val){write(x);pc(' ');write(val...);}template<typename...Ts>void writeln(Ts...val){write(val...);pc('\n');}}io;
int n,max_state,arr[30][30],mem[30][1<<20|1];
int dfs(int u,int state){
	if(state==max_state)	return u==1?0:0x3f3f3f3f;
	if(mem[u][state]!=-1)	return mem[u][state];
	int res = 2147483647;
	for(int i=1;i<=n;i++){
		if(i!=u&&!(state>>(i-1)&1))
			res = min(res,dfs(i,state|(1<<(i-1)))+arr[u][i]);
	}
	return mem[u][state] = res;
}
int main(){
	memset(mem,-1,sizeof(mem));
	n = io.read(),max_state = (1 << n) - 1;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			arr[i][j] = io.read();
	io.writeln(dfs(1,0));
	return 0;
}

加了快读和 O2O_2 也过不去

:(

2022/7/29 22:53
加载中...