您的位置:首页 > 其它

PKU 3216(最小路径覆盖 + floyd)

2010-08-25 11:34 316 查看
Repairing Company

Time Limit: 1000MSMemory Limit: 131072K
Total Submissions: 3986Accepted: 1033
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

#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define MAX 1005

struct node
{
int cnt;
int t1,t2;
int sum;
};

vector< vector<int> > map;
int mark[MAX];
bool flag[MAX];
int nm,num;
node elem[MAX];

bool cmp(node a,node b)
{
return a.sum < b.sum;
}

bool dfs(int pos)     //搜pos点是否存在增广路
{
int i,pre,tp;
int len=map[pos].size();
for(i=0;i<len;i++)
{
tp=map[pos][i];
if(!flag[tp])
{
flag[tp]=true;
pre=mark[tp];
mark[tp]=pos;
if(pre==-1 || dfs(pre)) return true;    //如果没被访问过或者存在增广路,pos到该点就存在增广路
mark[tp]=pre;          //否则pos到该点就不存在增广路
}
}
return false;
}

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

int main()
{
int n,m,i,j;
int graph[25][25];
while (scanf("%d%d",&n,&m),n+m)
{
num=0;
nm=m*2;
map.clear();
map.resize(nm+10);
memset(mark,-1,sizeof(mark));
for(i = 1;i <= n;i ++)
{
for (j = 1;j <= n;j ++)
{
scanf("%d",&graph[i][j]);
if(graph[i][j] == -1)
graph[i][j] = 999999;
}
}
floyd(n,graph);
for (i = 1;i <= m;i ++)
{
scanf("%d%d%d",&elem[i].cnt,&elem[i].t1,&elem[i].t2);
elem[i].sum = elem[i].t1 + elem[i].t2;
}
sort(elem+1,elem+m+1,cmp);
for (i = 1;i < m;i ++)
{
for (j = i+1;j <= m;j ++)
{
if((elem[i].sum + graph[elem[i].cnt][elem[j].cnt]) <= elem[j].t1)
{
map[i].push_back(j+m);
}

}
}
for (i=1;i<=m;i++)
{
memset(flag,0,sizeof(flag));
if(dfs(i)) num++;
}
printf("%d/n",m-num);
}
return 0;
}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: