Time Limit: 15000MS | Memory Limit: 131072K | |
Total Submissions: 20366 | Accepted: 8824 | |
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<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #include<vector> #include<set> #include<stack> #include<climits> using namespace std; queue<int> q; int n,m,s,t; struct node { int v,len,next; }e[460000]; int head[46000],cnt=1,dist[46000]; int maxflow; void add(int u,int v,int len) { e[++cnt].v=v;e[cnt].len=len;e[cnt].next=head[u];head[u]=cnt; } void build() { for(int i=1;i<=n;i++) { int x,y; scanf("%d%d",&x,&y); add(s,i,x); add(i,t,y); } for(int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,z); } } bool bfs() { while(!q.empty()) q.pop(); memset(dist,0,sizeof(dist)); q.push(s),dist[s]=1; while(!q.empty()) { int u,v; u=q.front(),q.pop(); for(int i=head[u];i;i=e[i].next) { if(!dist[v=e[i].v]&&e[i].len) { dist[v]=dist[u]+1; if(v==t) return true; q.push(v); } } } return false; } int dinic(int x,int flow) { if(x==t) return flow; int remain=flow,v,k; for(int i=head[x];i&remain;i=e[i].next) { if(dist[v=e[i].v]==dist[x]+1&&e[i].len) { k=dinic(v,min(remain,e[i].len)); if(!k) dist[v]=0; e[i].len-=k,e[i^1].len+=k; remain-=k; } } return flow-remain; } int main() { scanf("%d%d",&n,&m); s=0,t=n+1; build(); while(bfs()) maxflow+=dinic(s,INT_MAX); printf("%d ",maxflow); return 0; }