hdu 3225 Flowers Placement (dfs+匈牙利算法剪枝,太神了)
题意:给出一个n*m的矩阵。每个格子有一个数。每行1..n必须每个出现一次。每列1..n每个数最多出现一次。现在要添加一行,并且补违反上述规则。问添加的方案中字典序第k小的方案。如果一共不足k种方案,那么输出-1.
思路:有点像八皇后。。。就是纯搜。。。不过n好大。。。这么搜会TLE...
想了半天也没思路。。。看了题解。。发现是用二分图匹配来剪枝。。
比较重要的一点是。。。
n个数的某种排列,可以看做是一个位置集合{1..n}和数字集合{1..n}的二分图最大匹配。
我们可以根据这个来剪枝。
具体做法:
**我们先求出一个完备匹配,然后搜索每个位置能够种的花,假设当前位k置种了花i,那么判断k+1--n位置能不能形成一个完备匹配(即能否种出满足条件的花),若能那么当前位置可以种该花,继续搜索,若不能这返回**
然后把一个false写了true.调了一个小时。。。。。。。。。。。。。。无语凝噎。
/* ***********************************************
Author :111qqz
Created Time :2016年05月27日 星期五 00时42分16秒
File Name :code/hdu/3225.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=305;
7bool lie[N][N];
8bool hang[N];
9int n,m,k;
10int cnt;
11//vector <int>ans[N]; //不用每个都存,只存第k个就好。。。前面的随便覆盖。。。
12int cur[N];
13int link[N],tmp[N];
14bool vis[N];
15bool g[N][N];
1bool findgirl( int x,int limit)
2{
3 if (x<=limit) return false;//找到的匹配位置一定要在当前的后面。
4 for ( int i = 1 ; i <= n ; i++)
5 {
6 if (g[x][i]&&!vis[i])
7 {
8 vis[i] = true;
9 if (link[i]==-1||findgirl(link[i],limit))
10 {
11 link[i] = x;
12 return true;
13 }
14 }
15 }
16 return false;
17}
18bool hungary()
19{
20 ms(link,-1);
1 for ( int i = 1 ; i <= n ; i++)
2 {
3 ms(vis,false);
4 if (!findgirl(i,0)) return false; //最大匹配的话。。。必须每个都匹配。。。
5 }
6 return true;
7}
8bool check ( int pos,int id) //pos表示位置,id表示数字,也就是花的种类。
9{
10 if (link[id]==pos) return true;
11 int kind;
12 for ( int i = 1 ; i <= n ; i++)
13 {
14 tmp[i] = link[i] ; //tmp[i]数组为了保护link.
15 if (link[i]==pos) kind = i ;//kind记录了之前做二分图最大匹配的时候当前位置放的花的种类是哪个。
16 }
17 int t = link[id];//t表示id种花在做二分图最大匹配之后的位置。
18 link[id] = pos;
19 link[kind] = -1; //把id种花放在了pos位置,那么原来在pos位置的花就要重新匹配,所以是未匹配。
1 ms(vis,false);
2 if (findgirl(t,pos)) return true;
3 else for ( int i = 1 ; i <= n ; i++) link[i] = tmp[i];
4 return false;
1}
2bool dfs( int j) //j 表示第几列。
3{
4 if (j==n+1)
5 {
6 cnt++;
7 if (cnt==k) return true; //只问了前k个。。所以搜到第k个就好。。。
8 return false;
9 }
1 for ( int i = 1 ; i <= n ; i++)
2 {
3 if (hang[i]) continue ; //表示这一行已经出现了数字i.
4 if (g[j][i]&&check(j,i))
5 {
6 // cout<<"j:"<<j<<" i:"<<i<<endl;
7 hang[i] = true;
8 cur[j] = i ;
9 if (dfs(j+1)) return true;
10 hang[i] = false; //回溯。
11 }
12 }
1 return false;
2}
3int main()
4{
5 #ifndef ONLINE_JUDGE
6 freopen("code/in.txt","r",stdin);
7 #endif
8 int T;
9 scanf("%d",&T);
10 int cas = 0 ;
11 while (T--)
12 {
13 ms(lie,false);
14 ms(hang,false);
15 ms(g,false);
16 ms(cur,0);
17 scanf("%d%d%d",&n,&m,&k);
18// cout<<"n:"<<n<<" m:"<<m<<" k:"<<k<<endl;
19 for ( int i = 1 ; i <= m ; i++)
20 for ( int j = 1 ; j <= n ;j++)
21 {
22 int x;
23 scanf("%d",&x);
24 lie[j][x] = true;
25// cout<<"j:"<<j<<" x:"<<x<<endl;
}
1 for ( int i = 1 ; i <= n ; i++) //n种花为左集合,n个位置为右集合。
2 for ( int j = 1 ; j <= n ; j++)
3 if (!lie[i][j]) g[i][j] = true;
if (!hungary()) //必须是完全匹配。。。。
{
1 printf("Case #%d: -1\n",++cas);
2 continue;
3 }
1 cnt = 0;
2// cout<<"cnt:"<<cnt<<endl;
3 if (!dfs(1))
4 {
5 printf("Case #%d: -1\n",++cas);
6 }
7 else
8 {
9 printf("Case #%d: ",++cas);
1 for ( int i = 1 ; i <= n-1 ; i++)
2 printf("%d ",cur[i]);
3 printf("%d\n",cur[n]);
4 }
5 }
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}