POJ 3216 最小路径覆盖+floyd

Repairing Company
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 6646   Accepted: 1788

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

 
题目意思:
有n个点,m个任务,点之间的权值由矩阵给出,每个任务格式为p、t、d即该任务在p点发生,t时开始,完成任务需要花费的时间d。从一个点到另一个点的花费的时间即为权值。
问完成所有的任务需要多少人。
 
思路:
floyd处理一下,然后当做完i任务后可以做j任务,那么连一条边i->j,最小路径覆盖即可。
 
代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 #include <queue>
 7 #include <cmath>
 8 #include <set>
 9 using namespace std;
10 
11 #define N 25
12 #define M 205
13 
14 int max(int x,int y){return x>y?x:y;}
15 int min(int x,int y){return x<y?x:y;}
16 int abs(int x,int y){return x<0?-x:x;}
17 
18 struct node{
19     int p, t, d;
20 }a[M];
21 
22 int map[N][N];
23 int map2[N][N];
24 vector<int>ve[M];
25 int from[M];
26 bool visited[M];
27 int n;
28 
29 int march(int u){
30     int i, v;
31     for(i=0;i<ve[u].size();i++){
32         v=ve[u][i];
33         if(!visited[v]){
34             visited[v]=true;
35             if(from[v]==-1||march(from[v])){
36                 from[v]=u;
37                 return 1;
38             }
39         }
40     }
41     return 0;
42 }
43 
44 
45 main()
46 {
47     int i, j, k;
48     int q;
49     while(scanf("%d %d",&n,&q)==2){
50         if(n==0&&q==0) break;
51         for(i=1;i<=n;i++){
52             for(j=1;j<=n;j++){
53                 scanf("%d",&map[i][j]);
54             }
55         }
56         for(i=0;i<q;i++) scanf("%d %d %d",&a[i].p,&a[i].t,&a[i].d);
57         for(i=1;i<=n;i++){
58             for(j=1;j<=n;j++){
59                 for(k=1;k<=n;k++){
60                     if(map[j][i]!=-1&&map[i][k]!=-1){
61                         if(map[j][k]==-1) map[j][k]=map[j][i]+map[i][k];
62                         else map[j][k]=min(map[j][k],map[j][i]+map[i][k]);
63                     }
64                 }
65             }
66         }
67         for(i=0;i<q;i++) ve[i].clear();
68         for(i=0;i<q;i++){
69             for(j=0;j<q;j++){
70                 if(map[a[i].p][a[j].p]!=-1&&a[i].t+a[i].d+map[a[i].p][a[j].p]<=a[j].t){
71                     ve[i].push_back(j);
72                 //    printf("1111
");
73                 }
74             }
75         }
76         int num=0;
77         memset(from,-1,sizeof(from));
78         for(i=0;i<q;i++){
79             memset(visited,false,sizeof(visited));
80             if(march(i)) num++;
81         }
82     //    printf("%d
",num);
83         printf("%d
",q-num);
84     }
85 }
原文地址:https://www.cnblogs.com/qq1012662902/p/4641855.html