POJ 1584 Robots 最少边覆盖 二分图最大匹配
来源:互联网 发布:web项目怎么管理网络 编辑:程序博客网 时间:2024/05/19 14:52
如果将机器人走过的路线视为一条边,那么这道题和POJ 2594如出一辙,任然是求最少路径覆盖问题,并且点仍然是可以重复走的,因此可以使用二分图最大匹配来做,下面说说思路。首先保存所有节点信息,然后由于机器人只能向右走或者向下走,因此我们对任意两个点a、b进行比较,如果由a可以走到b,那么就建一条<a,b>有向边,然后对这个新建的图进行hungary即可,ans=n-hungary()。RobotsTime Limit: 1000MSMemory Limit: 10000KTotal Submissions: 4165Accepted: 1913Description
Your company provides robots that can be used to pick up litter from fields after sporting events and concerts. Before robots are assigned to a job, an aerial photograph of the field is marked with a grid. Each location in the grid that contains garbage is marked. All robots begin in the Northwest corner and end their movement in the Southeast corner. A robot can only move in two directions, either to the East or South. Upon entering a cell that contains garbage, the robot will pick it up before proceeding. Once a robot reaches its destination at the Southeast corner it cannot be repositioned or reused. Since your expenses are directly proportional to the number of robots used for a particular job, you are interested in finding the minimum number of robots that can clean a given field. For example, consider the field map shown in Figure 1 with rows and columns numbered as shown and garbage locations marked with a 'G'. In this scheme, all robots will begin in location 1,1 and end in location 6, 7.Figure 1 - A Field Map Figure 2 below shows two possible solutions, the second of which is preferable since it uses two robots rather than three.Figure 2 - Two Possible Solutions Your task is to create a program that will determine the minimum number of robots needed to pick up all the garbage from a field.Input
The input consists of one or more field maps followed by a line containing -1 -1 to signal the end of the input data. A field map consists of one or more lines, each containing one garbage location, followed by a line containing 0 0 to signal the end of the map. Each garbage location consists of two integers, the row and column, separated by a single space. The rows and columns are numbered as shown in Figure 1. The garbage locations will be given in row-major order. No single field map will have more than 24 rows and 24 columns. The sample input below shows an input file with two field maps. The first is the field map from Figure 1.Output
The output will consist of a single line for each field map containing the minimum number of robots needed to clean the corresponding field.Sample Input
1 21 42 42 64 44 76 60 01 12 24 40 0-1 -1Sample Output
21Source
Mid-Central USA 2003#include <iostream>#include <stdio.h>#include <string.h>using namespace std;struct P{ int r,c;};const int MAXN=1010;int uN,vN;int g[MAXN][MAXN];int linker[MAXN];bool used[MAXN];P gp[MAXN*MAXN];int gn;bool dfs(int u){ for(int v=0;v<vN;v++) { if(g[u][v]&&!used[v]) { used[v]=true; if(linker[v]==-1||dfs(linker[v])) { linker[v]=u; return true; } } } return false;}int hungary(){ int res=0; memset(linker,-1,sizeof(linker)); for(int u=0;u<uN;u++) { memset(used,false,sizeof(used)); if(dfs(u)) res++; } return res;}void init(){ uN=vN=0; memset(g,0,sizeof(g)); gn=0;}void addpoint(int r,int c){ gp[gn].r=r; gp[gn++].c=c;}bool cmp(P a,P b){ if((b.c-a.c)>=0&&(b.r-a.r>=0)) return true; else return false;}int main(){ int r,c; int ans; while(~scanf("%d%d",&r,&c)) { if(r==-1&&c==-1) break; if(r==0&&c==0) { printf("0\n"); continue; } init(); addpoint(r-1,c-1); while(~scanf("%d%d",&r,&c)) { if(r==0&&c==0) break; addpoint(r-1,c-1); } uN=vN=gn; for(int i=0;i<gn;i++) { for(int j=i+1;j<gn;j++) { if(cmp(gp[i],gp[j])) g[i][j]=1; if(cmp(gp[j],gp[i])) g[j][i]=1; } } ans=gn-hungary(); printf("%d\n",ans); } return 0;}
查看原文:http://colorfulshark.cn/wordpress/robots-999.html
0 0
- POJ 1584 Robots 最少边覆盖 二分图最大匹配
- poj 3020 二分图 最大匹配(最小边覆盖问题)
- HDU1150 二分匹配 最少点覆盖=最大匹配数
- Poj 1548 Robots【最小路径覆盖---二分匹配】
- [二分图最大匹配=最小覆盖点]uva12549 Sentry Robots
- 最小路径覆盖-二分图最大匹配 poj 1422
- POJ 3692 最小点覆盖 最大二分图匹配
- poj 3041 二分图最大匹配(最小点覆盖问题)
- poj 3041 二分图最大匹配 最小点覆盖
- poj 2594 二分图最大匹配最小路径覆盖
- POJ 3401 Asteroids 二分图最大匹配 最小点覆盖
- POJ 3041 Asteroids(二分图最大匹配-最小点覆盖)
- hdu 1054 Strategic Game 最少点覆盖 二分图最大匹配
- POJ - 1548 Robots 二分图 最小路径覆盖
- ZOJ 1654 Place the Robots(二分图最大匹配)
- ZOJ--1654--Place the Robots【二分图最大匹配】
- 【二分图|最大匹配】ZOJ-1654 Place the Robots
- Zju1654 Place the Robots(二分图最大匹配)
- 亿级Web系统的高容错性实践
- 成为C++高手之if与switch
- Android学习(50) -- 使用post方式提交数据
- 针对软件研发流程,我总结出的流水线生产方法论
- Field 'id' doesn't have a default value 对于SSH 架构,解决起来就简单多了
- POJ 1584 Robots 最少边覆盖 二分图最大匹配
- 【再见CSDN】
- 【华为OJ】【041-求int型数据在内存中存储时1的个数】
- [CSAPP笔记][第八章异常控制流][呕心沥血千行笔记]
- 【华为OJ】【042-矩阵乘法】
- C#动态编译、执行代码
- Unity3d Realtime Dynamic Clouds Rendering
- 【华为OJ】【044-超长正整数相加】
- 第93讲:Spark Streaming updateStateByKey案例实战和内幕源码