POJ 1149, maximum stream, poj1149
Note:
There are M pig rings, and each pig contains several pig initially. All pigsty links are closed at the beginning. There are N Customers in sequence, and each customer opens the specified pigsty and buys several pig. Each customer has an upper limit on the quantity he can buy. After each customer leaves, the pig in the pigsty he opens can be transferred to any other open pigsty, and all pig circles are closed again. Ask how many pigs can be sold in total. (1 <= N <= 100, 1 <= M <= 1000)
Rules for constructing this network model:
• Each customer is represented by a node.
• For the first customer in each pigsty, the size is the initial number of pig in the pigsty by connecting an edge from the source point to him. If there are multiple edges from the source point to a customer, they can be merged into one and the capacity is added.
• For each pigsty, assume that n Customers have opened it, then for All integers I, [1, n ), from the customer I of the pigsty to the customer I + 1, the capacity is ∞.
• Each customer has its own edge to the settlement point, and the capacity is the maximum number of items that each customer can buy.
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>#include <vector>using namespace std;const int maxn = 1000 + 50;const int INF = 1e9;int n, m;struct Edge{int from, to, cap, flow;};struct Dinic{int n, m, s, t;vector<Edge> edges;vector<int> G[maxn];bool vis[maxn];int d[maxn];int cur[maxn];void init(int n){this->n = n;for(int i=0; i<=n; ++i) G[i].clear();edges.clear();}void AddEdge(int from, int to, int cap){edges.push_back((Edge){from, to, cap, 0});edges.push_back((Edge){to, from, 0, 0});m = edges.size();G[from].push_back(m-2);G[to].push_back(m-1);}bool BFS(){memset(vis, 0, sizeof vis );queue<int> Q;Q.push(s);vis[s] = 1;d[s] = 0;while(!Q.empty()){int x = Q.front(); Q.pop();for(int i=0; i<G[x].size(); ++i){Edge& e = edges[G[x][i]];if(!vis[e.to] && e.cap > e.flow){vis[e.to] = 1;d[e.to] = d[x] + 1;Q.push(e.to);}}}return vis[t];}int DFS(int x, int a){if(x==t || a==0) return a;int flow = 0, f;for(int& i=cur[x]; i<G[x].size(); ++i){Edge& e = edges[G[x][i]];if(d[x]+1==d[e.to] && (f=DFS(e.to, min(a, e.cap-e.flow)))>0){e.flow += f;edges[G[x][i]^1].flow -= f;flow += f;a -= f;if(a==0) break;}}return flow;}int MaxFlow(int s, int t){this->s = s; this->t = t;int flow = 0;while(BFS()){memset(cur, 0, sizeof cur );flow += DFS(s, INF);}return flow;}};Dinic gao;int pig[maxn], pre[maxn];void fk(){int A, B, K;int s = 0, t = n + 1; gao.init(t+2);memset(pre, 0, sizeof pre );for(int i=1; i<=m; ++i){scanf("%d", &pig[i]);}for(int i=1; i<=n; ++i){scanf("%d", &A);for(int j=1; j<=A; ++j){scanf("%d", &K);if(!pre[K]){gao.AddEdge(s, i, pig[K]);}else {gao.AddEdge(pre[K], i, INF);}pre[K] = i;}scanf("%d", &B);gao.AddEdge(i, t, B);}int ans = gao.MaxFlow(s, t);printf("%d\n", ans);}int main(){//freopen("in.txt", "r",stdin);while(~scanf("%d%d", &m, &n)){fk();}return 0;}
Who will use the adjacent matrix for Poj3204 (maximum stream plus enumeration) (If yes, it will add a high score)
I have just tried the right corner of the adjacent matrix.
Because the simple network flow is three-party
Just change it to an adjacent table.
In addition, the network stream basically does not use the adjacent matrix. sap templates are used. Otherwise, the data will be absolutely suspended.
# Include <iostream>
Using namespace std;
Const int maxn = 510;
Const int maxm = 5010;
Const int maxl = 99999999;
Int a [maxm], c [maxn];
Int f [maxn] [maxn], g [maxn] [maxn];
Int l [maxn] [maxn];
Int r [maxn];
Int c1 [maxn], c2 [maxn];
Int m, n;
Int e;
Void iin ()
{
Int I, j, k, s;
Int c [maxm];
Scanf ("% d", & n, & m );
Memset (f, 0, sizeof (f ));
Memset (g, 0, sizeof (g ));
Memset (c, 0, sizeof (c ));
Memset (r, 0, sizeof (r ));
For (I = 0; I <m; I ++)
{
Scanf ("% d", & j, & k, & s );
L [j] [r [j] ++] = k;
L [k] [r [k] ++] = j;
F [j] [k] + = s;
G [j] [k] + = s;
}
E = 1;
For (I = 0; I <n; I ++)
While (e <f [0] [I])
E * = 2;
}
Int min (int I, int j)
{
If (I> j) return j; else return I;
}
Int dfs (int k, int p, int * c, int final)
{
Int I, j, t;
If (k = final) return p;
C [k] = 1;
For (t = 0; t <r [k]; t ++)
{
I = l [k] [t];
If (! C [I] & f [k] [I]> = e)
If (j = dfs (I, min (p, f [k] [I]), c, final ))
{
F [k] [I]-= j;
F [I] [k] + = j;
Return j;
}
}
Return 0;
}
Void maxflow ()
{
Int tot = 0, I, j;
While (e> 0)
{
Memset (c, 0, sizeof (c ));
While (I = dfs (0, maxl, c, n-1 ))
{
Tot + = I;
Memset (c, 0, sizeof (c ));
}
E/= 2;
}
E = 1;
Memset (c1, 0, sizeof (c1 ));
Memset (c2, 0, sizeof (c2 ));
Dfs (0, maxl, c1, n-1 );
For (I = 0; I <n; I ++) for (j = I + 1; j <n; j ++) {int code = f [I] [j]; f [I] [j] = f [j] [I]; f [j] [I] = code ;}
Dfs (n-1, maxl, c2, 0 );
Tot = 0;
For (I = 0; I <n; I ++) for (j = I + 1; j <n; j ++) {int code = f [I] [j]; f [I] [j] = f [j] [I]; f [j] [I] = code ;}
For (I = 0; I <n; I ++) for (j = 0; j & l ...... the remaining full text>
Questions about binary tree traversal on POJ
Minimum Spanning Tree Algorithm (prim, kruskal) (poj1789, poj2485, poj1258, poj3026)
Trie tree (static and dynamic) (poj2513)
Line Segment tree. (poj2528, poj2828, poj2777, poj2886, poj2750)
Static Binary Search Tree (poj2482, poj2352)
TREE tree group (poj1195, poj3321)
Theory of the maximum short circuit, Minimum Spanning Tree, bipartite graph, and maximum flow problem (mainly modeling and solving)
(Poj3155, poj2112, poj1966, poj3281, poj1087, poj2289, poj3216, poj2446)