3216

1 /*
2 最小路径覆盖问题
3 */
4
5 // include file
6 #include <cstdio>
7 #include <cstdlib>
8 #include <cstring>
9 #include <cmath>
10 #include <cctype>
11 #include <ctime>
12
13 #include <iostream>
14 #include <sstream>
15 #include <fstream>
16 #include <iomanip>
17 #include <bitset>
18 #include <strstream>
19
20 #include <algorithm>
21 #include <string>
22 #include <vector>
23 #include <queue>
24 #include <set>
25 #include <list>
26 #include <functional>
27
28 using namespace std;
29
30 // typedef
31 typedef long long LL;
32 typedef unsigned long long ULL;
33
34 //
35 #define read freopen("in.txt","r",stdin)
36 #define write freopen("out.txt","w",stdout)
37 #define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
38 #define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
39 #define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
40 #define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
41
42 #define FF(i,a) for(int i=0;i<(a);i+++)
43 #define FFD(i,a) for(int i=(a)-1;i>=0;i--)
44 #define Z(a) (a<<1)
45 #define Y(a) (a>>1)
46
47 const double eps = 1e-6;
48 const double INFf = 1e10;
49 const int INFi = 1000000000;
50 const double Pi = acos(-1.0);
51
52 template<class T> inline T sqr(T a){return a*a;}
53 template<class T> inline T TMAX(T x,T y)
54 {
55 if(x>y) return x;
56 return y;
57 }
58 template<class T> inline T TMIN(T x,T y)
59 {
60 if(x<y) return x;
61 return y;
62 }
63 template<class T> inline void SWAP(T &x,T &y)
64 {
65 T t = x;
66 x = y;
67 y = t;
68 }
69 template<class T> inline T MMAX(T x,T y,T z)
70 {
71 return TMAX(TMAX(x,y),z);
72 }
73
74
75 // code begin
76
77 #define MAXN 210
78 int Q,M;
79 int tm[25][25];
80 struct node
81 {
82 int p;
83 int t;
84 int d;
85 };
86 node tk[MAXN];
87 vector<int> G[MAXN];
88 int mt[MAXN];
89 bool used[MAXN];
90
91 bool Isok(int i,int j)
92 {
93 // 干完i,能否赶上j开工
94 if( tm[tk[i].p][tk[j].p]!=-1 && tk[i].t+tk[i].d+tm[tk[i].p][tk[j].p]<=tk[j].t)
95 return true;
96 return false;
97 }
98
99 bool hungarian_MM(int i)
100 {
101 FORj(0,G[i].size(),1)
102 {
103 if(!used[ G[i][j] ])
104 {
105 used[ G[i][j] ] = true;
106 if( mt[G[i][j]]==-1 || hungarian_MM(mt[G[i][j]]))
107 {
108 mt[G[i][j]] = i;
109 return true;
110 }
111 }
112 }
113 return false;
114 }
115
116 void Floyd()
117 {
118 FORk(1,Q+1,1)
119 {
120 FORi(1,Q+1,1)
121 {
122 FORj(1,Q+1,1)
123 {
124 if(i==j) continue;
125 if( tm[i][k]!=-1 && tm[k][j]!=-1 )
126 {
127 if(tm[i][j]==-1) tm[i][j] = tm[i][k]+tm[k][j];
128 else tm[i][j] = TMIN(tm[i][j],tm[i][k]+tm[k][j]);
129 }
130 }
131 }
132 }
133 }
134
135 int main()
136 {
137 read;
138 write;
139 int ans;
140 while(scanf("%d %d",&Q,&M)!=-1)
141 {
142 if(Q+M==0) break;
143 // matrix
144 FORi(1,Q+1,1)
145 {
146 FORj(1,Q+1,1)
147 {
148 scanf("%d",&tm[i][j]);
149 }
150 }
151
152 // 输入tk
153 FORi(0,M,1)
154 {
155 scanf("%d %d %d",&tk[i].p,&tk[i].t,&tk[i].d);
156 }
157 // 对tm还要再次FLoyd一次
158 Floyd();
159
160 // 建图
161 FORi(0,M,1)
162 {
163 G[i].clear();
164 FORj(0,M,1)
165 {
166 if(i==j) continue;
167 if( Isok(i,j))
168 G[i].push_back(j);
169 }
170 }
171
172 // hungarian_MM
173 memset(mt,-1,sizeof(int)*MAXN);
174 ans = 0;
175 FORi(0,M,1)
176 {
177 memset(used,0,sizeof(bool)*MAXN);
178 if( hungarian_MM(i))
179 ans++;
180 }
181 printf("%d\n",M-ans);
182 }
183 return 0;
184 }
posted @ 2011-03-05 17:17  AC2012  阅读(366)  评论(0编辑  收藏  举报