HDU-1350 Taxi Cab Scheme 最小路径覆盖=总点数-最大匹配数

#include <bits/stdc++.h>

using namespace std;

const int maxn = 505;
bool g[maxn][maxn], vis[maxn];
int match[maxn];
int val[maxn], keep[maxn];
int a[maxn], b[maxn], c[maxn], d[maxn];
int t, n, h, m;

bool dfs(int u) {
	for(int i = 1; i <= n; ++i) {
		if(vis[i] || !g[u][i]) continue;
		vis[i] = 1;
		if(match[i] == -1 || dfs(match[i])) {
			match[i] = u;
			return 1;
		}
	}
	return 0;
}
int main() {
	scanf("%d", &t);
	while(t--) {
		scanf("%d", &n);
		memset(g, 0, sizeof g);
		for(int i = 1; i <= n; ++i) {
			scanf("%d:%d", &h, &m);
			scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]);
			//出发时间
			val[i] = h*60+m;
			//需要行走的时间
			keep[i] = abs(a[i]-c[i]) + abs(b[i]-d[i]);
			for(int j = 1; j < i; ++j) {
				//如果j的出发时间+j的行走时间+j的终点到i的起点的距离 < i的出发时间
				//那么j就可以进行i
				if(val[j]+keep[j]+abs(a[i]-c[j])+abs(b[i]-d[j]) < val[i])
					g[j][i] = 1;
				//如果i的出发时间+i的行走世间+i的终点到j的起点的距离 < j的出发时间
				//那么i就可以进行j
				if(val[i]+keep[i]+abs(a[j]-c[i])+abs(b[j]-d[i]) < val[j])
					g[i][j] = 1;
			}
		}
		int ans = 0;
		memset(match, -1, sizeof match);
		for(int i = 1; i <= n; ++i) {
			memset(vis, 0, sizeof vis);
			if(dfs(i)) 
				++ans;
		}
		printf("%d
", n-ans);
	}
	return 0;
}

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12402663.html