Dual Core CPU
Time Limit: 15000MS | Memory Limit: 131072K | |
Total Submissions: 25099 | Accepted: 10866 | |
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<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=20080; int head[N],tot,S,T; int q[N],dis[N],n,m; int mp[N][22]; struct node { int next,v,w; } e[N*100]; void add(int u,int v,int w) { e[tot].v=v; e[tot].w=w; e[tot].next=head[u]; head[u]=tot++; } bool bfs() { memset(dis,-1,sizeof(dis)); dis[S]=0; int l=0,r=0; q[r++]=S; while(l<r) { int u=q[l++]; for(int i=head[u]; ~i; i=e[i].next) { int v=e[i].v; if(dis[v]==-1&&e[i].w>0) { q[r++]=v; dis[v]=dis[u]+1; if(v==T) return true; } } } return false; } int dfs(int s,int low) { if(s==T||!low) return low; int ans=low,a; for(int i=head[s]; ~i; i=e[i].next) { if(e[i].w>0&&dis[e[i].v]==dis[s]+1&&(a=dfs(e[i].v,min(e[i].w,ans)))) { e[i].w-=a; e[i^1].w+=a; ans-=a; if(!ans) return low; } } if(low==ans) dis[s]=-1; return low-ans; } int main() { scanf("%d%d",&n,&m);S=0,T=n+1; memset(head,-1,sizeof(head)); tot=0; int x,y,u,v,w; for(int i=1;i<=n;++i) { scanf("%d%d",&x,&y); add(S,i,x);add(i,S,0); add(i,T,y);add(T,i,0); } for(int i=1;i<=m;++i) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } int ans=0; while(bfs()) ans+=dfs(S,99999999); printf("%d\n",ans); }