poj 3216 Repairing Company

http://poj.org/problem?id=3216

 

n个地点,m个任务

每个任务有工作地点,开始时间,持续时间

最少派多少人可以完成所有的任务

 

传递闭包之后最小路径覆盖

 

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

#define N 21
#define M 201

int n,m;

int f[N][N];

bool mp[M][M];

int pos[M],start[M],last[M];

bool vis[M];
int match[M];

void read(int &x)
{
    x=0; int f=1; char c=getchar();
    while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); }
    while(isdigit(c)) { x=x*10+c-'0'; c=getchar();    }
    x*=f;
}

void floyd()
{
    for(int k=1;k<=n;++k)
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
                f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
}

void build()
{
    memset(mp,false,sizeof(mp));
    for(int i=1;i<=m;++i)
        for(int j=1;j<=m;++j)
        {
            if(i!=j)
            {
                if(start[i]+last[i]+f[pos[i]][pos[j]]<=start[j]) mp[i][j]=true;
            }
        }        
}

bool go(int u)
{
    for(int i=1;i<=m;++i)
    {
        if(!vis[i] && mp[u][i])
        {
            vis[i]=true;
            if(!match[i] || go(match[i])) 
            {
                match[i]=u;
                return true;
            }
        }
    }
    return false;
}

void Hungary()
{
    memset(match,0,sizeof(match));
    int cnt=0;
    for(int i=1;i<=m;++i)
    {
        fill(vis+1,vis+m+1,0);
        if(go(i)) cnt++;
    }
    cout<<m-cnt<<'\n';
}

int main()
{
    int inf;
    while(1)
    {
        read(n); read(m);
        if(!n) return 0;
        memset(f,63,sizeof(f));
        inf=f[0][0];
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
            {
                read(f[i][j]);
                if(f[i][j]==-1) f[i][j]=inf;
            }
                
        floyd();
        for(int i=1;i<=m;++i) read(pos[i]),read(start[i]),read(last[i]);
        build();
        Hungary();
    }
}

 

Repairing Company
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 7383   Accepted: 1993

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
posted @ 2017-12-09 09:48  TRTTG  阅读(277)  评论(0编辑  收藏  举报