An Unfair Game-[ACdream1035]

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

3
1 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

UUZ

Manager

题目大概意思是有N个人选择N个目标每人1个.第i个人选择第j个目标花费d[i][j]的时间.用时最长的选手受到惩罚.现在希望通过决定各个选手选择的目标使第M个选手受到惩罚,求此时M选手选择的目标.
一开始我居然觉得是贪心什么什么的...真是手生了.后来发现这是一道二分图匹配的问题.首先尝试i=1~N的点与M结合.与此同时删除M点与i点和其他点之间的连线.再删除花费时间超过d[M][i]的点.对剩下的图求最大匹配.如果结果是N-1则可以使M选手受到惩罚.
 1 #include<stdio.h>

 2 #include<string.h>

 3 int d[120][120],res[120],N,M,q[120];

 4 bool f[120][120],sta[120];

 5 bool find(int a)

 6 {

 7     for (int i=1;i<=N;i++)

 8     {

 9         if (f[a][i] && (!sta[i]))

10         {

11             sta[i]=true;

12             if (res[i]==0 || find(res[i]))

13             {

14                 res[i]=a;

15                 return true;

16             }

17         }

18     }

19     return false;

20 }

21 int hungary()

22 {

23     memset(res,0,sizeof(res));

24     int Ans=0;

25     for (int i=1;i<=N;i++)

26     {

27         memset(sta,0,sizeof(sta));

28         if (find(i)) Ans++;

29     }

30     return Ans;

31 }

32 int main()

33 {

34     while (scanf("%d",&N)!=EOF)

35     {

36         for (int i=1;i<=N;i++)

37             for (int j=1;j<=N;j++)

38                 scanf("%d",&d[i][j]);

39         scanf("%d",&M);

40         int T=0;

41         for (int i=1;i<=N;i++)

42         {

43             memset(f,true,sizeof(f));

44             for (int j=1;j<=N;j++) f[j][i]=false;

45             for (int j=1;j<=N;j++) f[M][j]=false;

46             for (int j=1;j<=N;j++)

47                 for (int k=1;k<=N;k++)

48                     if (d[j][k]>=d[M][i]) f[j][k]=false;

49             if (hungary()==(N-1)) q[++T]=i;

50         }

51         if (T==0) printf("-1\n");

52         else

53         {

54             for (int i=1;i<T;i++) printf("%d ",q[i]);

55             printf("%d\n",q[T]);

56         }

57     }

58     return 0;

59 }
View Code

 

你可能感兴趣的:(game)