1. 程式人生 > >[COCI2006-2007#1] Bond

[COCI2006-2007#1] Bond

nds \n single ted should number ram cati integer

Description
Everyone knows of the secret agent double-oh-seven, the popular Bond (James Bond). A lesser known fact is that he actually did not perform most of his missions by himself; they were instead done by his cousins, Jimmy Bonds. Bond (James Bond) has grown weary of having to distribute assign missions to Jimmy Bonds every time he gets new missions so he has asked you to help him out. Every month Bond (James Bond) receives a list of missions. Using his detailed intelligence from past missions, for every mission and for every Jimmy Bond he calculates the probability of that particular mission being successfully completed by that particular Jimmy Bond. Your program should process that data and find the arrangement that will result in the greatest probability that all missions are completed successfully. Note: the probability of all missions being completed successfully is equal to the product of the probabilities of the single missions being completed successfully.

\(n\)個人去執行\(n\)個任務,每個人執行每個任務有不同的成功率,每個人只能執行一個任務,求所有任務都執行的總的成功率。

輸入第一行,一個整數\(n\)\(1\leq n\leq 20\) ),表示人數兼任務數。接下來\(n\)行每行\(n\)個數,第\(i\)行第\(j\)個數表示第\(i\)個人去執行第\(j\)個任務的成功率(這是一個百分數,在\(0\)\(100\)間)。

輸出最大的總成功率(這應也是一個百分數)

Input
The first line will contain an integer N, the number of Jimmy Bonds and missions (1 ≤ N ≤ 20). The following N lines will contain N integers between 0 and 100, inclusive. The j-th integer on the ith line is the probability that Jimmy Bond i would successfully complete mission j, given as a percentage.

Output
Output the maximum probability of Jimmy Bonds successfully completing all the missions, as a percentage.

Sample Input 1
2
100 100
50 50

Sample Output 1
50.000000

Sample Input 2
2
0 50
50 0

Sample Output 2
25.00000

Sample Input 3
3
25 60 100
13 0 50
12 70 90

Sample Output 3
9.10000

HNIT
Clarification of the third example: If Jimmy bond 1 is assigned the 3rd mission, Jimmy Bond 2 the 1st mission and Jimmy Bond 3 the 2nd mission the probability is: 1.0 0.13 0.7 = 0.091 = 9.1%. All other arrangements give a smaller probability of success. Note: Outputs within ±0.000001 of the official solution will be accepted.

一看就是狀壓……(KM也能寫,不過不想填坑了)

\(f[i][sta]\)表示前\(i\)個人所做任務狀態為\(sta\)的成功率,轉移就隨便枚舉一下即可

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
#define lowbit(x) ((x)&-(x))
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
    static char buf[1000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
    int x=0,f=1; char ch=gc();
    for (;ch<'0'||ch>'9';ch=gc())   if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline int read(){
    int x=0,f=1; char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())  if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())    x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x<0)    putchar('-'),x=-x;
    if (x>9)    print(x/10);
    putchar(x%10+'0');
}
int g[(1<<20)+10],V[25][25];
double f[(1<<20)+10];
int main(){
    int n=read();
    for (int i=1;i<1<<n;i++)    g[i]=g[i-lowbit(i)]+1;
    for (int i=1;i<=n;i++)  for (int j=1;j<=n;j++)  V[i][j]=read();
    f[0]=1.0;
    for (int i=1;i<=n;i++){
        for (int sta=0;sta<1<<n;sta++){
            if (g[sta]!=i)  continue;
            for (int j=1;j<=n;j++)
                if (sta&(1<<(j-1)))
                    f[sta]=max(f[sta],f[sta^(1<<(j-1))]*V[i][j]/100);
        }
    }
    printf("%lf\n",f[(1<<n)-1]*100);
    return 0;
}

[COCI2006-2007#1] Bond