(floyd+匈牙利算法) poj 3216

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

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;
}

  



posted @ 2015-04-27 18:04  waterfull  阅读(147)  评论(0编辑  收藏  举报