Time Limit: 15000MS | Memory Limit: 131072K | |
Total Submissions: 18120 | Accepted: 7818 | |
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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; const int MAX_N = ;
const int INF = ( << );
int N,M;
struct Edge{ int from, to, cap, flow; };
vector<Edge> edges;
int cur[MAX_N],d[MAX_N];
vector<int> G[MAX_N];
bool vis[MAX_N]; void add_edge(int from, int to, int cap) {
edges.push_back(Edge {from, to, cap, });
edges.push_back(Edge {to, from, , });
int m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - );
} bool bfs(int s,int t) { memset(vis,,sizeof(vis));
queue<int> q;
q.push(s);
d[s] = ;
vis[s] = ;
while (!q.empty()) {
int x = q.front(); q.pop();
for (int i = ; i < G[x].size(); ++i) {
Edge& e = edges[ G[x][i] ];
if (!vis[e.to] && e.cap > e.flow) {
d[e.to] = d[x] + ;
vis[e.to] = ;
q.push(e.to);
}
}
} return vis[t];
} int dfs(int x, int a, int t) {
if(x == t || a == ) return a;
int flow = ,f;
for(int& i = cur[x]; i < G[x].size(); ++i) {
Edge& e = edges[ G[x][i] ];
if(d[x] + == d[e.to] && (f = dfs(e.to,min(a,e.cap - e.flow),t)) > ) {
e.flow += f;
edges[ G[x][i] ^ ].flow -= f;
a -= f;
flow += f;
if(a == ) break; } } return flow;
} int Maxflow(int s, int t) { int flow = ;
while (bfs(s,t)) {
//printf("fuck\n");
memset(cur,,sizeof(cur));
flow += dfs(s, INF, t);
}
return flow;
}
int main()
{
//freopen("sw.in","r",stdin);
scanf("%d%d", &N, &M);
for (int i = ; i <= N; ++i) {
int a,b;
scanf("%d%d", &a, &b);
add_edge(,i,a);
add_edge(i,N + ,b);
} for (int i = ; i <= M; ++i) {
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
add_edge(a,b,w);
add_edge(b,a,w);
} printf("%d\n",Maxflow(,N + ));
//cout << "Hello world!" << endl;
return ;
}