Time Limit: 15000MS | Memory Limit: 131072K | |
Total Submissions: 23800 | Accepted: 10348 | |
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
最小割
S到每个进程连边,容量为费用A;
每个进程到T连边,容量为费用B;
有关联的进程之间连双向边,容量均为费用w
求最小割
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<queue> 8 using namespace std; 9 const int INF=1e9; 10 const int mxn=20020; 11 int read(){ 12 int x=0,f=1;char ch=getchar(); 13 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} 14 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} 15 return x*f; 16 } 17 struct edge{ 18 int v,nxt,f; 19 }e[mxn*50]; 20 int hd[mxn],mct=1; 21 void add_edge(int u,int v,int w){ 22 e[++mct].v=v;e[mct].nxt=hd[u];e[mct].f=w;hd[u]=mct;return; 23 } 24 void insert(int u,int v,int c){ 25 add_edge(u,v,c);add_edge(v,u,0);return; 26 } 27 int n,m,S,T; 28 int d[mxn]; 29 bool BFS(){ 30 queue<int>q; 31 memset(d,0,sizeof d); 32 q.push(S); 33 d[S]=1; 34 while(!q.empty()){ 35 int u=q.front();q.pop(); 36 for(int i=hd[u];i;i=e[i].nxt){ 37 int v=e[i].v; 38 if(!d[v] && e[i].f){ 39 d[v]=d[u]+1; 40 q.push(v); 41 } 42 } 43 } 44 return d[T]; 45 } 46 int DFS(int u,int lim){ 47 if(u==T)return lim; 48 int f=0,tmp; 49 for(int i=hd[u];i;i=e[i].nxt){ 50 int v=e[i].v; 51 if(d[v]==d[u]+1 && e[i].f && (tmp=DFS(v,min(lim,e[i].f)))){ 52 f+=tmp;lim-=tmp; 53 e[i].f-=tmp; 54 e[i^1].f+=tmp; 55 if(!lim)return f; 56 } 57 } 58 d[u]=0; 59 return f; 60 } 61 int Dinic(){ 62 int res=0; 63 while(BFS())res+=DFS(S,INF); 64 return res; 65 } 66 int main(){ 67 int i,j; 68 n=read();m=read(); 69 S=0;T=n+1; 70 int a,b,w; 71 for(i=1;i<=n;i++){ 72 a=read();b=read(); 73 insert(S,i,a);insert(i,T,b); 74 } 75 for(i=1;i<=m;i++){ 76 a=read();b=read();w=read(); 77 insert(a,b,w); 78 insert(b,a,w); 79 } 80 int ans=Dinic(); 81 printf("%d ",ans); 82 return 0; 83 }