poj1548(最小路径覆盖)

思路:最小路径覆盖是很容易想到的(本题就是求最小的路径条数覆盖所有的点),关键是如何建图,其实也不难想到,对于当前点,如果后面的点它能够到达,那么就连边。

最小路径覆盖=顶点数-最大匹配。

点击题目链接
/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 888;
int xx[maxn],yy[maxn];
int link[maxn];
bool vis[maxn];
int n;
int g[maxn][maxn];
bool dfs(int u){
	for (int i = 1;i <= n;i++){
		if (g[u][i] && !vis[i]){
			vis[i] = true;
			if (link[i] == -1 || dfs(link[i])){
				link[i] = u;
				return true;
			}
		}
	}
	return false;
}
int Hungary(){
	int ret = 0;
	MEM(link, -1);
	for (int i = 1;i <= n;i++){
		MEM(vis, false);
		if (dfs(i)) ret++;
	}
	return ret;
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int x,y;
	n = 0;
	while(~scanf("%d%d",&x,&y)){
		if (x == -1 && y == -1) break;
		if (x == 0 && y == 0){
			MEM(g, 0);
			for (int i = 1;i <= n;i++){
				for (int j = i + 1;j <= n;j++){
					if (xx[j] >= xx[i] && yy[j] >= yy[i]){
						g[i][j] = true;
					}
				}
			}
			printf("%d\n",n - Hungary());
			n = 0;
		}
		else{
			xx[++n] = x;
			yy[n] = y;
		}
	}
	return 0;
}


你可能感兴趣的:(图论,二分图匹配)