POJ 3281 Dining(最大流)

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   for   sp   

POJ 3281 Dining

題目連結

題意:n個牛,每個牛有一些喜歡的食物和飲料,每種食物飲料只有一個,問最大能匹配上多少只牛每個牛都能吃上喜歡的食物和喜歡的飲料

思路:最大流,建模源點到每個食物連一條邊,容量為1,每個飲料向匯點連一條邊容量為1,然後由於每個牛有容量1,所以把牛進行拆點,然後食物連向牛的入點,牛的出點連向食物,跑一下最大流即可

代碼:

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int MAXNODE = 505;const int MAXEDGE = 100005;typedef int Type;const Type INF = 0x3f3f3f3f;struct Edge {int u, v;Type cap, flow;Edge() {}Edge(int u, int v, Type cap, Type flow) {this->u = u;this->v = v;this->cap = cap;this->flow = flow;}};struct Dinic {int n, m, s, t;Edge edges[MAXEDGE];int first[MAXNODE];int next[MAXEDGE];bool vis[MAXNODE];Type d[MAXNODE];int cur[MAXNODE];vector<int> cut;void init(int n) {this->n = n;memset(first, -1, sizeof(first));m = 0;}void add_Edge(int u, int v, Type cap) {edges[m] = Edge(u, v, cap, 0);next[m] = first[u];first[u] = m++;edges[m] = Edge(v, u, 0, 0);next[m] = first[v];first[v] = m++;}bool bfs() {memset(vis, false, sizeof(vis));queue<int> Q;Q.push(s);d[s] = 0;vis[s] = true;while (!Q.empty()) {int u = Q.front(); Q.pop();for (int i = first[u]; i != -1; i = next[i]) {Edge& e = edges[i];if (!vis[e.v] && e.cap > e.flow) {vis[e.v] = true;d[e.v] = d[u] + 1;Q.push(e.v);}}}return vis[t];}Type dfs(int u, Type a) {if (u == t || a == 0) return a;Type flow = 0, f;for (int &i = cur[u]; i != -1; i = next[i]) {Edge& e = edges[i];if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {e.flow += f;edges[i^1].flow -= f;flow += f;a -= f;if (a == 0) break;}}return flow;}Type Maxflow(int s, int t) {this->s = s; this->t = t;Type flow = 0;while (bfs()) {for (int i = 0; i < n; i++)cur[i] = first[i];flow += dfs(s, INF);}return flow;}void MinCut() {cut.clear();for (int i = 0; i < m; i += 2) {if (vis[edges[i].u] && !vis[edges[i].v])cut.push_back(i);}}} gao;int f, d, n;int main() {while (~scanf("%d%d%d", &n, &f, &d)) {int s = 0, t = f + d + n * 2 + 1;gao.init(t + 1);for (int i = 1; i <= f; i++)gao.add_Edge(s, i, 1);for (int i = 1; i <= d; i++)gao.add_Edge(f + i, t, 1);for (int i = 1; i <= n; i++) {gao.add_Edge(i + f + d, n + f + d + i, 1);int fn, dn;scanf("%d%d", &fn, &dn);int v;while (fn--) {scanf("%d", &v);gao.add_Edge(v, i + f + d, 1);}while (dn--) {scanf("%d", &v);gao.add_Edge(n + f + d + i, f + v, 1);}}printf("%d\n", gao.Maxflow(s, t));}return 0;}


POJ 3281 Dining(最大流)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.