Codeforces Gym 100548H The Problem to Make You Happy (图上博弈DP)
来源:互联网 发布:sql select where or 编辑:程序博客网 时间:2024/05/22 04:11
题目链接
题目大意:
在一张有向图上Bob和Alice在两个点向前走,Bob先走,Alice再走,如果他们相遇那么Alice获胜,如果有人无路可走那么另一个获胜,如果游戏不会结束那么Bob获胜。每个人都选择最优策略。求谁会获胜。
解题思路:
明显是图上博弈dp,比赛时写的代码用记忆化搜索判断游戏不会结束的方法估计时错误的,就一直WA。
其实我们不需要判断出每一种情况,这里不好判断游戏结束,那我们就不判断它,只要找到所有Bob必败的情况,剩下的就是Bob必胜的情况了。
最终比赛结束的情况很好找到,决策也需要后面的结果,我们就从结束逆推回初始状态,建立反向图,表示状态用dp[i][j][k],i:Bob位置,j:Alice位置,k:该谁走。初始化就是对于所有相遇的情况Alice胜利,所有Bob无路可走的状态Alice胜利。然后用bfs倒推。状态转移是,对于Alice走,如果有可以转移到Bob必败的状态,那么这个状态Bob必败,对于Bob走,只有全部可以到达的状态都是必败那么这个状态必败。
AC代码:
#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <algorithm>#include <queue>#include <map>using namespace std;const int MAXN=100+3;struct Node{ int x,y,z;//Bob位置,Alice位置,该谁走 Node(int x,int y,int z):x(x),y(y),z(z){};};vector<int> G[MAXN];//反向图int N,M,X,Y;bool dp[MAXN][MAXN][2];//Bob位置,Alice位置,该谁走(0:Alice,1:Bob),Bob是否会输int cnt[MAXN][MAXN];//这个状态往下走输的情况数int out[MAXN];//出度int main(){ int T; scanf("%d",&T); for(int cas=1;cas<=T;++cas) { scanf("%d%d",&N,&M); for(int i=1;i<=N;++i)//初始化 { G[i].clear(); out[i]=0; for(int j=1;j<=N;++j) { dp[i][j][0]=dp[i][j][1]=false; cnt[i][j]=0; } } for(int i=0;i<M;++i) { int a,b; scanf("%d%d",&a,&b); G[b].push_back(a); ++out[a]; } scanf("%d%d",&X,&Y); queue<Node> que; for(int i=1;i<=N;++i)//相遇 { dp[i][i][0]=dp[i][i][1]=true; que.push(Node(i,i,0)); que.push(Node(i,i,1)); } for(int i=1;i<=N;++i)//无路可走 if(out[i]==0) for(int j=1;j<=N;++j) if(!dp[i][j][0]) { dp[i][j][0]=true; que.push(Node(i,j,0)); } while(!que.empty()) { int x=que.front().x,y=que.front().y,z=que.front().z; que.pop(); if(z)//该Bob走 { for(int i=0;i<G[x].size();++i) if(++cnt[G[x][i]][y]==out[G[x][i]]&&!dp[G[x][i]][y][0])//全部路径都是必败,这种情况就是必败 { dp[G[x][i]][y][0]=true; que.push(Node(G[x][i],y,0)); } } else//该Alice走 { for(int i=0;i<G[y].size();++i)//能够然Bob必败,那么这个情况就是必败 if(!dp[x][G[y][i]][1]) { dp[x][G[y][i]][1]=true; que.push(Node(x,G[y][i],1)); } } } printf("Case #%d: ",cas); puts(dp[X][Y][0]?"No":"Yes"); } return 0;}/* 3 5 3 1 2 3 4 4 5 3 1 4 3 1 2 2 3 3 4 1 2 3 3 1 2 2 3 3 1 2 1 */
0 0
- Codeforces Gym 100548H The Problem to Make You Happy (图上博弈DP)
- CF GYM 100548 The Problem to Make You Happy(2014ACM西安现场赛Problem H)
- UVA7042(2014西安H题)-The Problem to Make You Happy-博弈+bfs
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest(H - The Problem to Make You Happy )
- 【博弈论+记忆化dp+visit数组+回溯】acm 2014 西安赛区 H problem make you happy
- Codeforces 689 C The Values You Can Make(dp)
- codeforces Gym 100500Problem H. ICPC Quest 简单DP
- Codeforces Gym 100548G The Problem to Slow Down You (Palindromic Tree 或 Hash水过) 2014西安现场赛G题
- Codeforces Gym 100623H Problem H. Holes
- Codeforces 688E The Values You Can Make【Dp】
- Codeforces 687C The Values You Can Make DP
- Codeforces 688E The Values You Can Make(DP)
- Codeforces Gym 100379J Move the (p, q)-knight to the corner!(DP+lucas定理)
- Codeforces Round #360 (Div. 2) E The Values You Can Make(DP)
- Codeforces Round #360 (Div. 2) -- E. The Values You Can Make (DP)
- Codeforces Round #360 (Div. 1) C. The Values You Can Make(DP)
- CodeForces 678C The Values You Can Make (3维DP)
- 7_6_M题 The Values You Can Make题解[Codeforces 687C](DP)
- Prototype_Pattern
- phpredis实战--简单实现注册,删除,编辑,分页,登录,关注等功能
- JAVA温习课堂12
- 数据结构--求解最大公因子三个方法
- javascript中面向对象的程序设计——对象的创建
- Codeforces Gym 100548H The Problem to Make You Happy (图上博弈DP)
- Java数组
- Kindle DXG和Win10 64bits无法连接的问题
- The New Year: Meeting Friends Codeforces Round#375-A
- mvc框架留言框架实战之前端功能整理
- 反射程序集
- 【C/C++】对于可重入、线程安全、异步信号安全几个概念的理解
- MySQL 如何删除有外键约束的表数据
- Prototype_Level3