POJ 1698 Alice's Chance (maximum stream + split point), poj1698
POJ 1698 Alice's Chance
Question Link
Meaning: To make n movies, each film should be completed in the first w weeks, and only a few days can be filmed in a week. Each movie has a total time required to complete the filming.
Idea: the source node connects to each movie and the capacity is d. Then, each movie corresponds to the edge that can be shot on that day. Because the daily capacity limit is 1, the node is split, then, connect to the sink.
Code:
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int MAXNODE = 1005;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 t, n, day[10], d, w, vis[400];int main() {scanf("%d", &t);while (t--) {memset(vis, 0, sizeof(vis));gao.init(1000);scanf("%d", &n);int sum = 0;for (int i = 1; i <= n; i++) {for (int j = 1; j <= 7; j++)scanf("%d", &day[j]);scanf("%d%d", &d, &w);sum += d;gao.add_Edge(0, i, d);for (int j = 0; j < w; j++) {for (int k = 1; k <= 7; k++) {if (day[k]) {gao.add_Edge(i, 20 + j * 7 + k, INF);vis[20 + j * 7 + k] = 1;}}}}for (int i = 21; i <= 370; i++) {if (!vis[i]) continue;gao.add_Edge(i, i + 350, 1);gao.add_Edge(i + 350, 1000, INF);}printf("%s\n", gao.Maxflow(0, 1000) == sum ? "Yes" : "No");}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>