Title Address: Https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem &problem=5276
Idea: Minimum cut. Split each point into I and I ', set a source point and a sink point.
1. For Edge I--->j, the edge i '--->j, the capacity is the cost of the corresponding side.
2. The source point to I edge, the capacity to meet the conditions after the purchase of I cost.
3. For I and I ' edge, the capacity is the cost of direct purchase I.
4. For the target point to the meeting point edge, the capacity is INF.
Because each cut in the diagram is a scheme to acquire a target skill, the minimum cut is required.
#include <cstdio> #include <queue> #include <vector> #include <cstring> #include <algorithm > #define Debuusing namespace std;const int maxn = 1010;const int INF = 0x3f3f3f3f;struct edge{int from, to, Cap, FL ow Edge (int a,int b,int c,int D): From (a), to (b), Cap (c), flow (d) {}};int n,m,goal;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)); int 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; }};D inic G;int main () {#ifdef debug Freopen ("In.in", "R", stdin), #endif//debug int t; scanf ("%d", &t); while (t--) {scanf ("%d%d%d", &n,&m,&goal); G.init (2*n+1); for (int i=0; i<m; i++) {int x,y,c; scanf ("%d%d%d", &x,&y,&c); G.addedge (X+N,Y,C); } for (int i=1; i<=n; i++) {int x; scanf ("%d", &x); G.addedge (0,I,X); } for (int i=1; i<=n; i++) {int x; scanf ("%d", &x); G.addedge (I,I+N,X); } g.addedge (Goal,2*n+1,inf); int Ans=g.maxflow (0,2*n+1); printf ("%d\n", ans); } return 0;}
Uvalive 7264 Kejin Game (min cut)