Robots
Time Limit: 1000MS |
| Memory Limit: 10000K |
Total Submissions: 4212 |
| Accepted: 1941 |
Description
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 2
1 4
2 4
2 6
4 4
4 7
6 6
0 0
1 1
2 2
4 4
0 0
-1 -1
Sample Output
2
1
Source
题目大意:给你这些个点在一个24*24的图上,起点在左上角,终点在右下角,不能有相交线段的情况下,从起点出发,问最少需要多少次画线。
思路:
1、典型的最小路径覆盖问题。
2、建图:将(1,1)编号为1,一个点能够匹配的点就是其右下区域。e.g:阴影区域中如果有G,那么节点u就能够匹配这个点,使其连成一条线。
3、建图完之后,跑一次匈牙利最大二分匹配算法,ans==点的个数-最大二分匹配数。
最小路径覆盖数=n-最大二分匹配数,我们这里可以进行简单的证明:
有向图的最小路径覆盖数:
对于二分匹配得到的匹配数m,一共能够覆盖掉2m个点,我们设除了这2m个点之外,整个图中还剩下a个点没有被覆盖,这个时候我们需要a条边来覆盖这些点。
不难看出:n=2m+a,而且最小路径覆盖数==m+a,那么:m+a=n-m。
Ac代码:
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
struct node
{
int x,y;
}a[200000];
int match[200000];
int vis[200000];
vector<int >mp[50*50*10];
int cmp(node a,node b)
{
if(a.y==b.y)return a.x<b.x;
else return a.y<b.y;
}
int find(int u)
{
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(vis[v]==0)
{
vis[v]=1;
if(match[v]==-1||find(match[v]))
{
match[v]=u;
return 1;
}
}
}
return 0;
}
int main()
{
while(1)
{
int x,y;
int flag=0;
int n=0;
memset(match,-1,sizeof(match));
for(int i=1;i<=50*50;i++)mp[i].clear();
while(~scanf("%d%d",&x,&y))
{
if(x==-1&&y==-1)
{
flag=1;break;
}
if(x==0&&y==0)break;
a[n].x=x;
a[n].y=y;
n++;
}
if(flag==1)return 0;
sort(a,a+n,cmp);
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int u=(a[i].x-1)*24+a[i].y;
int v=(a[j].x-1)*24+a[j].y;
if(u<v)
{
mp[u].push_back(v);
}
}
}
int output=0;
for(int i=0;i<n;i++)
{
memset(vis,0,sizeof(vis));
if(find((a[i].x-1)*24+a[i].y))output++;
}
printf("%d\n",n-output);
}
}