Dual Core CPU
Time Limit: 15000MS |
|
Memory Limit: 131072K |
Total Submissions: 20852 |
|
Accepted: 9010 |
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
Title Description:
As more and more computers are configured with dual-core Cpu,tinysoft company's chief technical officer, SETAGLILB decided to upgrade their product-SWODNIW. The SWODNIW contains n modules, each of which must be run on a CPU. The cost of running each module on each CPU has been estimated and set to AI and bi. At the same time, M needs to share data between the modules, and if they are running on the same CPU, the cost of sharing the data is negligible, otherwise, additional costs will be required. You have to arrange the N modules well so that the total cost is minimized.
Idea: If two CPUs are treated as source and sink points, modules as vertices, you can compose as follows: For the first module in each CPU consumes AI and bi, from the source point to vertex I connect a capacity for the AI arc, from vertex i to sink point to connect a capacity for bi arc , for the additional cost of a module and the B module running on different CPUs, vertex A and vertex b connect an arc with a capacity of W, Vertex b joins the arc with Vertex A with a capacity of W . At this point each vertex (module) is connected to the source point and the sink point (two CPUs), i.e. each module can be run on either CPU.
For any cut in the graph, the source and sink points must not be connected. Therefore, each vertex (module) cannot be connected to both the source and the sink (two CPUs), i.e. each module runs only on the same CPU. At this time the cost is the cut capacity. It is clear that the total cost of the cut is minimal when the capacity is minimized. So the title is converted to the minimum cut capacity.
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAXN 21000# Define MAXM 1000000#define INF 0x3f3f3f3fusing namespace std;int N, m;int Head[maxn],cur[maxn], Cnt;int DIST[MAXN], Vis[ma xn];struct node{int u, V, cap, flow, next;}; Node Edge[maxm];void init () {cnt = 0; Memset (Head,-1, sizeof (head));} void Add (int u, int v, int w) {node E1 = {u, V, W, 0, Head[u]}; EDGE[CNT] = E1; Head[u] = cnt++; Node E2 = {V, u, 0, 0, Head[v]}; EDGE[CNT] = E2; HEAD[V] = cnt++;} void Getmap () {int A, B, w; for (int i = 1; I <= n; ++i) {scanf ("%d%d", &a, &b); Add (0, I, a); Add (i, n + 1, b); } for (int j = 1; J <=m; ++j) {scanf ("%d%d%d", &a, &b, &w); Add (A, B, W); Add (b, A, w); }}bool BFS (int st, int ed) {queue<int>q; memset (Vis, 0, sizeof (VIS)); memset (Dist,-1, sizeof (Dist)); VIS[ST] = 1; DIST[ST] = 0; Q.push (ST); while (!q.Empty ()) {int u = q.front (); Q.pop (); for (int i = head[u]; i =-1; i = Edge[i].next) {node E = Edge[i]; if (!VIS[E.V] && e.cap > E.flow) {vis[e.v] = 1; DIST[E.V] = Dist[u] + 1; if (e.v = = ed) return true; Q.push (E.V); }}} return false;} int DFS (int x, int ed, int a) {if (a = = 0 | | x = = ed) return A; int flow = 0, F; for (int &i = cur[x]; I! =-1; i = Edge[i].next) {node &e = Edge[i]; if (dist[e.v] = = Dist[x] + 1 && (f = DFS (e.v, ed, Min (A, e.cap-e.flow))) > 0) {e.flow + = f; edge[i ^ 1].flow-= f; Flow + + F; A-= f; if (a = = 0) break; }} return flow;} int Maxflow (int st, int ed) {int sumflow = 0; while (BFS (st,ed)) {memcpy (cur, head, sizeof (head)); Sumflow + = DFS (St, Ed, INF); } return sumflow;} int main () {WHILe (scanf ("%d%d", &n,&m)! = EOF) {init (); Getmap (); printf ("%d\n", Maxflow (0, n + 1)); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3469--dual Core CPU "min cut"