hdu 4292 Food 网络流

题目链接

给你f种食物, 以及每种食物的个数, d种饮料, 以及个数, n个人, 以及每个人可以接受的食物种类和饮料种类。 每个人必须得到一种食物和一种饮料。 问最后得到满足的人的个数。

因为一个人只能得到一种食物, 所以把人拆成两个点, 之间连一条权值为1的边。 建一个源点s, 汇点t, 每种食物向源点连边, 权值为食物的个数, 饮料向汇点连边, 权值为个数。 如果一个人可以接受某种饮料, u‘就向饮料连一条权值为1的边; 如果一个人可以接受某种食物, 食物就向u连权值为1的边。

每次数组都要开的很大才可以过...........

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define pb(x) push_back(x)
  4 #define ll long long
  5 #define mk(x, y) make_pair(x, y)
  6 #define lson l, m, rt<<1
  7 #define mem(a) memset(a, 0, sizeof(a))
  8 #define rson m+1, r, rt<<1|1
  9 #define mem1(a) memset(a, -1, sizeof(a))
 10 #define mem2(a) memset(a, 0x3f, sizeof(a))
 11 #define rep(i, a, n) for(int i = a; i<n; i++)
 12 #define ull unsigned long long
 13 typedef pair<int, int> pll;
 14 const double PI = acos(-1.0);
 15 const double eps = 1e-8;
 16 const int mod = 1e9+7;
 17 const int inf = 1061109567;
 18 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
 19 const int maxn = 8e4+5;
 20 int head[maxn*4], s, t, num, q[maxn], dis[maxn];
 21 struct node
 22 {
 23     int to, nextt, c;
 24 }e[maxn*4];
 25 void init() {
 26     mem1(head);
 27     num = 0;
 28 }
 29 void add(int u, int v, int c) {
 30     e[num].to = v; e[num].nextt = head[u]; e[num].c = c; head[u] = num++;
 31     e[num].to = u; e[num].nextt = head[v]; e[num].c = 0; head[v] = num++;
 32 }
 33 int bfs() {
 34     int u, v, st = 0, ed = 0;
 35     mem(dis);
 36     dis[s] = 1;
 37     q[ed++] = s;
 38     while(st<ed) {
 39         u = q[st++];
 40         for(int i = head[u]; ~i; i = e[i].nextt) {
 41             v = e[i].to;
 42             if(e[i].c&&!dis[v]) {
 43                 dis[v] = dis[u]+1;
 44                 if(v == t)
 45                     return 1;
 46                 q[ed++] = v;
 47             }
 48         }
 49     }
 50     return 0;
 51 }
 52 int dfs(int u, int limit) {
 53     if(u == t)
 54         return limit;
 55     int cost = 0;
 56     for(int i = head[u]; ~i; i = e[i].nextt) {
 57         int v = e[i].to;
 58         if(e[i].c&&dis[u] == dis[v]-1) {
 59             int tmp = dfs(v, min(limit-cost, e[i].c));
 60             if(tmp>0) {
 61                 e[i].c -= tmp;
 62                 e[i^1].c += tmp;
 63                 cost += tmp;
 64                 if(cost == limit)
 65                     break;
 66             } else {
 67                 dis[v] = -1;
 68             }
 69         }
 70     }
 71     return cost;
 72 }
 73 int dinic() {
 74     int ans = 0;
 75     while(bfs()) {
 76         ans += dfs(s, inf);
 77     }
 78     return ans;
 79 }
 80 char c[205];
 81 int main()
 82 {
 83     int n, f, d, x;
 84     while(cin>>n>>f>>d) {
 85         init();
 86         s = 0, t = f+2*n+d+1;
 87         for(int i = 1; i<=f; i++) {
 88             scanf("%d", &x);
 89             add(s, i, x);
 90         }
 91         for(int i = 1; i<=d; i++) {
 92             scanf("%d", &x);
 93             add(f+2*n+i, t, x);
 94         }
 95         for(int k = 0; k<2; k++) {
 96             for(int i = 1; i<=n; i++) {
 97                 scanf("%s", c);
 98                 int len = strlen(c);
 99                 for(int j = 0; j<len; j++) {
100                     if(c[j]==‘Y‘) {
101                         if(k==0) {
102                             add(j+1, f+i, 1);
103                         } else {
104                             add(f+n+i, j+1+f+2*n, 1);
105                         }
106                     }
107                 }
108             }
109         }
110         for(int i = 1; i<=n; i++) {
111             add(i+f, i+n+f, 1);
112         }
113         int ans = dinic();
114         cout<<ans<<endl;
115     }
116 }
时间: 2024-12-25 06:42:08

hdu 4292 Food 网络流的相关文章

hdu 4292 网络最大流

http://acm.hdu.edu.cn/showproblem.php?pid=4292 Problem Description You, a part-time dining service worker in your college's dining hall, are now confused with a new problem: serve as many people as possible. The issue comes up as people in your colle

hdu 4292 Food 最大流+拆点

Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2664    Accepted Submission(s): 899 Problem Description You, a part-time dining service worker in your college's dining hall, are now confus

HDU 3667 Transportation(网络流之费用流)

题目地址:HDU 3667 这题的建图真是巧妙...为了保证流量正好达到k,需要让每一次增广到的流量都是1,这就需要把每一条边的流量都是1才行.但是每条边的流量并不是1,该怎么办呢.这个时候可以拆边,反正c最多只有5,拆成5条流量为1的边.但是这时候费用怎么办呢,毕竟平方的关系不能简单把每一条边加起来.这时候可以把拆的边的流量设为1,3,5,7,9.如果经过了3个流量,那就肯定会流1,3,5,费用为9,是3的平方,同理,其他的也是如此.然后按照给出的边建图跑一次费用流就可以了. 代码如下: #i

hdu 4292 Food (最大流)

hdu 4292 Food Description You, a part-time dining service worker in your college's dining hall, are now confused with a new problem: serve as many people as possible. The issue comes up as people in your college are more and more difficult to serve w

HDU 4292 Food(建图+最大流)

使用 PVRTC 压缩格式创建纹理(Creating textures in the PVRTC compression format) 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 有关该篇

hdu 4292 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4296 Problem Description Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building. T

hdu 4292 Food【拆点网络流】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4292 解法:拆人 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <functional> #include <itera

Food (hdu 4292 网络流sap模板题)

Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3102    Accepted Submission(s): 1034 Problem Description You, a part-time dining service worker in your college's dining hall, are now confu

hdu 4292 Food 最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4292 You, a part-time dining service worker in your college’s dining hall, are now confused with a new problem: serve as many people as possible. The issue comes up as people in your college are more and