思路
将某个人的行程视为二分图中的一个节点,用结构体记录每个节点的信息(起点时间,终点时间,位置),然后对于每个节点,看它结束后能否提前赶到其他节点,能的话就加边。
然后就用二分图匹配,算出最小边覆盖就可以了。
为什么是最小边覆盖,因为一个计程车走一条边,囊括所有节点的最少的边数就是我们要求的最少计程车数。
记得空间要开两倍,我开小了给WA了。
代码
#include <map>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 505;
vector <int> G[2 * maxn];
int match[2 * maxn];
bool used[2 * maxn];
int m;
struct node
{
int s,e,t,x1,x2,y1,y2;
}a[maxn];
bool dfs(int u){
used[u] = true;
for(int i = 0; i < G[u].size(); ++i)
{
int v = G[u][i], w = match[v];
if(w < 0 || !used[w] &&dfs(w))
{
match[u] = v;
match[v] = u;
return true;
}
}
return false;
}
int Hungary(){
int ans = 0;
memset(match, -1, sizeof(match));
for(int i = 1; i <= 2 * m; ++i)
{
memset(used, 0, sizeof(used));
if(match[i] < 0)
{
if(dfs(i))
ans++;
}
}
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(a, 0, sizeof(a));
scanf("%d", &m);
for(int i = 1; i <= 2 * m; i++)
{
G[i].clear();
}
for(int i = 1; i <= m; i++)
{
int hour, minute, x1, y1, x2, y2;
scanf("%d:%d %d %d %d %d", &hour, &minute, &x1, &y1, &x2, &y2);
a[i].t = abs(x1 - x2) + abs(y1 - y2);
a[i].x1 = x1, a[i].y1 = y1, a[i].x2 = x2, a[i].y2 = y2;
a[i].s = hour * 60 + minute;
a[i].e = a[i].s + a[i].t;
}
for(int i = 1; i <= m; i++)
{
for(int j = i + 1; j <= m; j++)
{
int x1 = a[j].x1, y1 = a[j].y1, x2 = a[i].x2, y2 = a[i].y2;
if(a[i].e + abs(x1 - x2) + abs(y1 - y2) < a[j].s)
{
G[i].push_back(j + m);
G[j + m].push_back(i);
}
}
}
printf("%d\n", m - Hungary());
}
return 0;
}