Time Limit: 15000MS | Memory Limit: 131072K | |
Total Submissions: 14117 | Accepted: 6055 | |
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 asAi andBi. 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 extraw 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<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define INF 0xFFFFFF #define MAXN 25010 #define MAXM 1000000 struct node { int to,c; int next; }e[MAXM]; int n,m,en,dis[MAXN],gap[MAXN]; int st,ed; int g[MAXN]; void add(int a,int b,int c) { e[en].to=b; e[en].c=c; e[en].next=g[a]; g[a]=en++; e[en].to=a; e[en].c=0; e[en].next=g[b]; g[b]=en++; } int sap(int u,int flow) { if(u==ed) return flow; int ans=0,i,t; for(i=g[u];i!=-1;i=e[i].next) { if( e[i].c && dis[u]==dis[e[i].to]+1) { t=sap(e[i].to,min(flow-ans,e[i].c)); e[i].c-=t,e[i^1].c+=t,ans+=t; if(ans==flow) return ans; } } if(dis[st]>=n+2) return ans; if(!--gap[dis[u]]) //gap优化 dis[st]=n+2; ++gap[++dis[u]]; return ans; } void init() { int a,b,c; en=0; for(int i=0;i<=n+1;i++) { gap[i]=dis[i]=0; g[i]=-1; } for(int i=1;i<=n;i++) { scanf("%d%d",&a,&b); add(0,i,a); add(i,n+1,b); } for(int i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); add(a,b,c); add(b,a,c); } st=0,ed=n+1; } void solve() { int ans=0; for(gap[0]=n+2;dis[st]<n+2;) ans+=sap(st,INF); printf("%d\n",ans); } int main() { while(scanf("%d%d",&n,&m)!=EOF) { init(); solve(); } return 0; }