您的位置:首页 > 其它

POJ 3469 Dual Core CPU (最小割)

2017-08-28 17:04 441 查看
Dual Core CPU

Time Limit: 15000MS Memory Limit: 131072K
Total Submissions: 25064 Accepted: 10860
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 Aiand 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
题意:

每个任务可以在A里完成,也可以在B里完成,分别给你cost。

另外有m个限制,如果a和b不再同一个系统里完成,就要额外付出一个cost。

求最少的cost。

POINT:

网络流最难的就是建模。

每个任务在A里完成==s和任务建一个有向边。

B里完成==任务和t建一个有向边。

ab有要求,就a和b建一个无向边。

例如样例。



割就是在这些路里分割,把s和t分割掉,这个就不做解释了。

然后割有很多,就想里面各种颜色的割。我们选择容量最小的最小割。

最大流=最小割。即紫色的那条。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define LL long long
const int maxn = 20010;
const int inf = 0x3f3f3f3f;
int n,m,t,s;
struct node
{
int from,to,flow,cap;
node(int u,int v,int f,int c):
from(u),to(v),flow(f),cap(c){}
};
vector<int>edge[maxn];
vector<node>len;
void add(int u,int v,int cap)
{
len.push_back(node(u,v,0,cap));
len.push_back(node(v,u,0,0));
int now=len.size();
edge[u].push_back(now-2);
edge[v].push_back(now-1);
}
int cur[maxn];
int d[maxn],vis[maxn];
bool bfs()
{
queue<int>q;
memset(vis,0,sizeof vis);
memset(d,0,sizeof d);
q.push(0);
d[0]=1;
vis[0]=1;
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=0;i<edge[u].size();i++)
{
node e=len[edge[u][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
d[e.to]=d[u]+1;
vis[e.to]=1;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int u,int a)
{
if(u==t||a==0) return a;
int flow=0,f;
for(int &i = cur[u];i<edge[u].size();i++)
{
node &e = len[edge[u][i]];
if(d[u]==d[e.to]-1&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
flow+=f;
e.flow+=f;
len[edge[u][i]^1].flow-=f;
a-=f;
}
if(a==0) break;
}
if(flow==0) d[u]=-1;
return flow;
}
int maxflow()
{
int ans=0;
while(bfs())
{
memset(cur,0,sizeof cur);
ans+=dfs(s,inf);
}
return ans;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=1;i<=n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
add(0,i,a);
add(i,n+1,b);
}
for(int i=1;i<=m;i++)
{
int u,v,cap;
scanf("%d %d %d",&u,&v,&cap);
add(u,v,cap);
add(v,u,cap);
}
s=0;
t=n+1;
printf("%d\n",maxflow());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: