E. Another Sith Tournament

题目连接:

http://www.codeforces.com/contest/678/problem/E

Description

The rules of Sith Tournament are well known to everyone. n Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each battle in the Sith Tournament ends with a death of one of opponents? The Tournament ends when the only Sith remains alive.

Jedi Ivan accidentally appeared in the list of the participants in the Sith Tournament. However, his skills in the Light Side of the Force are so strong so he can influence the choice of participants either who start the Tournament or who take the loser's place after each battle. Of course, he won't miss his chance to take advantage of it. Help him to calculate the probability of his victory.

Input

The first line contains a single integer n (1 ≤ n ≤ 18) — the number of participants of the Sith Tournament.

Each of the next n lines contains n real numbers, which form a matrix pij (0 ≤ pij ≤ 1). Each its element pij is the probability that the i-th participant defeats the j-th in a duel.

The elements on the main diagonal pii are equal to zero. For all different i, j the equality pij + pji = 1 holds. All probabilities are given with no more than six decimal places.

Jedi Ivan is the number 1 in the list of the participants.

Output

Output a real number — the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10 - 6.

Sample Input

3

0.0 0.5 0.8

0.5 0.0 0.4

0.2 0.6 0.0

Sample Output

0.680000000000000

Hint

题意

有n个人在决斗,两个决斗,然后胜利者继续决斗

你是0号人物,你可以安排比赛顺序,问你最大的获胜概率是多少

题解:

状压dp

你是最后一个上场的人,这个结论猜一下就好了。

然后倒着做。

dp[i][j]表示你还要干死状态i的人,当前正在打的人是j,然后你获胜的最大概率是多少

然后直接状压dp莽一波就好了。

注意,这个状态是倒着的。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 18;
double p[maxn][maxn],dp[1<<maxn][maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>p[i][j];
dp[1][0]=1;
for(int i=0;i<(1<<n);i++)
{
for(int j=0;j<n;j++)if(i&(1<<j))
{
for(int k=0;k<n;k++)if(i&(1<<k)&&(k!=j))
dp[i][j]=max(dp[i][j],p[j][k]*dp[i^(1<<k)][j]+p[k][j]*dp[i^(1<<j)][k]);
}
}
double ans = 0;
for(int i=0;i<n;i++)
ans=max(ans,dp[(1<<n)-1][i]);
printf("%.12f\n",ans);
}

Educational Codeforces Round 13 E. Another Sith Tournament 状压dp的更多相关文章

  1. Educational Codeforces Round 13 E. Another Sith Tournament 概率dp+状压

    题目链接: 题目 E. Another Sith Tournament time limit per test2.5 seconds memory limit per test256 megabyte ...

  2. CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP

    题目传送门 https://codeforces.com/contest/1103/problem/D 题解 失去信仰的低水平选手的看题解的心路历程. 一开始看题目以为是选出一些数,每个数可以除掉一个 ...

  3. Codeforces 678E Another Sith Tournament 状压DP

    题意: 有\(n(n \leq 18)\)个人打擂台赛,编号从\(1\)到\(n\),主角是\(1\)号. 一开始主角先选一个擂主,和一个打擂的人. 两个人之中胜的人留下来当擂主等主角决定下一个人打擂 ...

  4. Codeforces Round #585 (Div. 2) E. Marbles(状压dp)

    题意:给你一个长度为n的序列 问你需要多少次两两交换 可以让相同的数字在一个区间段 思路:我们可以预处理一个数组cnt[i][j]表示把i放到j前面需要交换多少次 然后二进制枚举后 每次选择一个为1的 ...

  5. Codeforces Beta Round #8 C. Looking for Order 状压dp

    题目链接: http://codeforces.com/problemset/problem/8/C C. Looking for Order time limit per test:4 second ...

  6. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

  7. [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)

    [多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...

  8. Codeforces 1225G - To Make 1(bitset+状压 dp+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 还是做题做太少了啊--碰到这种题一点感觉都没有-- 首先我们来证明一件事情,那就是存在一种合并方式 \(\Leftrightarrow\) ...

  9. Educational Codeforces Round 13

    http://codeforces.com/contest/678 A:水题 #include<bits/stdc++.h> #define fi first #define se sec ...

随机推荐

  1. Linux内存初始化【转】

    转自:http://www.cnblogs.com/super-king/p/3291120.html start_kernel -> setup_arch 在这个函数中我们主要看这几个函数. ...

  2. JSONArray().fromObject(); 出现org.apache.catalina.core.StandardWrapperValve invoke错误的解决办法

    servlet: public void service(HttpServletRequest request, HttpServletResponse response) throws Servle ...

  3. JAVA复习笔记:内存结构和类加载

    Part1:JVM内存结构 JVM定义了若干个程序执行期间使用的数据区域.这个区域里的一些数据在JVM启动的时候创建,在JVM退出的时候销毁.而其他的数据依赖于每一个线程,在线程创建时创建,在线程退出 ...

  4. java根据日期获取周几和获取某段时间内周几的日期

    整理两个日期的方法. 根据日期获取当天是周几 /** * 根据日期获取当天是周几 * @param datetime 日期 * @return 周几 */ public static String d ...

  5. 拉格朗日(Lagrange)插值算法

    拉格朗日插值(Lagrange interpolation)是一种多项式插值方法,指插值条件中不出现被插函数导数值,过n+1个样点,满足如下图的插值条件的多项式.也叫做拉格朗日公式.  这里以拉格朗日 ...

  6. VS 2015 序列号/密钥/企业版/专业版

    专业版:HMGNV-WCYXV-X7G9W-YCX63-B98R2企业版:HM6NR-QXX7C-DFW2Y-8B82K-WTYJV

  7. abp zero 4.3 发布

    Demo URL: http://abpzerodemo.demo.aspnetzero.com Username: systemPassword: 123456 需要源码,请加QQ:3833-255 ...

  8. 组件库按需加载 借助babel-plugin-import实现

    前段时间一直在基于webpack进行前端资源包的瘦身.在项目中基于路由进行代码分离,http://www.cnblogs.com/legu/p/7251562.html.对于公司内部的组件库,所有内容 ...

  9. Java与Redis

    1.下载Java使用Redis架包并引入 jedis-2.9.0.jar 2.用Java使用Redis如下: package com.jef.redis; import redis.clients.j ...

  10. MemSQL Start[c]UP 2.0 - Round 1 F - Permutation 思维+线段树维护hash值

    F - Permutation 思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个, 所以可以转换成,x+k, x-k在到x所在位置的时候是否 ...