Tuesday, October 16, 2018

[POJ]1548 Robots


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. 


如果我们根据所有的垃圾所在的点建图:

  • 对于节点(x, y)和(i, j),如果x <= i && y <= j,那么会有(x, y)连向(i, j)的一条有向边
这个图肯定无环,所以必定是DAG。所以这道题就变成了,在DAG中寻找最少的路径,从而可以覆盖所有的节点,每一个节点只对应一条路径。这就是DAG的最小路径覆盖问题,根据这篇文章,DAG的最小路径覆盖可以转化成二分图的最大匹配问题。所以我们建对应的二分图然后跑匈牙利算法即可,时间复杂度O(V * E),代码如下:


//POJ 1548
/*
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.
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.
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
vector<pair<int, int> > vertex;
vector<vector<int> > graph;
bool dfs(int u, vector<int>& match, vector<bool>& visited)
{
if(visited[u])return false;
visited[u] = true;
vector<int> adj = graph[u];
for(int i = 0; i < adj.size(); ++i)
{
int v = adj[i];
if(match[v] == -1 || dfs(match[v], match, visited))
{
match[u] = v;
match[v] = u;
return true;
}
}
return false;
}
int hungarian()
{
int n = graph.size(), totalMatch = 0;
vector<int> match(n, -1);
for(int i = 0; i < n; i += 2)
{
vector<bool> visited(n, false);
if(match[i] == -1 && dfs(i, match, visited))
++totalMatch;
}
return totalMatch;
}
int main() {
int x, y, num = 0;
while (scanf("%d%d", &x, &y) == 2)
{
if (x == -1 && y == -1) break;
if(x == 0 && y == 0)
{
graph = vector<vector<int> >(2 * num);
for(int i = 0; i < num; ++i)
{
for(int j = i + 1; j < num; ++j)
{
pair<int, int> nodeI = vertex[i], nodeJ = vertex[j];
if(nodeI.first <= nodeJ.first && nodeI.second <= nodeJ.second)
graph[2 * i].push_back(2 * j + 1);
if(nodeI.first >= nodeJ.first && nodeI.second >= nodeJ.second)
graph[2 * j].push_back(2 * i + 1);
}
}
int maxMatch = hungarian();
//minimum path cover in DAG = N - maximum match in G
//N = number of nodes
//G = bipartite graph with every node x in DAG splitted into two nodes x1, x2
//if the is a directed edge from x to y, then we link x1 and y2 in G, there is
//no cycle in DAG so G must be bipartite
printf("%d\n", num - maxMatch);
graph.clear();
vertex.clear();
num = 0;
}
else
{
vertex.push_back(make_pair(x, y));
++num;
}
}
return 0;
}

No comments:

Post a Comment