POJ 3469.Dual Core CPU 最大流dinic算法模板
Time Limit: 15000MS | Memory Limit: 131072K | |
Total Submissions: 24830 | Accepted: 10756 | |
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
Source
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define PI acos(-1.0)
typedef long long ll;
typedef pair<int,int> P;
const int maxn=1e5+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e18+;
priority_queue<P,vector<P>,greater<P> >que;
struct edge
{
int from,to;
int cap;
int rev;///方向边的编号
};
vector<edge>G[maxn];///邻接表存图
int iter[maxn];///当前弧,在其之前的边已经没有了作用
int level[maxn];///层次
void addedge(int u,int v,int c)
{
edge e;
e.from=u,e.to=v,e.cap=c,e.rev=G[v].size();
G[u].push_back(e);
e.from=v,e.to=u,e.cap=,e.rev=G[u].size()-;
G[v].push_back(e);
}
int bfs(int s,int t)
{
memset(level,-,sizeof(level));
queue<int>q;
level[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=; i<G[u].size(); i++)
{
edge e=G[u][i];
if(e.cap>&&level[e.to]<)
{
level[e.to]=level[u]+;
q.push(e.to);
}
}
}
if(level[t]<) return ;
else return ;
}
int dfs(int u,int t,int f)
{
if(u==t||f==) return f;
int flow=;
for(int &i=iter[u]; i<G[u].size(); i++)
{
edge e=G[u][i];
if(e.cap>&&level[u]+==level[e.to])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>)
{
G[u][i].cap-=d;
G[e.to][e.rev].cap+=d;
flow+=d;
f-=d;
if(f==) break;
}
}
}
return flow;
}
int max_flow(int s,int t)
{
int flow=;
while(bfs(s,t))
{
memset(iter,,sizeof(iter));
flow+=dfs(s,t,inf);
}
return flow;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int s=,t=n+;
for(int i=; i<=n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
addedge(s,i,a);
addedge(i,t,b);
}
for(int i=; i<=m; i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
addedge(a,b,w);
addedge(b,a,w);
}
cout<<max_flow(s,t)<<endl;
return ;
}
最大流模板
POJ 3469.Dual Core CPU 最大流dinic算法模板的更多相关文章
- 【网络流#8】POJ 3469 Dual Core CPU 最小割【ISAP模板】 - 《挑战程序设计竞赛》例题
[题意]有n个程序,分别在两个内核中运行,程序i在内核A上运行代价为ai,在内核B上运行的代价为bi,现在有程序间数据交换,如果两个程序在同一核上运行,则不产生额外代价,在不同核上运行则产生Cij的额 ...
- POJ 3469 Dual Core CPU 最大流
划分成两个集合使费用最小,可以转成最小割,既最大流. //#pragma comment(linker, "/STACK:1024000000,1024000000") #incl ...
- poj 3469 Dual Core CPU【求最小割容量】
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 21453 Accepted: 9297 ...
- POJ 3469 Dual Core CPU Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 23780 Accepted: 10338 Case Time Lim ...
- POJ 3469 Dual Core CPU (最小割建模)
题意 现在有n个任务,两个机器A和B,每个任务要么在A上完成,要么在B上完成,而且知道每个任务在A和B机器上完成所需要的费用.然后再给m行,每行 a,b,w三个数字.表示如果a任务和b任务不在同一个机 ...
- POJ 3469 Dual Core CPU(最小割)
[题目链接] http://poj.org/problem?id=3469 [题目大意] 有N个模块要在A,B两台机器上执行,在不同机器上有不同的花费 另有M个模块组(a,b),如果a和b在同一台机子 ...
- poj 3469 Dual Core CPU——最小割
题目:http://poj.org/problem?id=3469 最小割裸题. 那个限制就是在 i.j 之间连双向边. 根据本题能引出网络流中二元关系的种种. 别忘了写 if ( x==n+1 ) ...
- poj 3469 Dual Core CPU
题目描述:由于越来越多的计算机配置了双核CPU,TinySoft公司的首席技术官员,SetagLilb,决定升级他们的产品-SWODNIW.SWODNIW包含了N个模块,每个模块必须运行在某个CPU中 ...
- poj 3469 Dual Core CPU 最小割
题目链接 好裸的题....... 两个cpu分别作为源点和汇点, 每个cpu向元件连边, 权值为题目所给的两个值, 如果两个元件之间有关系, 就在这两个元件之间连边, 权值为消耗,这里的边应该是双向边 ...
随机推荐
- 人脸识别 1:1 和1:n
- mysql 触发器介绍
create trigger triggerName after/before insert/update/delete on tableName for each row --这句话在my ...
- Eclipse里的代码光标变成一个黑色块
以前经常在编写程序是不知到碰到键盘上的那个键了,或是那几个组合键了,使得Eclipse里的代码光标变成一个黑色块:在这个状态下,光标不在活动自如,只能一直往后写代码,就不想平时的 " | & ...
- 环境变量之path的一点理解
最初安装java环境时一直不明白为什么要配置环境变量,百度了一下还是理解不透彻. 后来安装python时也要配置环境变量.. 在经过未配置和配置的操作后,才有些理解path的含义. 1.未配置环境变量 ...
- vue watch,computed,metods的区别
通俗来讲:computed是在HTML DOM加载后马上执行的,如赋值:而methods则必须要有一定的触发条件才能执行,如点击事件:watch呢?它用于观察Vue实例上的数据变动.对应一个对象,键是 ...
- node Cannot enqueue Quit after invoking quit.
因为第二次调用数据库时连接关闭了,应该把connection.connect();放在请求的函数里面:不然第二次请求出错
- xm数据写入
reshape有两个参数: 其中,参数:cn为新的通道数,如果cn = 0,表示通道数不会改变. 参数rows为新的行数,如果rows = 0,表示行数不会改变. 注意:新的行*列必须与原来的行*列相 ...
- java中几个happens-before规则
1. 程序顺序规则:一个线程中的每个操作,happens-before 该线程中的任意后续操作. 2.监视器锁规则:对一个锁的解锁, happens-before 于随后对这个锁的加锁操作 3.vol ...
- Dao层向sql语句传递多个参数
手动封装: serviceImpl层 Map<String, Object> params = new HashMap<String, Object>(2);params.pu ...
- js增减日期
参考 https://www.cnblogs.com/gmq-sh/p/5194706.html date.setDate(date.getDate() + 3);