Title: Https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&page=show_ problem&problem=1758
State-Compressed Dp,dp[i][st] indicates that the state for St considers the back I personal owner minimum cost,
Because there are three states for each account, it can be represented by a three-decimal number,
There are not many states, so you can pre-process each number of the three-step,
Decisions are chosen and not selected.
#include <bits/stdc++.h>using namespacestd;Const intMaxs =8;Const intMaxsta =6561+5;Const intMAXN =101;int Base[maxs+1],trp[maxsta][maxs+1];intDp[maxn][maxsta];intTch[maxn],cost[maxn],sa;intS,m,n;Const intINF =0x3fffffff;voidPredeal () {Base[0] =1; for(inti =1; I <= maxs; i++){ Base[I] =3*Base[I1]; } for(inti =1; i < Maxsta; i++){ intx = i, cnt =0; while(x) {trp[i][cnt+ +] = x percent3; X/=3; } }}intDfsintIintSt) { if(i = = N)returnSt = = sa?0: INF; int&ans =Dp[i][st]; if(~ans)returnans; Ans= DFS (i+1, ST); //int nst = st; for(intj =0; J < S; J + +){ if(Trp[tch[i]][j] && trp[st][j]! =2) {St+=Base[j]; }} ans= Min (Ans,dfs (i+1, ST) +Cost[i]); returnans;}intWork () {SA=Base[s]-1; intSt =0, sum =0; for(inti =0; I < m; i++){ intC scanf"%d",&c); Sum+=C; while(GetChar ()! ='\ n'){ intSub = GetChar ()-'1'; intt =Trp[st][sub]; if(t! =2) {St+=Base[Sub]; } } } for(inti =0; I < n; i++) {scanf ("%d", cost+i); Tch[i]=0; while(GetChar ()! ='\ n') {Tch[i]+=Base[GetChar ()-'1']; }} memset (DP,-1,sizeof(DP)); returnSum + DFS (0, ST);}intMain () {//freopen ("In.txt", "R", stdin);Predeal (); while(SCANF ("%d%d%d",&s,&m,&N), s) {printf ("%d\n", work ()); } return 0;}
UVA 10817-headmaster ' s headache