B - Dining POJ - 3281 网络流

Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.

Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.

Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).

Input

Line 1: Three space-separated integers: NF, and D 
Lines 2.. N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fiintegers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.

Output

Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes

Sample Input

4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3

Sample Output

3

Hint

One way to satisfy three cows is: 
Cow 1: no meal 
Cow 2: Food #2, Drink #2 
Cow 3: Food #1, Drink #1 
Cow 4: Food #3, Drink #3 
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.

牛吃草问题,这个牛是一个点,有因为每一头牛只能用一次,所以要进行拆点。

这个图很好建的,我就不说了。

#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <cstring>
#include <cmath>
#include <string>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 10;
struct edge
{
    int u, v, c, f, cost;
    edge(int u, int v, int c, int f, int cost) :u(u), v(v), c(c), f(f), cost(cost) {}
};
vector<edge>e;
vector<int>G[maxn];
int a[maxn];//找增广路每个点的水流量
int p[maxn];//每次找增广路反向记录路径
int d[maxn];//SPFA算法的最短路
int inq[maxn];//SPFA算法是否在队列中
int s, t;
void init()
{
    for (int i = 0; i <= maxn; i++)G[i].clear();
    e.clear();
}
void add(int u, int v, int c, int cost)
{
    e.push_back(edge(u, v, c, 0, cost));
    e.push_back(edge(v, u, 0, 0, -cost));
    int m = e.size();
    G[u].push_back(m - 2);
    G[v].push_back(m - 1);
}
bool bellman(int s, int t, int& flow, long long & cost)
{
    memset(d, inf, sizeof(d));
    memset(inq, 0, sizeof(inq));
    d[s] = 0; inq[s] = 1;//源点s的距离设为0,标记入队
    p[s] = 0; a[s] = INF;//源点流量为INF(和之前的最大流算法是一样的)

    queue<int>q;//Bellman算法和增广路算法同步进行,沿着最短路拓展增广路,得出的解一定是最小费用最大流
    q.push(s);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        inq[u] = 0;//入队列标记删除
        for (int i = 0; i < G[u].size(); i++)
        {
            edge & now = e[G[u][i]];
            int v = now.v;
            if (now.c > now.f && d[v] > d[u] + now.cost)
                //now.c > now.f表示这条路还未流满(和最大流一样)
                //d[v] > d[u] + e.cost Bellman 算法中边的松弛
            {
                d[v] = d[u] + now.cost;//Bellman 算法边的松弛
                p[v] = G[u][i];//反向记录边的编号
                a[v] = min(a[u], now.c - now.f);//到达v点的水量取决于边剩余的容量和u点的水量
                if (!inq[v]) { q.push(v); inq[v] = 1; }//Bellman 算法入队
            }
        }
    }
    if (d[t] == INF)return false;//找不到增广路
    flow += a[t];//最大流的值,此函数引用flow这个值,最后可以直接求出flow
    cost += (long long)d[t] * (long long)a[t];//距离乘上到达汇点的流量就是费用
    for (int u = t; u != s; u = e[p[u]].u)//逆向存边
    {
        e[p[u]].f += a[t];//正向边加上流量
        e[p[u] ^ 1].f -= a[t];//反向边减去流量 (和增广路算法一样)
    }
    return true;
}
int MincostMaxflow(int s, int t, long long & cost)
{
    cost = 0;
    int flow = 0;
    while (bellman(s, t, flow, cost));//由于Bellman函数用的是引用,所以只要一直调用就可以求出flow和cost
    return flow;//返回最大流,cost引用可以直接返回最小费用
}
struct node
{
    int x, y;
    node(int x=0,int y=0):x(x),y(y){}
};
node peo[110], house[110];
char mp[110][110];
int main()
{
    int n, m;
    while(cin>>n>>m)
    {
        init();
        int cas = 0, tot = 0;
        if (n == 0 && m == 0) break;
        for (int i = 1; i <= n; i++)
        {
            cin >> mp[i] + 1;
            for(int j=1;j<=m;j++)
            {
                if (mp[i][j] == ‘m‘) peo[++cas] = node(i, j);
                if (mp[i][j] == ‘H‘) house[++tot] = node(i, j);
            }
        }
        s = 0, t = cas + tot + 1;
        for (int i = 1; i <= cas; i++) add(s, i, 1, 0);
        for (int i = 1; i <= tot; i++) add(cas + i, t, 1, 0);
        for(int i=1;i<=cas;i++)
        {
            for(int j=1;j<=tot;j++)
            {
                int cost = abs(peo[i].x - house[j].x) + abs(peo[i].y - house[j].y);
                add(i, j + cas, 1, cost);
            }
        }
        ll cost = 0;
        int ans = MincostMaxflow(s, t, cost);
        printf("%lld\n", cost);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/EchoZQN/p/10798003.html

时间: 2024-11-01 22:12:07

B - Dining POJ - 3281 网络流的相关文章

POJ 3281 网络流dinic算法

B - Dining Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3281 Appoint description: Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she wil

POJ 3281 网络流 拆点保证本身只匹配一对食物和饮料

如何建图? 最开始的问题就是,怎么表示一只牛有了食物和饮料呢? 后来发现可以先将食物与牛匹配,牛再去和饮料匹配,实际上这就构成了三个层次. 起点到食物层边的容量是1,食物层到奶牛层容量是1,奶牛层到饮料层容量是1,饮料层到终点容量是1. 但是后来发现有一组hack数据: 2 3 3 3 3 1 2 3 1 2 3 3 3 1 2 3 1 2 3 我们发现一头奶牛居然吃了多个套餐,所以要解决这个只需要将自己与自己建立一条容量是1的边就行了. #include <cstdio> #include

POJ 3281 网络流 拆点 Dining

题意: 有F种食物和D种饮料,每头牛有各自喜欢的食物和饮料,而且每种食物或者饮料只能给一头牛. 求最多能有多少头牛能同时得到它喜欢的食物或者饮料. 分析: 把每个牛拆点,中间连一条容量为1的边,保证一头牛不会被多个食物或者饮料分配. 然后把饮料和牛连边,食物和另外一边的牛连边,最后增加一个源点和汇点跑最大流. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <alg

kuangbin专题专题十一 网络流 Dining POJ - 3281

题目链接:https://vjudge.net/problem/POJ-3281 题目:有不同种类的食物和饮料,每种只有1个库存,有N头牛,每头牛喜欢某些食物和某些饮料,但是一头牛 只能吃一种食物和喝一种饮料,问怎么分配食物和饮料才能让最多数量的牛饱餐. 思路:容易想到  食物->牛->饮料的流,当然一个牛可以被多个饮料流到,需要把牛拆成入点和出点,入点和出点流量为1,这样可以保证牛只吃或者喝某种食物和饮料,别的都流是套路,除了牛的分点之间流量为1,别的连接设置成1或者INF都一样,因为有牛的

Dining POJ - 3281(最大流)

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25452   Accepted: 11183 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabul

AC日记——Dining poj 3281

[POJ-3281] 思路: 把牛拆点: s向食物连边,流量1: 饮料向t连边,流量1: 食物向牛1连边,流量1: 牛2向饮料连边,流量1: 最大流: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 2005 #define INF 0x7fffffff int n,f,

POJ 3281 网络流(dinic邻接矩阵、单路增广、多路增广)

思路:刚开始看题就想到怎么建图了,源点连向所有的食物,食物连牛,牛连饮料,饮料连汇点,所有的流量都是1.不过这样建图好后,WA了.原来是一头牛只能单一匹配一组食物和饮料,所以牛得拆点,牛之间得相连,流量为1,以保证单一匹配食物和饮料. 邻接矩阵dinic单路的代码: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #in

POJ 3281 Dining(网络最大流)

http://poj.org/problem?id=3281 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9121   Accepted: 4199 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

poj 3281 Dining(最大流)

poj 3281 Dining Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their prefer