Agent 47 LightOJ - 1037

Agent 47 is in a dangerous Mission "Black Monster Defeat - 15". It is a secret mission and so 47 has a limited supply of weapons. As a matter of fact he has only one weapon the old weak "KM .45 Tactical (USP)". The mission sounds simple - "he will encounter at most 15 Targets and he has to kill them all". The main difficulty is the weapon. After huge calculations, he found a way out. That is after defeating a target, he can use target's weapon to kill other targets. So there must be an order of killing the targets so that the total number of weapon shots is minimized. As a personal programmer of Agent 47 you have to calculate the least number of shots that need to be fired to kill all the targets.

 

Agent 47

Now you are given a list indicating how much damage each weapon does to each target per shot, and you know how much health each target has. When a target's health is reduced to 0 or less, he is killed. 47 start off only with the KM .45 Tactical (USP), which does damage 1 per shot to any target. The list is represented as a 2D matrix with the ith element containing N single digit numbers ('0'-'9'), denoting the damage done to targets 0, 1, 2, ..., N-1 by the weapon obtained from target i, and the health is represented as a series of N integers, with the ith element representing the amount of health that target has.

Given the list representing all the weapon damages, and the health each target has, you should find the least number of shots he needs to fire to kill all of the targets.

Input

Input starts with an integer T (≤ 40), denoting the number of test cases.

Each case begins with a blank line and an integer N (1 ≤ N ≤ 15). The next line contains N space separated integers between 1 and 106 denoting the health of the targets 0, 1, 2, ..., N-1. Each of the next N lines contains N digits. The jth digit of the ith line denotes the damage done to target j, if you use the weapon of target i in each shot.

Output

For each case of input you have to print the case number and the least number of shots that need to be fired to kill all of the targets.

Sample Input

2

 

3

10 10 10

010

100

111

 

3

3 5 7

030

500

007

Sample Output

Case 1: 30

Case 2: 12

-----------------------------------------------------------------

 

1.判断一个数字x二进制下第i位是不是等于1。

方法:if ( ( ( 1 << ( i - 1 ) ) & x ) > 0)

将1左移i-1位,相当于制造了一个只有第i位上是1,其他位上都是0的二进制数。然后与x做与运算,如果结果>0,说明x第i位上是1,反之则是0。

2.将一个数字x二进制下第i位更改成1。

方法:x = x | ( 1<<(i-1) )

证明方法与1类似,此处不再重复证明。

3.把一个数字二进制下最靠右的第一个1去掉。

方法:x=x&(x-1)

-----------------------------------------------------------------摘自http://blog.csdn.net/u011077606/article/details/43487421

题意:你现在需要消灭n个敌人,n个敌人的血量已知,你的普通攻击力为1,但是如果你杀死敌人i可以用它的武器去杀死其他敌人,map[i][j] 表示用敌人i的武器射杀敌人j会减map[i][j]滴血.问你最少可以攻击多少次可以将敌人杀死。

 

 1 #define INF 1e8
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 int n;
 9 int p[16],map[16][16];
10 int dp[1<<16];
11 
12 int solve(){
13     memset(dp,0x3f,sizeof(dp));
14     dp[0]=0;
15     for(int s=0;s<(1<<n)-1;s++){               //所有的状态
16         for(int i=0;i<n;i++){                  //找当前状态小没死的人
17             if(s&(1<<i)) continue;
18             int k,m,t=INF;
19             for(int j=0;j<n;j++){              //找当前状态下已经死掉的人并用他们的武器攻击 i 
20                 if(s&(1<<j)&&map[j][i]!=0){            
21                     m=p[i]/map[j][i];
22                     k=(p[i]%map[j][i])? m+1:m;
23                     t=min(t,k);
24                 }
25             }
26             if(t==INF) t=p[i];                 //如果所有的攻击值都为0,那么就用基础攻击。
27             dp[s|(1<<i)]=min(dp[s|(1<<i)],dp[s]+t);
28         }
29     }
30     return dp[(1<<n)-1];
31 } 
32 
33 int main()
34 {   int kase;
35     cin>>kase;
36     for(int t=1;t<=kase;t++){
37         cin>>n;
38         for(int i=0;i<n;i++) scanf("%d",&p[i]);
39         string s[16];
40         for(int i=0;i<n;i++) cin>>s[i];
41         for(int i=0;i<n;i++)
42             for(int j=0;j<n;j++) 
43                 map[i][j]=s[i][j]-'0';
44         printf("Case %d: %d\n",t,solve());
45     }
46     return 0;
47 }

 

 

 

posted @ 2017-08-10 13:09  天之道,利而不害  阅读(219)  评论(0编辑  收藏  举报