zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    ASP.NET 设计模式 读书摘记1
    [Exception]Sys.WebForm.PageRequestManagerServerErrorException:500
    [Exception] 当前 TransactionScope 已完成
    [Javascript]客户端检测
    c.Tom and paper
    Currency System in Geraldion (Codeforces 560A)
    巡逻机器人(BFS)
    比赛建金字塔问题解题报告
    除法(暴力)
    比赛找丢失的数解题报告T
  • 原文地址:https://www.cnblogs.com/yohaha/p/5018465.html
Copyright © 2011-2022 走看看