hdu-4292.food(绫籨ining缃戠粶娴佸缓鍥?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu-4292.food(绫籨ining缃戠粶娴佸缓鍥?相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/inpu' title='inpu'>inpu php rem ESS bool some enc person bsp
Food
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9289 Accepted Submission(s): 3019
銆€銆€The issue comes up as people in your college are more and more difficult to serve with meal: They eat only some certain kinds of food and drink, and with requirement unsatisfied, go away directly.
銆€銆€You have prepared F (1 <= F <= 200) kinds of food and D (1 <= D <= 200) kinds of drink. Each kind of food or drink has certain amount, that is, how many people could this food or drink serve. Besides, You know there’re N (1 <= N <= 200) people and you too can tell people’s personal preference for food and drink.
銆€銆€Back to your goal: to serve as many people as possible. So you must decide a plan where some people are served while requirements of the rest of them are unmet. You should notice that, when one’s requirement is unmet, he/she would just go away, refusing any service.
銆€銆€For each test case, the first line contains three numbers: N,F,D, denoting the number of people, food, and drink.
銆€銆€The second line contains F integers, the ith number of which denotes amount of representative food.
銆€銆€The third line contains D integers, the ith number of which denotes amount of representative drink.
銆€銆€Following is N line, each consisting of a string of length F. 顏塭 jth character in the ith one of these lines denotes whether people i would accept food j. “Y” for yes and “N” for no.
銆€銆€Following is N line, each consisting of a string of length D. 顏塭 jth character in the ith one of these lines denotes whether people i would accept drink j. “Y” for yes and “N” for no.
銆€銆€Please process until EOF (End Of File).
1 /* 2 缁撶偣0 ~ n - 1瀛樺乏鐗涚粨鐐? 3 缁撶偣n ~ 2 * n - 1瀛樺彸鐗涚粨鐐? 4 缁撶偣2 * n ~ 2 * n + f - 1瀛樺乏椋熺墿 5 缁撶偣2 * n + f ~ 2 * n + f * 2 - 1瀛樺彸椋熺墿 6 缁撶偣2 * n + 2 * f ~ 2 * n + 2 * f + d - 1瀛橀ギ鏂欏乏 7 缁撶偣2 * n + 2 * f + d ~ 2 * n + 2 * f + d * 2 - 1瀛橀ギ鏂欏彸 8 缁撶偣2 * n + 2 * f + 2 * d涓簊锛宼 = s = 1銆? 9 */ 10 11 #include <cstdio> 12 #include <cstring> 13 #include <algorithm> 14 using namespace std; 15 16 const int maxn = 200 + 5, maxm = 1000 * 1000 + 5, inf = 0x3f3f3f3f; 17 int numf[maxn], numd[maxn]; 18 char str[maxn]; 19 20 int tot, head[maxn << 3], que[maxn << 3], dep[maxn << 3], cur[maxn << 3], sta[maxn << 3]; 21 22 struct Edge 23 int to, cap, flow, next, from; 24 edge[maxm << 1]; 25 26 void init() 27 tot = 2; 28 memset(head, -1, sizeof head); 29 30 31 void addedge(int u, int v, int w) 32 edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0; edge[tot].from = u; 33 edge[tot].next = head[u]; head[u] = tot ++; 34 edge[tot].to = u; edge[tot].cap = 0; edge[tot].flow = 0; edge[tot].from = v; 35 edge[tot].next = head[v]; head[v] = tot ++; 36 37 38 bool bfs(int s, int t, int n) 39 memset(dep, -1, sizeof dep[0] * (n + 1)); 40 int front = 0, tail = 0; 41 dep[s] = 0; 42 que[tail ++] = s; 43 while(front < tail) 44 int u = que[front ++]; 45 for(int i = head[u]; ~i; i = edge[i].next) 46 int v = edge[i].to; 47 if(edge[i].cap > edge[i].flow && dep[v] == -1) 48 dep[v] = dep[u] + 1; 49 if(v == t) return true; 50 que[tail ++] = v; 51 52 53 54 return false; 55 56 57 int dinic(int s, int t, int n) 58 int maxflow = 0; 59 while(bfs(s, t, n)) 60 for(int i = 0; i <= n; i ++) cur[i] = head[i]; 61 int u = s, tail = 0; 62 while(~cur[s]) 63 if(u == t) 64 int tp = inf; 65 for(int i = tail - 1; i >= 0; i --) 66 tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow); 67 maxflow += tp; 68 for(int i = tail - 1; i >= 0; i --) 69 edge[sta[i]].flow += tp; 70 edge[sta[i] ^ 1].flow -= tp; 71 if(edge[sta[i]].cap - edge[sta[i]].flow == 0) tail = i; 72 73 u = edge[sta[tail] ^ 1].to; 74 else if(~ cur[u] && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + 1 == dep[edge[cur[u]].to]) 75 sta[tail ++] = cur[u]; 76 u = edge[cur[u]].to; 77 else 78 while(u != s && cur[u] == -1) 79 u = edge[sta[-- tail] ^ 1].to; 80 cur[u] = edge[cur[u]].next; 81 82 83 84 return maxflow; 85 86 87 int main() 88 int n, f, d; 89 while(~scanf("%d %d %d", &n, &f, &d)) 90 init(); 91 int s = 2 * n + 2 * f + d * 2, t = s + 1;//瓒呯骇婧愮偣鍜岃秴绾ф眹鐐?/span> 92 for(int i = 0; i < f; i ++) 93 scanf("%d", &numf[i]); 94 addedge(s, 2 * n + i, inf);//瓒呯骇婧愮偣鍒伴鐗╁乏 95 addedge(2 * n + i, 2 * n + f + i, numf[i]);//椋熺墿宸﹀埌椋熺墿鍙?/span> 96 97 for(int i = 0; i < d; i ++) 98 scanf("%d", &numd[i]); 99 addedge(2 * n + 2 * f + i, 2 * n + 2 * f + d + i, numd[i]);//楗枡宸﹀埌楗枡鍙?/span> 100 addedge(2 * n + 2 * f + d + i, t, inf);//楗枡鍙冲埌瓒呯骇姹囩偣 101 102 for(int i = 0; i < n; i ++) 103 addedge(i, n + i, 1);//宸︾墰鍒板彸鐗?/span> 104 105 for(int i = 0; i < n; i++) 106 scanf("%s", str); 107 for(int j = 0; j < f; j ++) 108 if(str[j] == 鈥?/span>Y鈥?/span>) 109 addedge(2 * n + f + j, i, 1);//浠庨鐗╁彸鍒板乏鐗?/span> 110 111 112 for(int i = 0; i < n; i ++) 113 scanf("%s", str); 114 for(int j = 0; j < d; j ++) 115 if(str[j] == 鈥?/span>Y鈥?/span>) 116 addedge(n + i, 2 * n + 2 * f + j, 1);//浠庡彸鐗涘埌宸﹂ギ鏂?/span> 117 118 119 // for(int i = 2; i <= tot; i ++) 120 // printf("%d -> %d\n", edge[i].from, edge[i].to); 121 // 122 printf("%d\n", dinic(s, t, 2 * n + 2 * f + 2 * d + 2)); 123 124 return 0; 125
涓嬮潰杩欑寤哄浘鏂规硶鍜屼笂闈㈢殑绫讳技锛屽彧鏄笂鍥炬槸鎷嗙偣闄愬埗鐐规祦閲忥紝鑰屾垜浠煡閬撳浜庢瘡涓€浠堕鐗╋紝濡傛灉鎴戜滑鏈変竴涓汉閫夊彇瀹冿紝閭d箞瀹冨繀瀹氭槸鍙€夊彇浜嗕竴浠讹紝鍥犱负鍚庨潰鎷嗙偣n鍐冲畾鐨勶紝閭d箞涔熷氨鏄瘡涓汉鍙兘鍙栦粬鎵€鍠滄椋熺墿涓殑涓€绉嶄腑鐨勪竴涓紝鎵€浠ユ垜浠彧闇€瑕佸鎴戜滑鑳藉鎻愪緵鐨勬煇绉嶉鐗╅檺閲忓氨鍙互浜嗭紝涔熷氨鏄粠婧愮偣鍒版煇绉嶉鐗╂潈鍊间负food_num锛岃繖鏍峰氨鍙互闄愬埗浣忔瘡绉嶉鐗╃殑鐢ㄩ噺浜嗭紝鎺ョ潃鐪嬮ギ鏂欙紝濡傛灉鏌愪釜浜洪€夊彇浜嗕竴涓ギ鏂欙紝閭d箞浠栦篃鍙兘閫夊彇杩欎竴绉嶉ギ鏂欎腑鐨勪竴鐡讹紝鍥犱负鍓嶉潰宸茬粡瀵筺鎷嗙偣瀵艰嚧瀹冭兘鎵╁睍鐨勬祦涔熷彧鏈?锛屾墍浠ュ鑷村ス閫夌殑楗枡涔熸槸1瀵?锛屾墍浠ユ兂瑕侀檺鍒堕ギ鏂欑殑涓暟锛屼篃鍙渶瑕佹棤闄愮储鍙栵紝鐩村埌鏈€鍚庢棤娉曟祦鍒版眹鐐瑰氨ok锛岄偅涔熷氨鏄粠楗枡鍒版眹鐐规潈鍊间负drink_num銆?/p>
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 200 + 5, maxm = 1000 * 1000 + 5, inf = 0x3f3f3f3f; 7 int numf[maxn], numd[maxn]; 8 char str[maxn]; 9 10 int tot, head[maxn << 3], que[maxn << 3], dep[maxn << 3], cur[maxn << 3], sta[maxn << 3]; 11 12 struct Edge 13 int to, cap, flow, next, from; 14 edge[maxm << 1]; 15 16 void init() 17 tot = 2; 18 memset(head, -1, sizeof head); 19 20 21 void addedge(int u, int v, int w) 22 edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0; edge[tot].from = u; 23 edge[tot].next = head[u]; head[u] = tot ++; 24 edge[tot].to = u; edge[tot].cap = 0; edge[tot].flow = 0; edge[tot].from = v; 25 edge[tot].next = head[v]; head[v] = tot ++; 26 27 28 bool bfs(int s, int t, int n) 29 memset(dep, -1, sizeof dep[0] * (n + 1)); 30 int front = 0, tail = 0; 31 dep[s] = 0; 32 que[tail ++] = s; 33 while(front < tail) 34 int u = que[front ++]; 35 for(int i = head[u]; ~i; i = edge[i].next) 36 int v = edge[i].to; 37 if(edge[i].cap > edge[i].flow && dep[v] == -1) 38 dep[v] = dep[u] + 1; 39 if(v == t) return true; 40 que[tail ++] = v; 41 42 43 44 return false; 45 46 47 int dinic(int s, int t, int n) 48 int maxflow = 0; 49 while(bfs(s, t, n)) 50 for(int i = 0; i <= n; i ++) cur[i] = head[i]; 51 int u = s, tail = 0; 52 while(~cur[s]) 53 if(u == t) 54 int tp = inf; 55 for(int i = tail - 1; i >= 0; i --) 56 tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow); 57 maxflow += tp; 58 for(int i = tail - 1; i >= 0; i --) 59 edge[sta[i]].flow += tp; 60 edge[sta[i] ^ 1].flow -= tp; 61 if(edge[sta[i]].cap - edge[sta[i]].flow == 0) tail = i; 62 63 u = edge[sta[tail] ^ 1].to; 64 else if(~ cur[u] && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + 1 == dep[edge[cur[u]].to]) 65 sta[tail ++] = cur[u]; 66 u = edge[cur[u]].to; 67 else 68 while(u != s && cur[u] == -1) 69 u = edge[sta[-- tail] ^ 1].to; 70 cur[u] = edge[cur[u]].next; 71 72 73 74 return maxflow; 75 76 77 int main() 78 int n, f, d; 79 while(~scanf("%d %d %d", &n, &f, &d)) 80 init(); 81 int s = 2 * n + f + d, t = s + 1;//瓒呯骇婧愮偣鍜岃秴绾ф眹鐐?/span> 82 for(int i = 0; i < f; i ++) 83 scanf("%d", &numf[i]); 84 addedge(s, n * 2 + i, numf[i]); 85 86 for(int i = 0; i < d; i ++) 87 scanf("%d", &numd[i]); 88 addedge(n * 2 + f + i, t, numd[i]); 89 90 for(int i = 0; i < n; i++) 91 addedge(i, n + i, 1);//宸︾墰鍒板彸鐗?/span> 92 scanf("%s", str); 93 for(int j = 0; j < f; j ++) 94 if(str[j] == 鈥?/span>Y鈥?/span>) 95 addedge(2 * n + j, i, 1); 96 97 98 for(int i = 0; i < n; i ++) 99 scanf("%s", str); 100 for(int j = 0; j < d; j ++) 101 if(str[j] == 鈥?/span>Y鈥?/span>) 102 addedge(n + i, 2 * n + f + j, 1);//浠庡彸鐗涘埌楗枡 103 104 105 // for(int i = 2; i <= tot; i ++) 106 // printf("%d -> %d\n", edge[i].from, edge[i].to); 107 // 108 printf("%d\n", dinic(s, t, 2 * n + f + d + 2)); 109 110 return 0; 111
涓嶅緱涓嶆壙璁よ繖绉嶅仛娉曠‘瀹炶妭鐪佷簡寰堝绌洪棿鍛€銆?/p>
以上是关于hdu-4292.food(绫籨ining缃戠粶娴佸缓鍥?的主要内容,如果未能解决你的问题,请参考以下文章