您的位置:首页 > 其它

hdu1281(Hungary模板 + 枚举)简单

2015-09-09 22:01 411 查看
思路:题目说得很明显了,一行或一列中最多放一个车,,,给了一些可以放车的点,必然有个可以放最多个车的方案,设最多放ans个车;这样的话,有些点不放车,换成另外一个地方放车也是可以最多放ans个车的,但是有些点是必须放菜能使最后可以放ans个车,问有多少个第二种情况的点;既然问点有多少个,,,我就枚举每个点不放车时,这种时候可以放的车的最大数目max_num,如果不等于ans,必然这个点满足要求;这样一来,整个题的思路就清楚了;

所以最后用一个Hungary() + 枚举删点就好了;

PS:Hungary()的思想就是寻找增广路径(这个是图论中常用的概念,请百度资料然后熟记),可以在当前匹配M的情况下,还能利用原关系找到两个未匹配点的匹配,就说明找到了一个,则此时|M| + 1就变成新的匹配了;

知道找不到这种路径,算法就结束了;

题目链接



/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
#define pk push_back
template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}
typedef long long ll;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 105;
int gg[maxn][maxn];
int link[maxn];//(x,y)的匹配关系;
bool vis[maxn];//每次增广保证不会重复访问;
int chess[maxn * maxn][2];//棋子的位置;
inline void Init(int K){
for (int i = 1;i <= K;++i)
gg[chess[i][0]][chess[i][1]] = 1;
}

bool Search_Path(int M,int u){
for (int i = 1;i <= M;++i){
if (!vis[i] && gg[u][i]){
vis[i] = 1;
if (link[i] == 0 || Search_Path(M,link[i])){
link[i] = u;
return true;
}
}
}
return false;
}

inline int Hungary(int N,int M){
int ret = 0;
for (int i = 1;i <= N;++i){
for (int j = 1;j <= M;++j)
vis[j] = 0;
if (Search_Path(M,i))
ret++;
}
return ret;
}

int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int N,M,K;
int iCase = 0;
while(~scanf("%d%d%d",&N,&M,&K)){
int rec = 0;
for (int i = 1;i <= K;++i){
scanf("%d%d",&chess[i][0],&chess[i][1]);
}
MEM(gg, 0);
MEM(link, 0);
Init(K);
int ans = Hungary(N,M);
for (int i = 1;i <= K;++i){
for (int j = 0;j <= M;j++)
link[j] = 0;
gg[chess[i][0]][chess[i][1]] ^= 1;
if (Hungary(N,M) != ans)
rec++;
gg[chess[i][0]][chess[i][1]] ^= 1;
}
// printf("ans = %d\n",ans);
// printf("rec = %d\n",rec);
printf("Board %d have %d important blanks for %d chessmen.\n",++iCase, rec, ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: