1. 程式人生 > >B - Dining POJ - 3281 網絡流

B - Dining POJ - 3281 網絡流

med use single icu 記錄 while sat sample 網絡流

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: N, F, 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;
}

B - Dining POJ - 3281 網絡流