POJ 3469 最小割

来源:互联网 发布:淘宝宁夏中宁枸杞 编辑:程序博客网 时间:2024/05/17 08:15
Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072KTotal Submissions: 17675 Accepted: 7631Case 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 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 extraw dollars for the data-exchange between them.

Output

Output only one integer, the minimum total cost.

Sample Input

3 11 102 1010 32 3 1000

Sample Output

13

题意:给定n个部件,在A,B两个内核上运行的费用给定,假如某两个部件不在同一个内核上运行,需要额外支付一定钱,求最小花费,

对于每一个部件,从源点向其连边,流量为在a上运行的费用,从其向汇点连边流量为在b上运行的时间,给定的关系的加双向边,

根据最小割最大流定理,最小割等于最大流,求出来等于答案。

代码:

/* ***********************************************Author :rabbitCreated Time :2014/3/8 22:19:12File Name :treap2.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 10000000000000LL#define eps 1e-8#define pi acos(-1.0)typedef int ll;const ll maxn=31909;const ll maxm=4000010;struct Edge{    ll to,next,cap,flow;    Edge(){};    Edge(ll _next,ll _to,ll _cap,ll _flow){        next=_next;to=_to;cap=_cap;flow=_flow;    }}edge[maxm];ll head[maxn],tol,gap[maxn],dep[maxn],cur[maxn];void addedge(ll u,ll v,ll flow){    edge[tol]=Edge(head[u],v,flow,0);head[u]=tol++;    edge[tol]=Edge(head[v],u,0,0);head[v]=tol++;}ll Q[maxn];void bfs(ll start,ll end){    memset(dep,-1,sizeof(dep));    memset(gap,0,sizeof(gap));    gap[0]++;ll front=0,rear=0;    dep[end]=0;Q[rear++]=end;    while(front!=rear){        ll u=Q[front++];        for(ll i=head[u];i!=-1;i=edge[i].next){            ll v=edge[i].to;if(dep[v]==-1&&edge[i].cap)                Q[rear++]=v,dep[v]=dep[u]+1,gap[dep[v]]++;        }    }}ll S[maxn];ll sap(ll start,ll end,ll N){    bfs(start,end);    memcpy(cur,head,sizeof(head));    ll top=0,u=start,ans=0;    while(dep[start]<N){        if(u==end){            ll MIN=INF,id;            for(ll i=0;i<top;i++)                if(MIN>edge[S[i]].cap-edge[S[i]].flow)                    MIN=edge[S[i]].cap-edge[S[i]].flow,id=i;            for(ll i=0;i<top;i++)                edge[S[i]].flow+=MIN,edge[S[i]^1].flow-=MIN;            ans+=MIN,top=id,u=edge[S[top]^1].to;            continue;        }        bool flag=0;ll v;        for(ll i=cur[u];i!=-1;i=edge[i].next){            v=edge[i].to;            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){                flag=1;cur[u]=i;break;            }        }        if(flag){            S[top++]=cur[u];u=v;continue;        }        ll MIN=N;        for(ll i=head[u];i!=-1;i=edge[i].next)            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<MIN)                MIN=dep[edge[i].to],cur[u]=i;        if(--gap[dep[u]]==0)break;gap[dep[u]=MIN+1]++;        if(u!=start)u=edge[S[--top]^1].to;    }    return ans;}int main(){     //freopen("data.in","r",stdin);     //freopen("data.out","w",stdout);     int n,m; while(~scanf("%d%d",&n,&m)){ memset(head,-1,sizeof(head));tol=0; for(int i=1;i<=n;i++){ int u,v; scanf("%d%d",&u,&v); addedge(0,i,u); addedge(i,n+1,v); } while(m--){ int i,j,k; scanf("%d%d%d",&i,&j,&k); addedge(i,j,k); addedge(j,i,k); } printf("%d\n",sap(0,n+1,3*n+100)); }     return 0;}


0 0
原创粉丝点击