日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

POJ 3281 -- Dining(最大流,拆点建图)

發布時間:2024/4/18 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 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 Fi integers 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

AC

  • 建圖思路:
    題目問的是最多有幾頭牛可以同時得到自己想要的食物和水,如果一頭牛可以同時可以得到兩對合適的食物和水,這樣就浪費了,所以要把牛拆開,牛和自己建一條權值為1的邊,這樣保證一頭牛如果可以的話只能得到一對食物和水源,從而使得越多的牛可以得到對應的食物和水

  • 源點和所有食物建立一條權值為1的邊
  • 食物和牛建立權值為1的邊
  • 牛的副本和對應的水建立權值為1的邊
  • 水和匯點建立權值為1的邊
  • 牛和牛的副本建立權值為1的邊
  • 建好圖之后,跑一遍最大流,得到的就是最多牛的個數

#include <iostream> #include <stdio.h> #include <map> #include <vector> #include <queue> #include <algorithm> #include <cmath> #define N 300005 #include <cstring> #define ll long long #define P pair<int, int> #define mk make_pair using namespace std;int a[405][405], pre[405]; bool vis[405]; int inf = 0x3f3f3f3f; int n, f, d; // EK模板 bool bfs(int s, int e) {memset(pre, -1, sizeof(pre));memset(vis, false, sizeof(vis));vis[s] = true;pre[s] = s;queue<int> que;que.push(s);while (!que.empty()) {int t = que.front();que.pop();for (int i = 0; i <= n + n + f + d + 1; ++i) {if (vis[i] || a[t][i] == 0) continue;vis[i] = true;pre[i] = t;if (i == e) return true;que.push(i);}}return false; }ll solve(int s, int e) {ll sum = 0;while (bfs(s, e)) {int MIN = inf;for (int i = e; i != s; i = pre[i]) {MIN = min(MIN, a[pre[i]][i]);}for (int i = e; i != s; i = pre[i]) {a[pre[i]][i] -= MIN;a[i][pre[i]] += MIN;} sum += MIN;}return sum; }int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifscanf("%d%d%d" ,&n, &f, &d);for (int i = 1; i <= n; ++i) {int l, r, t;scanf("%d%d",&l, &r);// food 和 cow 建邊 for (int j = 0; j < l; ++j) {scanf("%d", &t);a[t][f + i] = 1; }// cow 和 water 建邊 for (int j = 0; j < r; ++j) {scanf("%d", &t);a[f + i + n][t + f + n + n] = 1;}}// 源點 和 食物建邊 for (int i = 1; i <= f; ++i) {a[0][i] = 1;}// 水 和 匯點建邊 for (int i = 1; i <= d; ++i) {a[i + f + n + n][n + f + d + 1 + n] = 1;}// 牛和牛之間建邊 for (int i = 1; i <= n; ++i) {a[f + i][f + n + i] = 1;}// 跑一邊EK ll ans = solve(0, n + n + f + d + 1);printf("%d\n", ans);return 0; }

總結

以上是生活随笔為你收集整理的POJ 3281 -- Dining(最大流,拆点建图)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。