poj3469最大流(最大割)
2016-03-10 09:41
399 查看
Dual Core CPU
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 andBi. 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
Sample Output
13
题意:要在有核A核B组成的cup上运行N个模块。模块i在A上执行花费为Ai,在B上执行花费为Bi;在M个互相之间需要进行数据交换的模块组合(ai,bi),如果这两个模块在同一个核上执行则没有额外花费,否者会产生wi的花费,求执行所有模块需要的最小花费;;
最小费用将对象划分为两集合问题,可以将其转化为最小割问题;
最小割可以转化成最大流来实现
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
/**模板大法好*/
using namespace std;
const int MAXN=100110;
const int MAXM=4000110;
const int INF=0x3f3f3f3f;
struct Node
{
int to,next,cap;
}edge[MAXM];
int tol;int head[MAXN];
int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN];
void init()
{
tol=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw=0)
{
edge[tol].to=v;edge[tol].cap=w;edge[tol].next=head[u];head[u]=tol++;
edge[tol].to=u;edge[tol].cap=rw;edge[tol].next=head[v];head[v]=tol++;
}
int sap(int start,int end,int nodenum)
{
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
memcpy(cur,head,sizeof(head));
int u=pre[start]=start,maxflow=0,aug=-1;
gap[0]=nodenum;
while(dis[start]<nodenum)
{
loop:
for(int &i=cur[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap&&dis[u]==dis[v]+1)
{
if(aug==-1||aug>edge[i].cap)
aug=edge[i].cap;
pre[v]=u;
u=v;
if(v==end)
{
maxflow+=aug;
for(u=pre[u];v!=start;v=u,u=pre[u])
{
edge[cur[u]].cap-=aug;
edge[cur[u]^1].cap+=aug;
}
aug=-1;
}
goto loop;
}
}
int mindis=nodenum;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap&&mindis>dis[v])
{
cur[u]=i;
mindis=dis[v];
}
}
if((--gap[dis[u]])==0)break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return maxflow;
}
int main()
{
int n,m;
int S,T;
int a,b,w;
while(~scanf("%d%d",&n,&m))
{
init();
S=0;
T=n+1;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
addedge(S,i,a);
addedge(i,T,b);
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&w);
addedge(a,b,w);
addedge(b,a,w);
}
printf("%d\n",sap(S,T,T+1));
}
return 0;
}
Time Limit: 15000MS | Memory Limit: 131072K | |
Total Submissions: 22059 | Accepted: 9587 | |
Case Time Limit: 5000MS |
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 andBi. 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
题意:要在有核A核B组成的cup上运行N个模块。模块i在A上执行花费为Ai,在B上执行花费为Bi;在M个互相之间需要进行数据交换的模块组合(ai,bi),如果这两个模块在同一个核上执行则没有额外花费,否者会产生wi的花费,求执行所有模块需要的最小花费;;
最小费用将对象划分为两集合问题,可以将其转化为最小割问题;
最小割可以转化成最大流来实现
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
/**模板大法好*/
using namespace std;
const int MAXN=100110;
const int MAXM=4000110;
const int INF=0x3f3f3f3f;
struct Node
{
int to,next,cap;
}edge[MAXM];
int tol;int head[MAXN];
int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN];
void init()
{
tol=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw=0)
{
edge[tol].to=v;edge[tol].cap=w;edge[tol].next=head[u];head[u]=tol++;
edge[tol].to=u;edge[tol].cap=rw;edge[tol].next=head[v];head[v]=tol++;
}
int sap(int start,int end,int nodenum)
{
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
memcpy(cur,head,sizeof(head));
int u=pre[start]=start,maxflow=0,aug=-1;
gap[0]=nodenum;
while(dis[start]<nodenum)
{
loop:
for(int &i=cur[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap&&dis[u]==dis[v]+1)
{
if(aug==-1||aug>edge[i].cap)
aug=edge[i].cap;
pre[v]=u;
u=v;
if(v==end)
{
maxflow+=aug;
for(u=pre[u];v!=start;v=u,u=pre[u])
{
edge[cur[u]].cap-=aug;
edge[cur[u]^1].cap+=aug;
}
aug=-1;
}
goto loop;
}
}
int mindis=nodenum;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap&&mindis>dis[v])
{
cur[u]=i;
mindis=dis[v];
}
}
if((--gap[dis[u]])==0)break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return maxflow;
}
int main()
{
int n,m;
int S,T;
int a,b,w;
while(~scanf("%d%d",&n,&m))
{
init();
S=0;
T=n+1;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
addedge(S,i,a);
addedge(i,T,b);
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&w);
addedge(a,b,w);
addedge(b,a,w);
}
printf("%d\n",sap(S,T,T+1));
}
return 0;
}
相关文章推荐
- 分享截屏总结
- "诗词大闯关"调查过程心得体会
- Distributed Systems-leader based分布式一致算法
- Network学习4_CentOS系统下ifcfg-Auto_em1的正确配置方式
- 多态 基类于子类相互转换
- [LeetCode]153. Find Minimum in Rotated Sorted Array
- Linux学习笔记1:查看监听所有端口netstat -lntp
- C++上机报告 第一次实验-1
- 触发器与存储过程的区别
- Android 事件分发机制
- Android 应用层View绘制流程
- 不能不知道的CSS选择器技巧
- Android结束进程的方法详解
- 【JVM命令系列(2)】堆栈跟踪之Jstack命令
- 软件开发所需的13个文档
- 一个程序尽可能多的实现Java面向对象编程的概念
- 5 Things You Should Know About the New Maxwell GPU Architecture
- 修改VirtualBox虚拟机默认存储路径及虚拟机迁移方法
- Servlet 共享变量
- 2016.3.7__HTML 基础_第一天