An Unfair Game-[ACdream1035]
2014-05-02 16:23
381 查看
Problem Description
There are n people and n target, everyone should get one target, no two people get the same target, the last one who get the target should be punished, there is only one person will be punished, so if more than one person are the last, no one will be punished. As the judge, you know exactly how much time that people i getting target j will spend, but there are no people know how much time he needs. You need to arrange for a target to everyone, and you want the i-th people to be punished (Now you know why it is an unfair game?).Input
The input contains several test, and ends with EOF. For each test, the first line has an integer n,which is the number of people and target,next there is an integer matrix which size is n*n, the i-th row and the j-th colume of matrix means the time that i-th person needs to get target j. Next line is a number m, the number of the person who you want to be punished.1<=m<n<=100
Output
Each test print one line, the number of the target that m-th person gets, if there are more than one answer, print all of them. If there is no answer, only print -1.Sample Input
31 2 3
2 3 4
3 4 5
3
3
3 3 3
2 2 2
1 1 1
3
Sample Output
2 3-1
Source
UUZManager
nanae题目大概意思是有N个人选择N个目标每人1个.第i个人选择第j个目标花费d[i][j]的时间.用时最长的选手受到惩罚.现在希望通过决定各个选手选择的目标使第M个选手受到惩罚,求此时M选手选择的目标.
一开始我居然觉得是贪心什么什么的...真是手生了.后来发现这是一道二分图匹配的问题.首先尝试i=1~N的点与M结合.与此同时删除M点与i点和其他点之间的连线.再删除花费时间超过d[M][i]的点.对剩下的图求最大匹配.如果结果是N-1则可以使M选手受到惩罚.
#include<stdio.h> #include<string.h> int d[120][120],res[120],N,M,q[120]; bool f[120][120],sta[120]; bool find(int a) { for (int i=1;i<=N;i++) { if (f[a][i] && (!sta[i])) { sta[i]=true; if (res[i]==0 || find(res[i])) { res[i]=a; return true; } } } return false; } int hungary() { memset(res,0,sizeof(res)); int Ans=0; for (int i=1;i<=N;i++) { memset(sta,0,sizeof(sta)); if (find(i)) Ans++; } return Ans; } int main() { while (scanf("%d",&N)!=EOF) { for (int i=1;i<=N;i++) for (int j=1;j<=N;j++) scanf("%d",&d[i][j]); scanf("%d",&M); int T=0; for (int i=1;i<=N;i++) { memset(f,true,sizeof(f)); for (int j=1;j<=N;j++) f[j][i]=false; for (int j=1;j<=N;j++) f[M][j]=false; for (int j=1;j<=N;j++) for (int k=1;k<=N;k++) if (d[j][k]>=d[M][i]) f[j][k]=false; if (hungary()==(N-1)) q[++T]=i; } if (T==0) printf("-1\n"); else { for (int i=1;i<T;i++) printf("%d ",q[i]); printf("%d\n",q[T]); } } return 0; }
View Code
相关文章推荐
- ZOJ 3791 An Easy Game(dp+组合数)
- POJ 1738 An old Stone Game(石子合并)
- An old Stone Game ~~GarsiaWachs算法
- ZOJ_3791 An Easy Game[ 二维dp ]
- POJ 1694 An Old Stone Game
- HDUT-2414-An interesting game(最小费用最大流)
- hdu 5975---Aninteresting game(树状数组)
- 【转】What is an entity system framework for game development?
- poj-3791-An Easy Game-记忆化搜索
- 校赛 An Easy Number Game 回溯
- Game Sound: An Introduction to the History, Theory, and Practice of Video Game Music and Sound Desig
- POJ 1694 An Old Stone Game 笔记
- hdu 3295 An interesting mobile game (IDA*)
- POJ 1738:An old Stone Game 石子归并(GarsiaWachs算法)
- zoj3791(An Easy Game) DP
- [GarsiaWachs算法] BZOJ 3229 [Sdoi2008]石子合并 & POJ 1738 An old Stone Game & 51Nod 1023 石子归并 V3
- zoj 3791 An Easy Game
- ZOJ 3791 An Easy Game
- ZOJ 3791 An Easy Game [组合计数]
- AN Game: view && SurfaceView