您的位置:首页 > 其它

poj--3469--Dual Core CPU(最小割裸题)

2016-05-04 21:50 316 查看

Dual Core CPU

Time Limit: 15000MS Memory Limit: 131072KB 64bit IO Format: %I64d & %I64u
Submit Status

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


Source

POJ Monthly--2007.11.25, Zhou Dong

双核的电脑,两个核共由n个模块组成,每个模块放在1或者2都会产生一定的花费,并且有些模块如果在一个核中会产生额外的花费,问,将这n个模块放在一起需要的最少的话费

比较裸的最小割,两个核就是两个集合,直接套模板

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 20010
#define INF 0x3f3f3f3f
struct node
{
int u,v,cap,flow,next;
}edge[MAXN*100];
int vis[MAXN],dis[MAXN],n,m,head[MAXN],cnt,cur[MAXN];
void add(int a,int b,int c)
{
node E={a,b,c,0,head[a]};
edge[cnt]=E;
head[a]=cnt++;
node E1={b,a,0,0,head[b]};
edge[cnt]=E1;
head[b]=cnt++;
}
void getmap()
{
int a,b,c;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
add(0,i,a);
add(i,1+n,b);
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
}
bool bfs(int s,int e)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
while(!q.empty()) q.pop();
q.push(s);
dis[s]=0;
vis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(E.cap>E.flow&&!vis[E.v])
{
vis[E.v]=1;
dis[E.v]=dis[E.u]+1;
if(E.v==e) return true;
q.push(E.v);
}
}
}
return false;
}
int dfs(int x,int a,int e)
{
if(x==e||a==0)
return a;
int flow=0,f;
for(int &i=cur[x];i!=-1;i=edge[i].next)
{
node &E=edge[i];
if(dis[x]+1==dis[E.v]&&(f=dfs(E.v,min(E.cap-E.flow,a),e))>0)
{
E.flow+=f;
edge[i^1].flow-=f;
a-=f;
flow+=f;
if(a==0) break;
}
}
return flow;
}
int MAXflow(int s,int e)
{
int flow=0;
while(bfs(s,e))
{
memcpy(cur,head,sizeof(head));
flow+=dfs(s,INF,e);
}
return flow;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt=0;
memset(head,-1,sizeof(head));
getmap();
printf("%d\n",MAXflow(0,1+n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: