UVA 10817-headmaster ' s headache

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.