1. 程式人生 > >zoj 3010 The Lamp Game

zoj 3010 The Lamp Game

Little Tom likes playing games. Recently he is fond of a game called Lamp Game. The game is like this: at first, there are n lamps on the desk, all of which are lighted and Tom is given an initial score. Each time, Tom selects one lamp and changes its state, this action will costs little Tom some percent of score. What's more, the circuit of the lamps is designed so that, when Tom changes the state of one lamp, some related lamps' states will also be changed. The object of this game is to make all lamps off and make the total lost of score as low as possible. Of course little Tom is not smart enough to solve this problem, so he asks you, a student in ZJU, to help him.

Input

The input contains several test cases. Each case begins with two integers n and m (n<=10,0<m<=100000000), which are the number of lamps and the initial score of Tom. Then n lines follows, each in the format of t n1 n2 ...... nt f (0<=t<n,1<=ni<=n,0<=f<100). The i-th line means changing the state of the i-th lamp will make t related lamps' states changed and the following t integers give the t related lamps (these t numbers will not include i). And changing the state of the i-th lamp will cost Tom f percents of score. All numbers are integers. Two 0s of n and m signals the end of input.

Output

For each test case, output one line containing the maximum score Tom has when he finished the game successfully. The result should be accurate to 2 decimal places. If Tom cannot finish this game, simply output -1.

Sample Input:

3 100
1 2 50
0 50
1 2 50
0 0

Sample Output

12.50
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define max(a,b) (a>b ? a:b)

int a[20][20],b[20],c[20];

int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF&&m)
	{
		int i,j,k,t;
		memset(a,0,sizeof(a));
		for(i=1;i<=n;i++) {
			scanf("%d",&t);
			for(j=0;j<t;j++) {
				scanf("%d",&k);
				a[k][i]=1;
			}
			a[i][i]=a[i][n+1]=1;
			scanf("%d",&b[i]);
		}
		int res=0;
		for(j=1;j<=n;j++) {
			for(i=j;i<=n;i++) {
				if(a[i][j]!=0) {
					if(i!=j)
					for(k=1;k<=n+1;k++) swap(a[i][k],a[j][k]);
					break;
				}
			}
			if(a[j][j]==0) {
				c[res++]=j;
				continue;
			}
			for(i=1;i<=n;i++) if(i!=j){
				for(k=n+1;k>=j;k--)
				  a[i][k]=(a[i][k]-a[i][j]/a[j][j]*a[j][k]+2)%2;
			}
		}
		for(i=n-res+1;i<=n;i++)
		  if(a[i][n+1]!=0) break;
		if(i<=n) {
			printf("-1\n");
			continue;
		}
		double ans=0,cnt;
		for(i=0;i<(1<<res);i++) {
			cnt=m;
			for(k=0;k<res;k++)
			  if((i&(1<<k))) cnt*=(1-b[c[k]]/100.0);
			for(j=1;j<=n-res;j++) {
				t=1;
				for(k=0;k<res;k++)
				  if((i&(1<<k))) t+=a[j][c[k]];
				if(t%2==a[j][n+1]) cnt*=(1-b[j]/100.0);
			}
			ans=max(ans,cnt);
		}
		printf("%.2lf\n",ans);
	}
	return 0;
}

心得:Guass消元時,當方程組有多解時,要注意那幾個變數是任意的