您的位置:首页 > 大数据 > 人工智能

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

2016-05-03 20:27 816 查看
Language:
Default

Repairing Company

Time Limit: 1000MSMemory Limit: 131072K
Total Submissions: 6925Accepted: 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 pi, ti 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

最小路径覆盖:

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

最小路径覆盖=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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: