jzoj1350-游戏(某C组)【SPFA,图,最短路】
2018-02-01 20:39
316 查看
前言
每天一道C组题get√题目
有一颗树,只有一个树根,Alice有M1块石头,Bob有M2块石头,Alice先移每个人轮流移动石头,谁先把自己的石头全部移动到树根处就失败了,输出游戏胜者。Input
输入包含多组测试数据。第一行输入T(T<=10)表示测试数据组数。
接下来每组测试数据第一行输入3个整数N(1<=N<=10000),M1(1<=M1<=10000),M2(1<=M2<=10000),其中N表示树的节点数。
接下来N-1行描述树,每行包含两个整数A和B(0<=A,B<=N-1)表示树中有一条边连接A,B两点,注意0是树根。
接下来一行M1个数,表示Alice的M1个石子的位置。
接下来一行M2个数,表示Bob的M2个石子的位置。
Output
对于每组测试数据,输出赢家的名字。Sample Input
23 1 1
0 1
2 0
1
2
3 2 1
0 1
1 2
2 2
2
Sample Output
BobAlice
解题思路
用SPFA求出每个点到根节点的最短路,然后加以判断代码
#include<cstdio> #include<cstring> using namespace std; struct line{ int last,next; }a[20001]; int f[10002],ls[10002],state[10002],t,n,m1,m2,dx,dy,w; int head,tail,s; bool b[10002]; void spfa()//找最短路 { memset(f,127/3,sizeof(f)); head=0;tail=1; state[1]=1;b[1]=true;int q;f[1]=0; do { head=(head+2)%(n+1)-1; q=ls[state[head]]; while (q!=0) { if (f[state[head]]+1<f[a[q].last]) { f[a[q].last]=f[state[head]]+1; if (!b[a[q].last]) { b[a[q].last]=true; tail=(tail+2)%(n+1)-1; state[tail]=a[q].last; } } q=a[q].next; } b[state[head]]=false; } while (head!=tail); } int main() { scanf("%d",&t); for (int ti=1;ti<=t;ti++) { b4e8 memset(ls,0,sizeof(ls)); scanf("%d%d%d",&n,&m1,&m2); w=0; for (int i=1;i<n;i++) { scanf("%d%d",&dy,&dx); a[++w].last=dy+1; a[w].next=ls[dx+1]; ls[dx+1]=w; a[++w].last=dx+1; a[w].next=ls[dy+1]; ls[dy+1]=w;//建图 } spfa();s=0; for (int i=1;i<=m1;i++) { scanf("%d",&dx); s+=f[dx+1];//累计 } for (int i=1;i<=m2;i++) { scanf("%d",&dx); s-=f[dx+1];//累计 } if (s>0) printf("Alice\n"); else printf("Bob\n");//判断输赢 } }
相关文章推荐
- 【CodeVS】1269 匈牙利游戏 最短路 次短路 SPFA
- codevs 1269 匈牙利游戏——次短路(spfa)
- 【中山市选2009】小明的游戏 jzoj 2182/bzoj 2464 最短路
- CODE[VS] 2292 图灵机游戏 SPFA求最短路
- Codevs1269 匈牙利游戏 次短路spfa
- codevs1269匈牙利游戏(spfa跑次短路)
- 【wikioi】1269 匈牙利游戏(次短路+spfa)
- 基础最短路(模板 spfa)
- It's not a Bug, It's a Feature! (poj 1482 最短路SPFA+隐式图+位运算)
- spfa 算法 (单源最短路)
- bzoj 2464 jzoj 2182. 【中山市选2009】小明的游戏
- 最短路知识点总结(Dijkstra,Floyd,SPFA,Bellman-Ford)
- HDU 2544 最短路 (Dijkstra || SPFA)
- 【次短路径/SPFA】BZOJ1726-[Usaco2006 Nov]Roadblocks第二短路
- Remmarguts' Date POJ - 2449 第k短路 SPFA+A*
- 带权最短路 Dijkstra, SPFA, Bellman-Ford, ASP, Floyd-Warshall 算法分析
- 洛谷P1144-最短路计算【日常最短路,日常图论,SPFA】
- [jzoj1163]第k短路
- HDU 5137--How Many Maos Does the Guanxi Worth【枚举 && 最短路 && spfa】
- HDU 1546 Idiomatic Phrases Game 基础最短路spfa