HDU 1350 最小边覆盖

思路

将某个人的行程视为二分图中的一个节点,用结构体记录每个节点的信息(起点时间,终点时间,位置),然后对于每个节点,看它结束后能否提前赶到其他节点,能的话就加边。

然后就用二分图匹配,算出最小边覆盖就可以了。

为什么是最小边覆盖,因为一个计程车走一条边,囊括所有节点的最少的边数就是我们要求的最少计程车数。

记得空间要开两倍,我开小了给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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值