现在的位置: 首页 > 综合 > 正文

zoj3010The Lamp Game(状态压缩+SPFA求最大路)经典

2018年02月22日 ⁄ 综合 ⁄ 共 2460字 ⁄ 字号 评论关闭

The Lamp Game


Time Limit: 2 Seconds
Memory Limit:
65536 KB


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
解题:可以把n个灯看成一个整体状态,因每种状态每次只能翻一个灯,所以有n种转换法,如果我们直接用深搜或广搜那么最坏情况有10^10肯定会超时,那么我们再想因为一个状态经过一次转第i个灯变成为另一个状态,可以看成一条有向边,边权为1-perc[i]/100,也不是转变后的状态剩于score占上一状态的百分比。这样我们把一个状态看成一个点就会联想到最短路算法,想法正确,就是求最大路,那么我们就可以先根据上面所说的去建图,把状态看成一个点,边权就是上述的百分比。这样也就算是完成了这个题。
#include<stdio.h>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define N 1029
typedef struct nn
{
    int to;
    double per;
}TO;
vector<TO>map[N];
double node[N];
void bfs(int st,double m)
{
    queue<int>q;
    int inq[N]={0};
    q.push(st); inq[st]=1; node[st]=m;
    while(!q.empty())
    {
        int p=q.front(); q.pop();
        inq[p]=0;
        for(int j=0;j<map[p].size();j++)
        {
            int i=map[p][j].to;
            if(node[i]<node[p]*map[p][j].per)
            {
                node[i]=node[p]*map[p][j].per;
                if(!inq[i])
                inq[i]=1,q.push(i);
            }
        }
    }
}
double perc[11];
int turn[11];
void buildeMap(int n)
{
    TO p; node[0]=-1;
    for(int i=1;i<(1<<n);i++)
    {
        map[i].clear(); node[i]=-1;
        for(int j=0;j<n;j++)
        {
            p.to=i^turn[j];p.per=(100-perc[j])/100;
            map[i].push_back(p);
        }
    }
}
int main()
{
    int n,t,a;
    double m;
    while(scanf("%d%lf",&n,&m)>0&&(n||m))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&t); turn[i]=1<<i;
            while(t--)
            {
                scanf("%d",&a);a--; turn[i]|=1<<a;
            }
            scanf("%lf",&perc[i]);
        }
        buildeMap(n);
        bfs((1<<n)-1,m);
        if(node[0]>0)printf("%.2lf\n",node[0]);
        else printf("-1\n");
    }
}

抱歉!评论已关闭.