最小割经典题(两个点依附在一起的情况)poj3469
阅读原文时间:2023年07月10日阅读:2

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
#include
#include
using namespace std;
const int N=;
int head[N],tot,S,T;
int q[N],dis[N],n,m;
int mp[N][];
struct node
{
int next,v,w;
} e[N*];
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,-,sizeof(dis));
dis[S]=;
int l=,r=;
q[r++]=S;
while(l)
{
q[r++]=v;
dis[v]=dis[u]+;
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>&&dis[e[i].v]==dis[s]+&&(a=dfs(e[i].v,min(e[i].w,ans))))
{
e[i].w-=a;
e[i^].w+=a;
ans-=a;
if(!ans) return low;
}
}
if(low==ans) dis[s]=-;
return low-ans;
}
int main()
{
scanf("%d%d",&n,&m);S=,T=n+;
memset(head,-,sizeof(head));
tot=;
int x,y,u,v,w;
for(int i=;i<=n;++i) {
scanf("%d%d",&x,&y);
add(S,i,x);add(i,S,);
add(i,T,y);add(T,i,);
}
for(int i=;i<=m;++i) {
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
int ans=;
while(bfs()) ans+=dfs(S,);
printf("%d\n",ans);
}