划分成两个集合使费用最小,可以转成最小割,既最大流。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c)
{
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c)
{
return max(max(a,b),max(a,c));
}
void debug()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in","r",stdin);
// freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch()
{
int ch;
while((ch=getchar())!=EOF)
{
if(ch!=' '&&ch!='\n')return ch;
}
return EOF;
} struct Edge
{
int to,cap;
};
const int maxn = ;
vector<int> g[maxn];
vector<Edge> edge;
int n,m,s,t;
int d[maxn];
int vis[maxn];
int cur[maxn]; void add(int u,int v,int cap)
{
//printf("%d -> %d : %d\n", u, v, cap);
edge.push_back((Edge){v, cap});
g[u].push_back(edge.size() - );
edge.push_back((Edge){u, });
g[v].push_back(edge.size() - );
}
void build()
{
for(int i = ; i <= n; i++)
g[i].clear();
edge.clear();
s = n + ;
t = s + ;
for(int i = ; i <= n; i++)
{
int a,b;
scanf("%d%d", &a, &b);
add(s, i, a);
add(i, t, b);
}
for(int i = ; i <= m; i++)
{
int a,b,w;
scanf("%d%d%d", &a, &b, &w);
add(a, b, w);
add(b, a, w);
}
} bool bfs()
{
memset(vis, , sizeof(vis));
d[s] = ;
queue<int> q;
q.push(s);
vis[s] = true;
while(!q.empty())
{
int x = q.front(); q.pop();
for (int i = ; i < g[x].size(); i++)
{
Edge &e = edge[g[x][i]];
if(e.cap> && !vis[e.to])
{
vis[e.to] = true;
q.push(e.to);
d[e.to] = d[x] + ;
}
}
}
return vis[t];
} int dfs(int u,int f)
{
if(u==t || f==) return f;
for(int &i = cur[u]; i < g[u].size(); i++)
{
Edge &e = edge[g[u][i]];
if(e.cap> && d[e.to] == d[u] + )
{
int d = dfs(e.to, min(f,e.cap));
if(d>)
{
e.cap -= d;
edge[g[u][i]^].cap += d;
return d;
}
}
}
return ;
} int max_flow()
{
int res = ;
while(bfs())
{
memset(cur, , sizeof(cur));
int d;
while(d=dfs(s,INF))
{
res += d;
}
}
return res;
}
int main()
{
debug();
while(scanf("%d%d", &n, &m) != EOF)
{
build();
printf("%d\n", max_flow());
}
return ;
}

POJ 3469 Dual Core CPU 最大流的更多相关文章

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

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

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

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

  3. POJ 3469 Dual Core CPU Dual Core CPU

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

  4. POJ 3469 Dual Core CPU(最小割)

    [题目链接] http://poj.org/problem?id=3469 [题目大意] 有N个模块要在A,B两台机器上执行,在不同机器上有不同的花费 另有M个模块组(a,b),如果a和b在同一台机子 ...

  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——最小割

    题目:http://poj.org/problem?id=3469 最小割裸题. 那个限制就是在 i.j 之间连双向边. 根据本题能引出网络流中二元关系的种种. 别忘了写 if ( x==n+1 ) ...

  7. poj 3469 Dual Core CPU

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

  8. 【网络流#8】POJ 3469 Dual Core CPU 最小割【ISAP模板】 - 《挑战程序设计竞赛》例题

    [题意]有n个程序,分别在两个内核中运行,程序i在内核A上运行代价为ai,在内核B上运行的代价为bi,现在有程序间数据交换,如果两个程序在同一核上运行,则不产生额外代价,在不同核上运行则产生Cij的额 ...

  9. poj 3469 Dual Core CPU 最小割

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

随机推荐

  1. vs2013密钥

    Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥) ...

  2. Javascript垃圾回收机制(学习笔记)

    1,javascript具有自动的垃圾回收机制,自动内存的分配和无用内存的回收都可以自动管理.垃圾回收器周期性的执行: 2,Javascript的垃圾回收策略分为:引用计数和标记清除: 2.1 标记清 ...

  3. vmware workstation 上创建的centos 7.2 ,新添加一块网卡。无法找到配置文件。

    在vmware workstation 11上,新建一个centos 7.2系统. 初装带有一个块网卡:能够在/etc/sysconfig/network-scripts/目录下找到相应的网卡配置文件 ...

  4. js关于页面坐标api

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  5. (转)深入理解javascript连续赋值表达式

    引入 今天逛园子的时候看到一道javascript面试题,是关于连续赋值的,正好最近读jQuery源码经常看到这种连续赋值的表达式,所以很感兴趣. 废话不多说,来看题: var a = {n: 1} ...

  6. c#绘制表格

    //绘制表格 //定义绘制表格的参数 Pen _Pen = new Pen(Brushes.Black); // VBConversions Note: Initial value cannot be ...

  7. 大毕设-MATLAB-FFT实现

    引用来自:http://blog.csdn.net/sinwel/article/details/8115673 %仿真参数中的含义 % Ts 表示间隔Ts时间采样,这个越小越接近连续信号,而实际上不 ...

  8. mysql高性能索引策略

    转载说明:http://www.nyankosama.com/2014/12/19/high-performance-index/ 1. 引言 随着互联网时代地到来,各种各样的基于互联网的应用和服务进 ...

  9. MIME Type

    一.首先,我们要了解浏览器是如何处理内容的.在浏览器中显示的内容有 HTML.有 XML.有 GIF.还有 Flash --那么,浏览器是如何区分它们,决定什么内容用什么形式来显示呢?答案是 MIME ...

  10. oracle sql改写

    or可以改写成union 但是要注意,改写成union的时候一定要有一个唯一列参照,不然会少记录,因为union会去重. 可以用的唯一列:唯一索引列,主键列,rowid,rownum(视图里用这个)