Find the Marble


Time Limit: 2 Seconds     
Memory Limit: 65536 KB


Alice and Bob are playing a game. This game is played with several identical pots and one marble. When the game starts, Alice puts the pots in one line and puts the marble in one of the pots. After that, Bob cannot see the inside of the pots. Then Alice
makes a sequence of swappings and Bob guesses which pot the marble is in. In each of the swapping, Alice chooses two different pots and swaps their positions.

Unfortunately, Alice's actions are very fast, so Bob can only catch k ofm swappings and regard these
k swappings as all actions Alice has performed. Now given the initial pot the marble is in, and the sequence of swappings, you are asked to calculate which pot Bob most possibly guesses. You can assume that Bob missed any of the swappings with equal
possibility.

Input

There are several test cases in the input file. The first line of the input file contains an integerN (N ≈ 100), then
N cases follow.

The first line of each test case contains 4 integers n, m,
k
and s(0 < sn ≤ 50, 0 ≤ km ≤ 50), which are the number of pots, the number of swappings Alice makes, the number of swappings Bob catches and index of the initial pot the marble is in. Pots are indexed
from 1 to n. Then m lines follow, each of which contains two integersai and
bi (1 ≤ ai, bi
n
), telling the two pots Alice swaps in the i-th swapping.

Outout

For each test case, output the pot that Bob most possibly guesses. If there is a tie, output the smallest one.

Sample Input

3
3 1 1 1
1 2
3 1 0 1
1 2
3 3 2 2
2 3
3 2
1 2

Sample Output

2
1
3

题目大意:N个容器,M次两两交换。当中K次是能够知道的,一開始珠子放在当中一个容器里。问你交换完以后,珠子在哪个容器的概率最大

思路:用DP[m][k][n] 表示 m次交换,知道了当中的k次,结尾为n的方案数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector> using namespace std;
typedef long long ll;
const int maxn = 60;
int n,m,k,s;
int A[maxn],B[maxn];
ll dp[maxn][maxn][maxn];
int main(){ int ncase;
cin >> ncase;
while(ncase--){
scanf("%d%d%d%d",&n,&m,&k,&s);
for(int i = 1; i <= m; i++){
scanf("%d%d",&A[i],&B[i]);
}
memset(dp,0,sizeof dp);
dp[0][0][s] = 1;
for(int i = 1; i <= m; i++){
dp[i][0][s] = 1;
for(int j = 1; j <= i&&j <= k; j++){
dp[i][j][A[i]] = dp[i-1][j-1][B[i]];
dp[i][j][B[i]] = dp[i-1][j-1][A[i]];
for(int d = 1; d <= n; d++){
dp[i][j][d] += dp[i-1][j][d];
if(d!=A[i]&&d!=B[i]){
dp[i][j][d] += dp[i-1][j-1][d];
}
}
}
}
int idx = 1;
for(int i = 2; i <= n; i++){
if(dp[m][k][i] > dp[m][k][idx]){
idx = i;
}
}
cout<<idx<<endl;
}
return 0;
}


版权声明:本文博客原创文章,博客,未经同意,不得转载。

ZOJ3605-Find the Marble(可能性DP)的更多相关文章

  1. zoj3605 Find the Marble --- 概率dp

    n个杯子.球最開始在s位置.有m次换球操作,看到了k次,看的人依据自己看到的k次猜球终于在哪个位置,输出可能性最大的位置. dp[m][k][s]表示前m次操作中看到了k次球终于在s的频率. #inc ...

  2. [AC自己主动机+可能性dp] hdu 3689 Infinite monkey theorem

    意甲冠军: 给n快报,和m频率. 然后进入n字母出现的概率 然后给目标字符串str 然后问m概率倍的目标字符串是敲数量. 思维: AC自己主动机+可能性dp简单的问题. 首先建立trie图,然后就是状 ...

  3. ZOJ 3605 Find the Marble(dp)

    Find the Marble Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice and Bob are playing a game. ...

  4. 可能性dp+减少国家HDU4336

    Card Collector Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  5. zoj 3822 Domination (可能性DP)

    Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headm ...

  6. CF 148D. Bag of mice (可能性DP)

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  7. poj2096--Collecting Bugs(可能性dp第二弹,需求预期)

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 2678   Accepted: 1302 ...

  8. UVA 10529 Dumb Bones 可能性dp 需求预期

    主题链接:点击打开链接 题意: 要在一条直线上摆多米诺骨牌. 输入n, l, r 要摆n张排,每次摆下去向左倒的概率是l, 向右倒的概率是r 能够採取最优策略.即能够中间放一段.然后左右两边放一段等, ...

  9. hdu 4870 Rating(可能性DP&amp;高数消除)

    Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

随机推荐

  1. GNU名称解析

    GNU它是GNU's NOT UNIX缩写G     N    U缩写,和GNU全名GNU's NOT UNIX 中间 GNU 也GNU's NOT UNIX缩写,它使用递归方式定义GNU.

  2. 【Android进阶】让程序运行效率更高的编程技巧总结

    1.在程序中若出现字符串连接的情况,请使用StringBuffer代替String,这样可以减少多次创建String以及垃圾回收所带来的内存消耗 2.尽量使用局部变量.调用方法时传递的参数以及调用中创 ...

  3. 编译Android源代码与内核总结

    这些天花了些时间自己下载了android源代码来编译,当中走了一些弯路导致耗了些时间,如今又一次梳理总结下,让有同样想法的人自己编译的时候能少走些弯路,官方指导文档在http://source.and ...

  4. windows下使用lighttpd+php(fastcgi)+mysql

    一.windows下编译配置执行lighttpd 1.下载并安装cygwin. 2.下载lighttpd源码并解压3.在cygwin环境下进入lighttpd的解压文件夹后,执行: 1> ./c ...

  5. 8皇后-----回溯法C++编程练习

    /* * 八皇后问题回溯法编程练习 * 在8×8的棋盘上,放置8个皇后,两个皇后之间不能两两攻击 * 也即,直线,垂直45度.135度方向不能出现两个皇后 * * copyright Michael ...

  6. effective c++ 条款11 Handle assignment to self in operator=

    赋值给自己,听起来有些不可思议,但是却要引起重视,它很容易把自己隐藏起来. 例如 1 a[i]=a[j]; 如果 i, j的值一样? 2 *px=*py; 如果px py指向同一个object 3   ...

  7. 玩转Web之Json(三)-----easy ui怎么把前台显示的dataGird中的所有数据序列化为json,返回到后台并解析

    最近做一个项目时,需要在dataGird中插入<input>,即文本输入框,当点击提交时,需要把文本框里填的数据返以及其他列的一些信息以json数组的格式返回到后台,虽然我实现了该功能,但 ...

  8. Swift String length property

    Swift的String居然没有length属性,好难受,每次要获取String的字符串长度都要借助全局函数countElements. 没办法.仅仅有扩展String结构体,给它加入一个属性了. i ...

  9. 3、Spring4之Bean 配置的细节

    1). 若字面值中包括特殊字符,则能够使用 value 节点的 <![CDATA[]]> 把字面值包裹起来.      <constructor-arg>           ...

  10. java反射机制性能优化

    import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.uti ...