zoj 1654 Place the Robots 二分图匹配
2014-08-17 18:59
274 查看
/* 题目:poj1654 时间:2014-08-17 标签:二分图匹配 题意:给一个map '#'代表Wall 子弹不穿 '*'代表Grass 子弹穿 'o'代表Empty 子弹穿,放机器人 求最多能放几个机器人。 http://blog.csdn.net/acdreamers/article/details/8654005 */ #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define mem(a,b) memset(a,b,sizeof a) const int N=52*52/2; //二分图模板 struct Edge{//邻接图 int v,next; }edge[N*N]; int cnt,head ; void addEdge(int u,int v){ edge[cnt].v = v; edge[cnt].next = head[u]; head[u] = cnt++; } bool visit ; int link ; bool dfs(int u){//寻找增广路 for(int i = head[u]; i!=-1; i = edge[i].next){ int v = edge[i].v; if(visit[v] == false){ visit[v] = true; if(link[v] == -1 || dfs(link[v])){//找到未匹配的点 link[v] = u; return true; } } } return false; } int match(int R){//二分图匹配(左端点个数) int ans=0; mem(link,-1); for(int i=1;i<=R;i++){ mem(visit,false); if(dfs(i)) ans++; } return ans; } char mp ; int row ,col ; int R,C; void build(int n,int m){//map转换成二分图 mem(row,false); mem(col,false); R=C=1; for(int i=1;i<=n;i++){//二分图构点 for(int j=1;j<=m;j++){ if(mp[i][j]!='#'&&row[i][j]==0){//横向一条 for(int k=j;mp[i][k]!='#'&&k<=m;k++) row[i][k]=R; R++; } if(mp[i][j]!='#'&&col[i][j]==0){//竖向一条 for(int k=i;mp[k][j]!='#'&&k<=n;k++) col[k][j]=C; C++; } } } mem(head,-1); cnt=0; for(int i=1;i<=n;i++){//二分图构边 for(int j=1;j<=m;j++) if(mp[i][j]=='o'&&row[i][j]!=0&&col[i][j]!=0){ addEdge(row[i][j],col[i][j]); //printf("%d %d is edge\n",row[i][j],col[i][j]); } } } int main(){ int T,cas=1,n,m; cin>>T; while(T--){ cin>>n>>m; for(int i=1;i<=n;i++) scanf("%s",mp[i]+1); printf("Case :%d\n",cas++); build(n,m);//构造二分图的点和边 printf("%d\n",match(R));//二分图匹配 } return 0; }
相关文章推荐
- 【图论】【二分图匹配】[ZOJ 1654]Place the Robots
- zoj 1654 Place the Robots(二分图匹配 匈牙利算法)
- zoj 1654 Place the Robots 二分图匹配DFS实现
- ZOJ1654 Place the Robots (二分图匹配)
- zoj 1654 Place the Robots 二分图匹配DFS实现
- ZOJ1654-Place the Robots(二分图匹配)
- ZOJ 1654 Place the Robots【二分图匹配】
- ZOJ1654 place the robots(二分图匹配)
- zoj 1654 Place the Robots (二分图匹配)
- ZOJ 1654 匈牙利算法 二分图匹配 Place the Robots
- ZOJ1654 Place the Robots
- ZOJ 1654 Place the Robots
- ZOJ 1654 Place the Robots(最大二分匹配)
- ZOJ 1654 Place the Robots(超牛的建图思路) - from lanshui_Yang
- [ZOJ1654]Place the Robots 做题笔记
- ZOJ 1654 Place the Robots DINIC
- ZOJ 1654 Place the Robots(最大匹配)
- zoj 1654 Place the Robots
- ZOJ1654 Place the Robots(二分图最大匹配)
- ZOJ1654:Place the Robots