(floyd+匈牙利算法) poj 3216

O - Repairing Company
Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 3216

Description

Lily runs a repairing company that services the Q blocks in the city. One day the company receives M repair tasks, the ith of which occurs in block pi, has a deadline ti on any repairman’s arrival, which is also its starting time, and takes a single repairman di time to finish. Repairmen work alone on all tasks and must finish one task before moving on to another. With a map of the city in hand, Lily want to know the minimum number of repairmen that have to be assign to this day’s tasks.

Input

The input contains multiple test cases. Each test case begins with a line containing Q and M (0 < Q ≤ 20, 0 < M ≤ 200). Then follow Q lines each with Q integers, which represent a Q × Q matrix Δ = {δij}, where δij means a bidirectional road connects the ith and the jth blocks and requires δijtime to go from one end to another. If δij = −1, such a road does not exist. The matrix is symmetric and all its diagonal elements are zeroes. Right below the matrix are M lines describing the repairing tasks. The ith of these lines contains piti and di. Two zeroes on a separate line come after the last test case.

Output

For each test case output one line containing the minimum number of repairmen that have to be assigned.

Sample Input

1 2
0
1 1 10
1 5 10
0 0

Sample Output

2

题意:

给出Q的街道和M个任务

然后给出i*j的矩阵..表示第i个街道到第j个街道的距离 其中-1表示不可到达

然后接下来M行有 p t d 表示 任务在p街道 开始时间是t 完成工作花费时间是d

问最少派出多少人可以完成M个任务

思路:

用floyd求出街道之间的最短距离

根据两个任务距离花费时间+完成工作花费时间+工作开始时间<=第二个工作开始时间

确定两个任务是否可以由一个人完成..

然后得到一个二分图..

然后用n-最大匹配 求出最短路径匹配

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
#define INF 100000000
int q,m;
int a[25][25],mp[205][205],link[205],mark[205];
struct node
{
    int p,t,d;
}job[205];
bool dfs(int x)
{
    for(int i=1;i<=m;i++)
    {
        if(mark[i]==-1&&mp[x][i])
        {
            mark[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}
void floyd()
{
    for(int k=1;k<=q;k++)
    {
        for(int i=1;i<=q;i++)
        {
            for(int j=1;j<=q;j++)
            {
                if(a[i][j]>a[i][k]+a[k][j])
                    a[i][j]=a[i][k]+a[k][j];
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&q,&m)!=EOF)
    {
        if(q==0&&m==0)
            break;
        int ans=0;
        memset(mp,0,sizeof(mp));
        memset(link,-1,sizeof(link));
        for(int i=1;i<=q;i++)
        {
            for(int j=1;j<=q;j++)
            {
                scanf("%d",&a[i][j]);
                if(a[i][j]==-1)
                    a[i][j]=INF;
            }
        }
        floyd();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&job[i].p,&job[i].t,&job[i].d);
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(i!=j)
                {
                    int x=job[i].p;
                    int y=job[j].p;
                    if(job[i].t+job[i].d+a[x][y]<=job[j].t)
                        mp[i][j]=1;
                }
            }
        }
        for(int i=1;i<=m;i++)
        {
            memset(mark,-1,sizeof(mark));
            if(dfs(i))
                ans++;
        }
        printf("%d\n",m-ans);
    }
    return 0;
}

  



转载于:https://www.cnblogs.com/water-full/p/4460895.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值