Headmaster‘s Headache
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit Status Practice UVA 10817
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 const int inf=1e9+7; 6 const int mm=65536; 7 8 int dp[105][65590]; 9 int appcost[105],appnum[105][30]; 10 11 int main() 12 { 13 int s,n,m,x,y; 14 int i,j,k; 15 int sum0,k0; 16 char a[103]; 17 while(scanf("%d %d %d",&s,&m,&n)!=EOF) 18 { 19 if(s==0) 20 break; 21 memset(appnum,0,sizeof(appnum)); 22 23 sum0=0;k0=0; 24 for(i=1;i<=m;i++) 25 { 26 scanf("%d",&x); 27 sum0=sum0+x; 28 gets(a); 29 for(j=0;a[j]!=‘\0‘;j++) 30 { 31 if(‘1‘<=a[j] && a[j]<=‘9‘) 32 { 33 y=a[j]-‘0‘;y--; 34 if(!((k0>>(2*y)) & 1)) 35 { 36 k0=k0 | (1<<(2*y)); 37 } 38 else if(!((k0>>(2*y+1)) & 1)) 39 { 40 k0=k0 | (1<<(2*y+1)); 41 } 42 } 43 } 44 } 45 for(i=1;i<=n;i++) 46 { 47 scanf("%d",&appcost[i]); 48 gets(a); 49 int num=0; 50 for(j=0;a[j]!=‘\0‘;j++) 51 { 52 if(‘1‘<=a[j] && a[j]<=‘9‘) 53 { 54 //y=a[j]-‘0‘;y--; 55 num++; 56 appnum[i][num]=a[j]-‘0‘; 57 } 58 } 59 appnum[i][0]=num; 60 } 61 62 int S=1<<(2*s); 63 for(i=0;i<S;i++) 64 { 65 dp[0][i]=inf; 66 } 67 dp[0][k0]=sum0; 68 69 for(i=1;i<=n;i++) 70 { 71 for(j=0;j<S;j++) 72 { 73 dp[i][j]=dp[i-1][j]; 74 } 75 76 for(j=0;j<S;j++) 77 { 78 if(dp[i-1][j]<inf) 79 { 80 k=j; 81 for(int l=1;l<=appnum[i][0];l++) 82 { 83 y=appnum[i][l]-1; 84 if(!((k>>(2*y)) & 1)) 85 { 86 k=k | (1<<(2*y)); 87 } 88 else if(!((k>>(2*y+1)) & 1)) 89 { 90 k=k | (1<<(2*y+1)); 91 } 92 } 93 dp[i][k]=min(dp[i][k],dp[i-1][j]+appcost[i]); 94 } 95 } 96 } 97 printf("%d\n",dp[n][(1<<(2*s))-1]); 98 } 99 return 0; 100 }
UVA 10817 十一 Headmaster's Headache
时间: 2024-11-06 03:45:28