POJ-3216-Repairing Company(最小路径覆盖)

来源:互联网 发布:排版打印软件 编辑:程序博客网 时间:2024/06/06 15:51
Language:
Repairing Company
Time Limit: 1000MS Memory Limit: 131072KTotal Submissions: 6925 Accepted: 1861

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 201 1 101 5 100 0

Sample Output

2

Source






最小路径覆盖:

根据题意,可以计算出两点之间的可达关系,根据可达关系建图,求出最大匹配,

最小路径覆盖=N-最大匹配。


floyd都写错了找了半天bug  mdzz。OTZ。。。。


#include <iostream>#include <cstdio>#include <cstring>#include <set>#include <queue>#include <cmath>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define bug cout<<"bug\n"const int MAXN = 207;const int MAXM = 1e6+7;int map_[MAXN][MAXN];int cost[MAXN][MAXN];int n,m;struct node{    int id;    int start,endd;} task[MAXN];int link[MAXN],vis[MAXN];int DFS(int u){    for(int v=0; v<m; ++v)        if(map_[u][v] && !vis[v])        {            vis[v]=1;            if(link[v]==-1 || DFS(link[v]))            {                link[v]=u;                return 1;            }        }    return 0;}int hungary(){    int ans=0;    memset(link,-1,sizeof(link));    for(int u=0; u<m; ++u)    {        memset(vis,0,sizeof(vis));        ans+=DFS(u);    }    return ans;}void floyd(){    for(int k=0; k<n; ++k)        for(int i=0; i<n; ++i)            for(int j=0; j<n; ++j)                if(cost[i][j]>cost[i][k]+cost[k][j])                    cost[i][j]=cost[i][k]+cost[k][j];}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        if(!n||!m)break;        memset(cost,INF,sizeof(cost));        memset(map_,0,sizeof(map_));        for(int i=0; i<n; ++i)            for(int j=0; j<n; ++j)            {                scanf("%d",&cost[i][j]);                if(cost[i][j]==-1)cost[i][j]=INF;            }        for(int i=0; i<m; ++i)        {            int c;            scanf("%d%d%d",&task[i].id,&task[i].start,&c);            task[i].endd=task[i].start+c;        }        floyd();        for(int i=0; i<m; ++i)            for(int j=0; j<m; ++j)                if(i!=j && task[j].start-task[i].endd>=cost[ task[i].id-1 ][ task[j].id-1 ])                    map_[i][j]=1;        cout<<m-hungary()<<endl;    }    return 0;}


0 0
原创粉丝点击