Dual Core CPU (poj 3469 min. Cut solution)

Source: Internet
Author: User

Language:DefaultDual Core CPU
Time Limit: 15000MS Memory Limit: 131072K
Total Submissions: 19820 Accepted: 8601
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 is so if module a and module b don ' t execute on the same core, you should pay extra w dollars for the data-exchange between them.

Output

Output only one integer, the minimum total cost.

Sample Input

3 11 102 1010 32 3 1000

Sample Output

13

Source

POJ monthly--2007.11.25, Zhou Dong

Test instructionsnow there are n modules, two CPUs A and B, each module running on a, or running on B, giving each module the cost required to run on A and B machines. Then M-line, a,b,w three numbers per line. Indicates that if the A module and the B module are not running on the same machine, you will need to spend extra w to share the data. The minimum cost of running all tasks is now required.

Idea: Treat two CPUs as source and sink, consume AI and bi in each CPU for module I, connect a arc of capacity to vertex I from source point to node, connect an arc of capacity to meeting point from vertex I to sink, and consume W for a module and B module running on different CPU. A bidirectional edge with a capacity of W from Vertex A to B. The maximum flow is obtained by minimizing the cut.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 1005#define MAXN 20005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FRL (i,a,b) for (i = A; I < b; i++) #define Mem (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, & AMP;B) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n" ) const int MAXM = 480010;typedef Long Long ll;using namespace std;struct edge{int to,next,cap,flow;} Edge[maxm];int n,m,s,t;int tol;int head[maxn];int GAP[MAXN],DEP[MAXN],Pre[maxn],cur[maxn];void init () {tol=0; memset (head,-1,sizeof (Head));}     Add edge, one-way figure three parameters, bidirectional figure four parameters void Addedge (int u,int v,int w,int rw=0) {edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u]; edge[tol].flow=0;    head[u]=tol++; Edge[tol].to=u; EDGE[TOL].CAP=RW;    EDGE[TOL].NEXT=HEAD[V]; edge[tol].flow=0; head[v]=tol++;}    Input parameters: Starting point, end point, number of points//point number has no effect, as long as the total number of input points int sap (int start,int end,int N) {memset (gap,0,sizeof (GAP));    memset (dep,0,sizeof (DEP));    memcpy (cur,head,sizeof (head));    int U=start;    Pre[u]=-1;    Gap[0]=n;    int ans=0;            while (dep[start]<n) {if (u==end) {int min=inf; for (int i=pre[u];i!=-1;i=pre[edge[i^1].to]) if (min>edge[i].cap-edge[i].flow) min=ed            Ge[i].cap-edge[i].flow;                for (int i=pre[u];i!=-1;i=pre[edge[i^1].to]) {edge[i].flow+=min;            Edge[i^1].flow-=min;            } U=start;            Ans+=min; ConTinue;        } bool Flag=false;        int V;            for (int i=cur[u];i!=-1;i=edge[i].next) {v=edge[i].to;                if (Edge[i].cap-edge[i].flow && dep[v]+1==dep[u]) {flag=true;                Cur[u]=pre[v]=i;            Break            }} if (flag) {u=v;        Continue        } int min=n;            for (int i=head[u];i!=-1;i=edge[i].next) if (Edge[i].cap-edge[i].flow && dep[edge[i].to]<min)                {min=dep[edge[i].to];            Cur[u]=i;        } gap[dep[u]]--;        if (!gap[dep[u]]) return ans;        dep[u]=min+1;        gap[dep[u]]++;    if (U!=start) u=edge[pre[u]^1].to; } return ans;    int main () {int i,j;        while (~SFF (n,m)) {int a,b,c;        Init ();        s=0;t=n+1;            FRE (i,1,n) {SFF (A, b);            Addedge (S,i,a);        Addedge (I,T,B);      }  FRE (i,1,m) {sfff (a,b,c);        Addedge (A,B,C,C);    } printf ("%d\n", SAP (S,T,T+1)); } return 0;} /*3 11 102 1010 32 3 1000*/



Dual Core CPU (poj 3469 min. Cut solution)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.