Time Limit: 15000MS | Memory Limit: 131072K | |
Total Submissions: 18999 | Accepted: 8204 | |
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
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
const int mm=1000000+10;
const int mn=20000+10;
const int oo=1e9;
int node,s,t,edge,max_flow;
int ver[mm],cap[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
inline void init(int _node,int _s,int _t)
{
node=_node, s=_s, t=_t;
for(int i=0;i<node;++i)
head[i]=-1;
edge=max_flow=0;
}
inline void addedge(int u,int v,int c)
{
ver[edge]=v,cap[edge]=c,flow[edge]=0,next[edge]=head[u],head[u]=edge++;
ver[edge]=u,cap[edge]=0,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
int i,u,v,l,r=0;
for(i=0;i<node;++i) dis[i]=-1;
dis[ q[r++]=s ] = 0;
for(l=0;l<r;l++)
{
for(i=head[ u=q[l] ]; ~i ;i=next[i])
if(flow[i]<cap[i] && dis[ v=ver[i] ]<0)
{
dis[ q[r++]=v ]=dis[u]+1;
if(v==t) return 1;
}
}
return 0;
}
int Dinic_dfs(int u,int exp)
{
if(u==t) return exp;
for(int &i=work[u],v,temp; ~i ;i=next[i])
{
if(flow[i]<cap[i] && dis[ v=ver[i] ]==dis[u]+1 && ( temp=Dinic_dfs(v,min(exp,cap[i]-flow[i])) )>0)
{
flow[i]+=temp;
flow[i^1]-=temp;
return temp;
}
}
return 0;
}
int Dinic_flow()
{
int res,i;
while(Dinic_bfs())
{
for(i=0;i<node;++i) work[i]=head[i];
while( ( res=Dinic_dfs(s,INF) ) ) max_flow+=res;
}
return max_flow;
}
int main()
{
int n,m;
while(cin>>n>>m)
{
init(n+2,0,n+1);
int a,b,c;
FOR(i,1,n)
{
scanf("%d%d",&a,&b);
addedge(s,i,a);
addedge(i,t,b);
}
REP(i,m)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
cout<<Dinic_flow()<<endl;
}
return 0;
}