Topic Link: Click to enter
Start to see M and K are very small, the direct violence, the result of T. After seeing M is only 6 so big, K and less than 10, I think we can use the memory search, state we are set as DP[N1][N2][N3][N4][N5][N6], indicating the amount of n1–n6 six kinds of chemical reagents, When the state transitions, select two reagents to mix.
The code is as follows:
#include <iostream>#include <cstring>#include <cstdio>using namespace STD;#define INF 0x3f3f3f3fint Constmaxn= A;intDP[MAXN][MAXN][MAXN][MAXN][MAXN][MAXN];intVIS[MAXN][MAXN][MAXN][MAXN][MAXN][MAXN];typedef struct{intX,hot;} P P p[Ten][Ten];inta[ -],ans;intDfsintAintBintCintDintEintFintN) {if(n==1)return 0;int& Res=dp[a][b][c][d][e][f];int& Flag=vis[a][b][c][d][e][f];if(flag)returnResintnum[7]={0, a,b,c,d,e,f};intTmp=inf; for(intI=1; i<=6; i++) for(intj=1; j<=6; j + +) {if(i==j&&num[i]<2)Continue;if(num[i]==0|| num[j]==0)Continue; num[i]--; num[j]--; num[p[i][j].x]++; Tmp=min (Tmp,dfs (num[1],num[2],num[3],num[4],num[5],num[6],n-1) +p[i][j].hot); num[i]++;num[j]++;num[p[i][j].x]--; } flag=1; res=tmp;returnTMP;}intMain () {//Freopen ("In.txt", "R", stdin); intt,m,k,num[7];Charstr[Ten];scanf("%d", &t); for(intt=1; t<=t; t++) {if(t!=1)scanf('%s ', str);scanf("%d", &m); for(intI=1; i<=m;i++) for(intj=1; j<=m;j++) {scanf("%d%d", &p[i][j].x,&p[i][j].hot); }scanf("%d", &k);memset(Vis,0,sizeof(VIS));memset(Num,0,sizeof(num)); for(intI=1; i<=k;i++) {intXscanf("%d", &x); num[x]++; } DFS (num[1],num[2],num[3],num[4],num[5],num[6],K);printf("%d\n", dp[num[1]][num[2]][num[3]][num[4]][num[5]][num[6]]); }scanf('%s ', str);return 0;}
UVA 10604--chemical reaction+ Memory Search