当前位置: 移动技术网 > 移动技术>移动开发>IOS > K - Treasure Exploration POJ - 2594

K - Treasure Exploration POJ - 2594

2020年07月13日  | 移动技术网移动技术  | 我要评论


解题思路:

  • 其实很容易掉进一个误区,误以为这是裸的最小路径覆盖,但因为不同路径可以含有相同的点,所以需要给能到达的点之间都连上边才能转换为裸的最短路径覆盖

AC代码:

// Test1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//DAG的最小路径覆盖数 = DAG图中的节点数 - 相应二分图中的最大匹配数
//因为不同路径可以含有相同的点,需要给能到达的点之间都连上边才能转化为裸的路径覆盖
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 510;
int T, n, m;
int graph[maxn][maxn], match[maxn], vis[maxn];
void Init() {
	int x, y;
	for (int i = 1; i <= m; i++) {
		cin >> x >> y;
		graph[x][y] = 1;
	}
}
void aftermath() {
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			graph[i][j] = 0;
}
bool dfs(int x) {
	for (int i = 1; i <= n; i++) {
		if (graph[x][i] && !vis[i]) {
			vis[i] = 1;
			if (!match[i] || dfs(match[i])) {
				match[i] = x;
				return true;
			}
		}
	}
	return false;
}
int Maxmatch() {
	int sum = 0;
	memset(match, 0, sizeof(match));
	for (int i = 1; i <= n; i++) {
		memset(vis, 0, sizeof(vis));
		if (dfs(i)) sum++;
	}
	return sum;
}
void Floyd() {
	for (int k = 1; k <= n; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				if (graph[i][k] && graph[k][j])
					graph[i][j] = 1;
}
int main() {
	while (cin >> n >> m && (n || m)) {
		Init();
		Floyd();
		int ans = n - Maxmatch();
		cout << ans << endl;
		aftermath();
	}
	return 0;
}

类似题目:P6061 [加油武汉]疫情调查

本文地址:https://blog.csdn.net/weixin_45691711/article/details/107284242

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网