POJ 1548 Robots(最小路径覆盖)

POJ 1548 Robots

题目链接

题意:乍一看还以为是小白上那题dp,其实不是,就是求一共几个机器人可以覆盖所有路径

思路:最小路径覆盖问题,一个点如果在另一个点右下方,就建边,然后跑最小路径覆盖即可

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 25 * 25;

int x[N], y[N], cnt = 1;
vector<int> g[N];

bool judge(int i, int j) {
	return x[j] >= x[i] && y[j] >= y[i];
}

int left[N], vis[N];

bool dfs(int u) {
	for (int i = 0; i < g[u].size(); i++) {
		int v = g[u][i];
		if (vis[v]) continue;
		vis[v] = 1;
		if (left[v] == -1 || dfs(left[v])) {
			left[v] = u;
			return true;
		}
	}
	return false;
}

int hungary() {
	int ans = 0;
	memset(left, -1, sizeof(left));
	for (int i = 0; i < cnt; i++) {
		memset(vis, 0, sizeof(vis));
		if (dfs(i)) ans++;
	}
	return ans;
}

int main() {
	while (~scanf("%d%d", &x[0], &y[0])) {
		if (x[0] == -1 && y[0] == -1) break;
		while (~scanf("%d%d", &x[cnt], &y[cnt])) {
			if (x[cnt] == 0 && y[cnt] == 0) break;
			cnt++;
		}
		for (int i = 0; i < cnt; i++) {
			g[i].clear();
			for (int j = 0; j < i; j++) {
				if (judge(i, j)) g[i].push_back(j);
				if (judge(j, i)) g[j].push_back(i);
			}
		}
		printf("%d\n", cnt - hungary());
		cnt = 1;
	}
	return 0;
}
时间: 2024-08-04 23:16:35

POJ 1548 Robots(最小路径覆盖)的相关文章

POJ 1422 DAG最小路径覆盖

求无向图中能覆盖每个点的最小覆盖数 单独的点也算一条路径 这个还是可以扯到最大匹配数来,原因跟上面的最大独立集一样,如果某个二分图(注意不是DAG上的)的边是最大匹配边,那说明只要取两个端点只要一条边即可. 故最小覆盖数还是 顶点数-最大匹配数 根据DAG建图的时候,就是DAG有边就给对应的端点建边 #include <iostream> #include <cstdio> #include <cstring> using namespace std; int d[15

POJ 3020 (二分图+最小路径覆盖)

题目链接:http://poj.org/problem?id=3020 题目大意:读入一张地图.其中地图中圈圈代表可以布置卫星的空地.*号代表要覆盖的建筑物.一个卫星的覆盖范围是其周围上下左右四个点.问最少需要几个卫星才能覆盖所有建筑物. 解题思路: 有点类似POJ 1328的覆盖题,不过那题比较简单可以贪心.这题你可以YY试试. 覆盖问题其实可以用图论解决.这题就属于最小路径覆盖,手动由一个点出发连一些路径,这样Hungry就能求出最少需要多少这样的中心点,就可以达成目标了. 本题最大的疑问是

POJ 2594 二分图最小路径覆盖

点击打开链接 题意:将所有点都连起来至少需要多少条路径 思路:二分图的最小路径覆盖,而最小路径覆==图的顶点数-图的最大匹配,而当初还学习过最小顶点覆盖==最大匹配,而最小顶点覆盖需要连双向边,结果除以2,那是因为1-->2时,点1和点2都已经用过,所以我在连一个相应的一条边,代表这两个点不能在用了,样例详见hdu 1054 第二组.而接下来的求最小路径覆盖的最大匹配我们就只能是单向的,这个为什么可以避免呢,因为1-->2-->3这样的话,最小路径为1,但是转化为二分图上的话,对应的点2

poj 3020 二分图最小路径覆盖

二分图最小路径覆盖=|v|-最大匹配.此题为有向图,切所有边正反向存了两遍,所以结果匹配数要除以2 // // main.cpp // poj3020 // // Created by Fangpin on 15/5/29. // Copyright (c) 2015年 FangPin. All rights reserved. // #include <iostream> #include <cstdio> #include <vector> #include <

Taxi Cab Scheme POJ - 2060 二分图最小路径覆盖

Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coordination of the cabs in order to pick up the customers calling to get a cab as soon as possible,there is also a need to schedule all the taxi rides whi

POJ 2594 传递闭包的最小路径覆盖

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7171   Accepted: 2930 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored

POJ1548 Robots【二分图最小路径覆盖】

题目链接: http://poj.org/problem?id=1548 题目大意: 在一个N*M(N <= 24,M <= 24)的图中,有很多垃圾, 清理垃圾的机器人从左上角开始清理.已 知机器人只能向右或是向下清理垃圾,在清理完一个地方的垃圾后可以继续向右或是向下去清理 其他垃圾.最终运行到(N,M)的位置终止.问:最少需要多少个机器人,能清理完所有的垃圾. 思路: 图中没有给N和M的大小,只是给出了垃圾的位置.输入用0 0表示一组数据输入结束.建一个结构 体来存储垃圾的坐标值.现在来建

POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】

Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2594 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploratio

POJ Treasure Exploration 【DAG交叉最小路径覆盖】

传送门:http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 9802   Accepted: 3979 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure e