Time Limit: 15000MS | Memory Limit: 131072K | |
Total Submissions: 25576 | Accepted: 11033 | |
Case Time Limit: 5000MS |
Description
As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Officer of TinySoft Corporation, decided to update their famous product - SWODNIW.
The routine consists of N modules, and each of them should run in a certain core. The costs for all the routines to execute on two 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 are running on the same core, then the cost of this action can be ignored. Otherwise, some extra cost are needed. You should arrange wisely to minimize the total cost.
Input
There are two integers in the first line of input data, N and M (1 ≤ N ≤ 20000, 1 ≤ M ≤ 200000) .
The next N lines, each contains two integer, Ai and Bi.
In the following M lines, each contains three integers: a, b, w. The meaning is that 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 1 1 10 2 10 10 3 2 3 1000
Sample Output
13
Source
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 5000010,inf = 0x7fffffff; int n,f,d,S,T,head[maxn],to[maxn],nextt[maxn],tot = 2,w[maxn],ans,m; int vis[maxn]; void add(int x,int y,int z) { w[tot] = z; to[tot] = y; nextt[tot] = head[x]; head[x] = tot++; w[tot] = 0; to[tot] = x; nextt[tot] = head[y]; head[y] = tot++; } bool bfs() { queue <int> q; memset(vis,-1,sizeof(vis)); vis[S] = 0; q.push(S); while (!q.empty()) { int u = q.front(); q.pop(); if (u == T) return true; for (int i = head[u];i;i = nextt[i]) { int v = to[i]; if (vis[v] == -1 && w[i]) { vis[v] = vis[u] + 1; q.push(v); } } } return false; } int dfs(int u,int f) { int res = 0; if (u == T) return f; for (int i = head[u];i;i = nextt[i]) { int v = to[i]; if (w[i] && vis[v] == vis[u] + 1) { int temp = dfs(v,min(f - res,w[i])); res += temp; w[i] -= temp; w[i ^ 1] += temp; if (res == f) return res; } } if (!res) vis[u] = -1; return res; } void dinic() { while (bfs()) ans += dfs(S,inf); } int main() { scanf("%d%d",&n,&m); S = 0; T = n + 1; for (int i = 1; i <= n; i++) { int a,b; scanf("%d%d",&a,&b); add(S,i,a); add(i,T,b); } for (int i = 1; i <= m; i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); add(a,b,c); add(b,a,c); } dinic(); printf("%d ",ans); return 0; }