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.ide

1<=m<n<=100    spa

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
code

Sample Output

2 3
-1
blog

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
相關文章
相關標籤/搜索