Time Limit: 15000MS | Memory Limit: 131072K | |
Total Submissions: 23355 | Accepted: 10165 | |
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
题意:有一些模块(modules)和一个双核处理器,一个模块可以在任意一个核上处理,每个核对应每个模块有个开销。现在有一些模块间需要数据交换,如果需要数据交换的模块在一个核上处理,则不需要额外开销,否则需要加上一个开销。现在需要完成所有模块,问最小需要多少开销。
思路:建模很重要= =,很容易以为这题就是个贪心。。如果建图去做的话直接把每个模块当作一个点,起点到模块的权值是Ai,终点到模块权值是Bi,然后模块之间的连边正常连就行,最后最小割等于最大流,跑一遍最大流完事儿。
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<map>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<deque>
using namespace std;
typedef long long ll;
const int oo=1e9;/**oo 表示无穷大*/
const int mm=111111111;/**mm 表示边的最大数量,记住要是原图的两倍,在加边的时候都是双向的*/
const int mn=20010;/**mn 表示点的最大数量*/
int node,src,dest,edge;/**node 表示节点数,src 表示源点,dest 表示汇点,edge 统计边数*/
int ver[mm],flow[mm],nex[mm];
int head[mn],work[mn],dis[mn],q[mn];
void prepare(int _node, int _src,int _dest)
{
node=_node,src=_src,dest=_dest;
for(int i=0; i<=node; ++i)head[i]=-1;
edge=0;
}
void addedge( int u, int v, int c)
{
ver[edge]=v,flow[edge]=c,nex[edge]=head[u],head[u]=edge++;
ver[edge]=u,flow[edge]=0,nex[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
int i,u,v,l,r=0;
for(i=0; i<node; ++i)dis[i]=-1;
dis[q[r++]=src]=0;
for(l=0; l<r; ++l)
for(i=head[u=q[l]]; i>=0; i=nex[i])
if(flow[i]&&dis[v=ver[i]]<0)
{
dis[q[r++]=v]=dis[u]+1;
if(v==dest) return 1;
}
return 0;
}
int Dinic_dfs( int u, int exp)
{
if(u==dest) return exp;
for( int &i=work[u],v,tmp; i>=0; i=nex[i])
if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
{
flow[i]-=tmp;
flow[i^1]+=tmp;
return tmp;
}
return 0;
}
int Dinic_flow()
{
int i,ret=0,delta;
while(Dinic_bfs())
{
for(i=0; i<node; ++i)work[i]=head[i];
while((delta=Dinic_dfs(src,oo)))ret+=delta;
}
return ret;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
prepare(n+2,0,n+1);
for(int i=1; i<=n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
addedge(0,i,a);
addedge(i,n+1,b);
}
for(int i=0; i<m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
printf("%d\n",Dinic_flow());
return 0;
}