Dual Core CPU

Time Limit: 15000MS Memory Limit: 131072K

Total Submissions: 20935 Accepted: 9054

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

POJ Monthly–2007.11.25, Zhou Dong

将CPU1看成源点,将CPU2看成汇点,对于每个模块建立与源点和汇点容量弧,将不同模块a,b的额外花费建立容量为w的弧,建立起容量网络

#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define PI acos(-1.0)
#define MMM 0x3f3f3f3f
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout) const int INF = 0x3f3f3f3f; const int Max = 1000000; struct Edge
{
int v;
int cap;
int next;
}E[Max]; int top; int n,m; int Head[21000]; int Du[21000]; void Build(int u,int v,int w,int ww)
{
E[top].v=v; E[top].cap=w;
E[top].next=Head[u];
Head[u]=top++;
E[top].v=u; E[top].cap=ww;
E[top].next=Head[v];
Head[v]=top++;
} bool bfs()
{
memset(Du,0,sizeof(Du));
Du[0]=1;
queue<int>Q;
Q.push(0);
while(!Q.empty())
{
int a=Q.front();
Q.pop();
for(int i=Head[a];i!=-1;i=E[i].next)
{
if(Du[E[i].v]==0&&E[i].cap>0)
{
Du[E[i].v]=Du[a]+1;
Q.push(E[i].v);
}
}
}
return Du[n+1];
} int dfs(int star,int num)
{
if(star==n+1||num==0)
{
return num;
}
int s=0;
int ant;
for(int i=Head[star];i!=-1;i =E[i].next)
{
if(Du[star]+1==Du[E[i].v]&&(ant=dfs(E[i].v,min(num,E[i].cap)))>0)
{
E[i].cap-=ant;
num-=ant;
s+=ant;
E[i^1].cap+=ant;
if(num==0)
{
break;
}
}
}
return s;
} int Dinic()
{
int ant=0;
while(bfs())
{
ant+=dfs(0,INF);
}
return ant;
} int main()
{
int u,v,w,a,b;
while(~scanf("%d %d",&n,&m))
{
top=0;
memset(Head,-1,sizeof(Head));
for(int i=1;i<=n;i++)
{
scanf("%d %d",&a,&b);
Build(0,i,a,0);
Build(i,n+1,b,0);
}
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&u,&v,&w);
Build(u,v,w,w);
}
printf("%d\n",Dinic());
}
return 0;
}

Dual Core CPU的更多相关文章

  1. poj 3469 Dual Core CPU【求最小割容量】

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 21453   Accepted: 9297 ...

  2. POJ 3469.Dual Core CPU 最大流dinic算法模板

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 24830   Accepted: 10756 ...

  3. POJ 3469 Dual Core CPU Dual Core CPU

    Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 23780   Accepted: 10338 Case Time Lim ...

  4. poj3469 Dual Core CPU

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 25576   Accepted: 11033 ...

  5. POJ 3469 Dual Core CPU (最小割建模)

    题意 现在有n个任务,两个机器A和B,每个任务要么在A上完成,要么在B上完成,而且知道每个任务在A和B机器上完成所需要的费用.然后再给m行,每行 a,b,w三个数字.表示如果a任务和b任务不在同一个机 ...

  6. poj 3469 Dual Core CPU

    题目描述:由于越来越多的计算机配置了双核CPU,TinySoft公司的首席技术官员,SetagLilb,决定升级他们的产品-SWODNIW.SWODNIW包含了N个模块,每个模块必须运行在某个CPU中 ...

  7. poj 3469 Dual Core CPU 最小割

    题目链接 好裸的题....... 两个cpu分别作为源点和汇点, 每个cpu向元件连边, 权值为题目所给的两个值, 如果两个元件之间有关系, 就在这两个元件之间连边, 权值为消耗,这里的边应该是双向边 ...

  8. 【做题】POJ3469 Dual Core CPU——第一道网络流

    刚学了Dinic就开始做题,然后就崩了. 题意:若干个任务,可以放在两个CPU中任意一个上完成,各有一定代价.其中又有若干对任务,如果它们不在同一个CPU上完成,会产生额外代价.最小化并输出代价. 一 ...

  9. POJ3469:Dual Core CPU——题解

    http://poj.org/problem?id=3469 题目大意: 两个CPU,处理每个任务有不同的代价,有些对任务如果不在同一个CPU就会增加代价,求最小代价. ——————————————— ...

随机推荐

  1. PostgreSQL Replication之第十章 配置Slony(2)

    10.2 理解 Slony如何工作 在我们开始复制我们的第一个数据库之前,我们想深入Slony的架构.理解这是如何工作的是非常重要的,否则,将不可能以一种有用的和合理的方法使用这个软件.与事务日志流不 ...

  2. Leetcode: Trapping Rain Water II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  3. fighting_使用CSS美化文字

    CSS3颜色渐变 background-image:linear-gradient(black,blue,green,red); 默认从上到下显示. 示例代码: <!DOCTYPE html&g ...

  4. const修饰

    const int A() //const // ====>int A(const this) { //观点1:const是修饰a,但是通过测试,我们发现,b++也不能编译通过 //这说明:co ...

  5. 。。。JDBC里面的sql与hibernate里面的hql有关占位符"?"的总结。。。

    今天在看Hibernate的时候,似乎又有了一些收获的东东,嘻嘻... 我记得很清楚:以前用JDBC操作数据库的时候是这样的: String sql = "select * from use ...

  6. android studio ADB not responding.

    打开cmd    输入  netstat -aon|findstr "5037"   找到谁在占用5037端口 记住他的pid. 例如pid为 2028 输入  taskkill ...

  7. ligerui_ligerTree_003_配置url参数,加载“树”

    配置url参数,加载“树”: 源码下载地址:http://download.csdn.net/detail/poiuy1991719/8571255 效果图:json.txt HTML代码: < ...

  8. ios 开源代码

    .开源代码 http://www.oschina.net/iOS/codingList/365/ios-button http://www.devdiv.com/iOS_iPhone-iOS6%E6% ...

  9. CSSの神小结-简单备忘一下(亲测可用)

    css 选择器优先级,标签>id>class 权重 id>class>标签 只记录能想到的以免遗忘: 1.字体css可继承 2.表格:表格细线的合并,表格单元格合并,单元格内容 ...

  10. session与cookie的讲解

    session_start();//开启session http,无状态性 记录状态SESSION COOKIE SESSION :存储在服务端(器)的:每个人存一份:可以存储任意类型的数据:默认过期 ...