题意
传送门 HDU 1350
题解
将每一个预约的事件看成节点,若某个司机可以在预约事件 i i i 完成后执行预约事件 j j j,则从 i i i 向 j j j 连一条边,由于开车时间的单增性,可以得到一个 D A G DAG DAG。
问题转化为求 D A G DAG DAG 中使用尽可能少的路劲覆盖全部顶点。将每个点拆成 2 2 2 个,分别为 v v v 和 v ′ v' v′;若 D A G DAG DAG 存在 e = ( u , v ) e=(u,v) e=(u,v),则从 u u u 向 v ′ v' v′ 连边。所构造二分图的最大匹配可以看做路径上最多的前驱节点数,那么答案是节点数减去最大二分匹配。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define inf 0x3f3f3f3f
#define maxn 505
int N, M, V, hour[maxn], minate[maxn], sx[maxn], sy[maxn], tx[maxn], ty[maxn];
int match[maxn * 2];
vector<int> G[maxn * 2];
bool used[maxn * 2];
void add_edge(int u, int v)
{
G[u].push_back(v);
G[v].push_back(u);
}
bool dfs(int v)
{
used[v] = 1;
for (int i = 0; i < G[v].size(); i++)
{
int u = G[v][i], w = match[u];
if (w == -1 || (!used[w] && dfs(w)))
{
match[u] = v;
match[v] = u;
return 1;
}
}
return 0;
}
int bipartite_matching()
{
int res = 0;
memset(match, -1, sizeof(match));
for (int v = 0; v < V; v++)
{
if (match[v] == -1)
{
memset(used, 0, sizeof(used));
if (dfs(v))
{
++res;
}
}
}
return res;
}
void solve()
{
V = 2 * M;
for (int v = 0; v < V; v++)
{
G[v].clear();
}
for (int i = 0; i < M; i++)
{
for (int j = i + 1; j < M; j++)
{
int t1 = (hour[j] - hour[i]) * 60 + minate[j] - minate[i];
int t2 = abs(sx[i] - tx[i]) + abs(sy[i] - ty[i]) + abs(tx[i] - sx[j]) + abs(ty[i] - sy[j]);
if (t1 > t2)
{
add_edge(i, M + j);
}
}
}
printf("%d\n", M - bipartite_matching());
}
int main()
{
scanf("%d", &N);
while (N--)
{
scanf("%d", &M);
for (int i = 0; i < M; i++)
{
scanf("%d:%d%d%d%d%d", hour + i, minate + i, sx + i, sy + i, tx + i, ty + i);
}
solve();
}
return 0;
}