POJ 3216 Repairing Company (二分匹配)

Repairing Company
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 5696 Accepted: 1530

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 δij time 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

Source

 
 
题意:  一家维修公司,服务的 Q 街区。每个街区有一定距离, 一天公司收到M修理任务,第i个任务发生在第i个街区, 并且每个任务都有起始时间 ti,完成一个任务需要di时间.修理工必须完成一个任务后,再去另一个. 问要完成这M个任务最少需要多少个修理工?

思路:有向图的最小路径覆盖。先用floyd求出两点间的最短距离,然后建图 若第j个街区任务的开始时间大于等于第i个街区的任务完成时间 + 路上花费时间 则i到j间连一条边 建立二分图 求最大匹配 。ans = 顶点数 - 最大匹配数。
 
 
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int VM=30;
const int INF=0x3f3f3f3f;

int q,m;
int map[30][30],g[220][220],linker[220],vis[220];

struct Edge{
    int id;
    int x,y;
}task[220];

int DFS(int u){
    int v;
    for(v=1;v<=m;v++)
        if(g[u][v] && !vis[v]){
            vis[v]=1;
            if(linker[v]==-1 || DFS(linker[v])){
                linker[v]=u;
                return 1;
            }
        }
    return 0;
}

int Hungary(){
    int u,ans=0;
    memset(linker,-1,sizeof(linker));
    for(u=1;u<=m;u++){
        memset(vis,0,sizeof(vis));
        if(DFS(u))
            ans++;
    }
    return ans;
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d%d",&q,&m)){
        if(q==0 && m==0)
            break;
        memset(g,0,sizeof(g));
        for(int i=1;i<=q;i++)
            for(int j=1;j<=q;j++){
                scanf("%d",&map[i][j]);
                if(map[i][j]==-1)
                    map[i][j]=INF;
            }
        int d;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&task[i].id,&task[i].x,&d);
            task[i].y=task[i].x+d;
        }
        for(int k=1;k<=q;k++)
            for(int i=1;i<=q;i++)
                for(int j=1;j<=q;j++)
                    if(map[i][j]>map[i][k]+map[k][j])
                        map[i][j]=map[i][k]+map[k][j];
        for(int i=1;i<=m;i++)
            for(int j=1;j<=m;j++)
                if(task[j].x>=task[i].y+map[task[i].id][task[j].id])
                    g[i][j]=1;
        int ans=Hungary();
        printf("%d\n",m-ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jackge/archive/2013/04/14/3019843.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值