Test instructions
There are n modules running on a and B-core dual-core computer, each module is known for running time on a A, a, a, and M triples (A,B,W), indicating that A and B modules are not running on a core to generate the additional cost of W, the total minimum costs.
Analysis:
That is, the N modules are divided into two sets, which can be solved by the method of minimum cut.
Code:
POJ 3469//sep9#include <iostream> #include <queue> #include <algorithm>using namespace Std;const int Maxn=20020;const int maxm=500020;struct edge{int v,f,nxt;} e[maxm*2+10];queue<int> que;int src,sink;int g[maxn+10];int nume;bool vis[maxn+10];int dist[maxN+10];int n,m; int A[maxn],b[maxn];int A[MAXM],B[MAXM],W[MAXM]; void Addedge (int u,int v,int c) {e[++nume].v=v;e[nume].f=c;e[nume].nxt=g[u];g[u]=nume;e[++nume].v=u;e[nume].f=0;e[ Nume].nxt=g[v];g[v]=nume;} void Init () {memset (g,0,sizeof (g)); nume=1;} int BFs () {while (!que.empty ()) Que.pop (); memset (dist,0,sizeof (Dist)); memset (vis,0,sizeof (Vis)); vis[src]=true; Que.push (SRC), while (!que.empty ()) {int U=que.front (), Que.pop (), for (int i=g[u];i;i=e[i].nxt) if (e[i].f&&! VIS[E[I].V]) {Que.push (E[I].V);d ist[e[i].v]=dist[u]+1;vis[e[i].v]=true; if (e[i].v==sink) return 1;}} return 0;} int dfs (int u,int delta) {if (U==sink) return delta;int ret=0;for (int i=g[u];ret<delta&&i;i=e[i].nxt) if (E[i] . f&&dist[e[i].v]==dist[u]+1){int Dd=dfs (e[i].v,min (E[i].f,delta-ret)); if (dd>0) {e[i].f-=dd;e[i^1].f+=dd;ret+=dd;} Elsedist[e[i].v]=-1;} return ret;} int dinic () {int ret=0;while (BFS () ==1) Ret+=dfs (Src,int_max); return ret;} int main () {scanf ("%d%d", &n,&m), int i,j;for (i=0;i<n;++i) scanf ("%d%d", &a[i],&b[i]); for (i=0;i <m;++i) scanf ("%d%d%d", &a[i],&b[i],&w[i]); Src=n,sink=n+1;init (); for (i=0;i<n;++i) {Addedge (I, Sink,a[i]); Addedge (Src,i,b[i]);} for (i=0;i<m;++i) {Addedge (a[i]-1,b[i]-1,w[i]); Addedge (B[i]-1,a[i]-1,w[i]);} printf ("%d", Dinic ()); return 0;}
POJ 3469 Dual Core CPU min cut