Dual Core CPU
Time limit:15000ms Memory limit:131072k
Total submissions:23776 accepted:10335
Case Time Limit:5000ms
Description
As more and more computers is equipped with dual core CPU, SETAGLILB, the chief technology Officer of Tinysoft Corporatio N, decided to update their famous PRODUCT-SWODNIW.
The routine consists of N modules, and each of the them should run in a certain core. The costs for all the routines-to-execute on both cores has been estimated. Let ' s define them as Ai and Bi. Meanwhile, M pairs of modules need to do some data-exchange. If They is running on the same core and then the cost of this action can be ignored. Otherwise, some extra cost is needed. You should arrange wisely to minimize.
Input
There is integers in the first line of input data, N and M (1≤n≤20000, 1≤m≤200000).
The next N lines, each contains the integer, Ai and Bi.
In the following M lines, each contains three integers:a, B, W. The meaning are that if module A and Module B don ' t execut E on the same core, your should pay extra w dollars for the data-exchange between them.
Output
Output only one integer, the minimum total cost.
Sample Input
3 1
1 10
2 10
10 3
2 3 1000
Sample Output
13
Analysis
Dinic Multi-Channel augmentation good (bare) problem
Code
POJ 3469 #include <iostream> #include <cstring> #include <cstdio> #include <queue> #define INF
1e9+7 #define FO (i,j,k) for (i=j;i<=k;i++) using namespace std;
const int mxn=200005;
int Cnt,n,m,ans,tans;
int HEAD[MXN],DIS[MXN];
Queue <int> q;
struct node {int next,to,flow;} f[mxn<<3];
inline void Add (int u,int v,int flow) {f[++cnt].to=v;
F[cnt].flow=flow;
F[cnt].next=head[u];
head[u]=cnt;
} inline bool BFs () {memset (dis,-1,sizeof dis);
Q.push (0);
dis[0]=0;
while (!q.empty ()) {int U=q.front ();
Q.pop ();
for (int i=head[u];i;i=f[i].next) {int v=f[i].to;
if (dis[v]<0 && f[i].flow>0) {Q.push (v);
dis[v]=dis[u]+1;
}}} if (dis[n+1]>0) return 1;
return 0;
} inline int find (int u,int now) {int a,v,i,sum=0;
if (u==n+1) return now;
for (I=head[u];i;i=f[i].next) { v=f[i].to;
if (now>sum && f[i].flow>0 && dis[v]==dis[u]+1 && (A=find (V,min (Now-sum,f[i].flow))))
{f[i].flow-=a;
if (i&1) f[i+1].flow+=a;
else f[i-1].flow+=a;
Sum+=a;
}} return sum;
} int main () {int i,j,u,v,w;
scanf ("%d%d", &n,&m);
Fo (i,1,n) {scanf ("%d%d", &u,&v);
Add (0,i,u), add (i,0,0);
Add (I,n+1,v), add (n+1,i,0);
} fo (i,1,m) {scanf ("%d%d%d", &u,&v,&w);
Add (U,v,w), add (V,U,W);
} while (BFS ()) while (Tans=find (0,inf)) Ans+=tans;
printf ("%d\n", ans);
return 0; }