poj 3469 最小割模板sap+gap+弧优化
- /*以核心1为源点,以核心2为汇点建图,跑一遍最大流*/
- #include<stdio.h>
- #include<string.h>
- #include<queue>
- using namespace std;
- #define N 21000
- #define inf 999999999
- struct node {
- int u,v,w,next;
- }bian[N*40];
- int head[N],cur[N],gap[N],stac[N],top,n,sink,source,yong,start,dis[N];
- void init() {
- memset(head,-1,sizeof(head));
- top=0;yong=0;
- }
- void addedge(int u,int v,int w){
- bian[yong].u=u;
- bian[yong].v=v;
- bian[yong].w=w;
- bian[yong].next=head[u];
- head[u]=yong++;
- }
- void bfs() {
- queue<int>q;
- memset(dis,-1,sizeof(dis));
- q.push(sink);
- dis[sink]=0;
- while(!q.empty()) {
- int c=q.front();
- int i;
- q.pop();
- for(i=head[c];i!=-1;i=bian[i].next) {
- int v=bian[i].v;
- if(dis[v]==-1) {
- dis[v]=dis[c]+1;
- q.push(v);
- }
- }
- }
- }
- int ISAP() {
- int i,sum=0,k;
- bfs();
- memset(gap,0,sizeof(gap));
- for(i=1;i<=n;i++) {
- gap[dis[i]]++;
- cur[i]=head[i];
- }
- k=source;
- while(dis[source]<n) {
- if(k==sink) {
- int mi=inf,tep;
- for(i=0;i<top;i++){
- int e=stac[i];
- if(mi>bian[e].w) {
- mi=bian[e].w;
- tep=i;
- }
- }
- for(i=0;i<top;i++) {
- int e=stac[i];
- bian[e].w-=mi;
- bian[e^1].w+=mi;
- }
- sum+=mi;
- top=tep;
- k=bian[stac[top]].u;
- }
- for(i=cur[k];i!=-1;i=bian[i].next) {
- int v=bian[i].v;
- if(bian[i].w&&dis[k]==dis[v]+1) {
- cur[k]=i;
- k=v;
- stac[top++]=i;
- break;
- }
- }
- if(i==-1) {
- int m=n,i;
- for(i=head[k];i!=-1;i=bian[i].next) {
- int v=bian[i].v;
- if(m>dis[v]&&bian[i].w) {
- m=dis[v];
- cur[k]=i;
- }
- }
- if(--gap[dis[k]]==0)break;
- gap[dis[k]=m+1]++;
- if(k!=source)
- k=bian[stac[--top]].u;
- }
- }
- return sum;
- }
- int main() {
- int i,a,b,c,m;
- while(scanf("%d%d",&n,&m)!=EOF) {
- source=n+1;
- sink=n+2;
- init();
- for(i=1;i<=n;i++) {
- scanf("%d%d",&a,&b);
- addedge(source,i,a);
- addedge(i,source,0);
- addedge(i,sink,b);
- addedge(sink,i,0);
- }
- while(m--) {
- scanf("%d%d%d",&a,&b,&c);
- addedge(a,b,c);
- addedge(b,a,c);
- }
- n+=2;
- printf("%d\n",ISAP());
- }
- return 0;
- }
poj 3469 最小割模板sap+gap+弧优化的更多相关文章
- POJ 3469 最小割 Dual Core CPU
题意: 一个双核CPU上运行N个模块,每个模块在两个核上运行的费用分别为Ai和Bi. 同时,有M对模块需要进行数据交换,如果这两个模块不在同一个核上运行需要额外花费. 求运行N个模块的最小费用. 分析 ...
- 网络流SAP+gap+弧优化算法
poj1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54962 Accept ...
- 网络流 最大流—最小割 之SAP算法 详解
首先引入几个新名词: 1.距离标号: 所谓距离标号 ,就是某个点到汇点的最少的弧的数量(即边权值为1时某个点到汇点的最短路径长度). 设点i的标号为level[i],那么如果将满足level[i]=l ...
- poj 2125(最小割)
题目链接:http://poj.org/problem?id=2125 思路:将最小点权覆盖转化为最小割模型,于是拆点建图,将点i拆成i,i+n,其中vs与i相连,边容量为w[i]-,i+n与vt相连 ...
- 最大流/最小割模板(isap) POJ1273
isap模板核心代码: //d[]为距离标号数组,d[i]表示节点i到汇点的距离 //gap[]为GAP优化数组,gap[i]表示到汇点距离为i的节点个数 int dfs(int k,int flow ...
- poj2914无向图的最小割模板
题意:给出无向图的点,边,权值.求最小割. 思路:根据题目规模,最大流算法会超时. 网上参考的模板代码. 代码: /*最小割集◎Stoer-Wagner算法:一个无向连通网络,去掉一个边集可以使其变成 ...
- poj 3204(最小割--关键割边)
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- ISAP 最大流 最小割 模板
虽然这道题用最小割没有做出来,但是这个板子还是很棒: #include<stdio.h> #include<math.h> #include<string.h> # ...
- poj2914 Minimum Cut 全局最小割模板题
Minimum Cut Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 8324 Accepted: 3488 Case ...
随机推荐
- bzoj 1053 [ HAOI 2007 ] 反素数ant ——暴搜
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1053 试图打表找规律,但无果... 看TJ了,暴搜: 注意参数 w 是 long long. ...
- vmware centos7 没有网络设备
vmware centos7 没有网络设备 选择VMware 虚拟机模拟器为CentOS 64 即可;
- openStack 主机流量计运行状态 随笔记录
root@ruiy-controller:~# ifconfigeth0 Link encap:Ethernet HWaddr 0c:c4:7a:0d:97:2c ine ...
- HDU3085 Nightmare Ⅱ
题目: Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were ...
- Visual Studio q启动卡顿
在开发人员CMD下面执行 Devenv.exe /ResetSettings ,然后顺利打开 总发现vs2015经常把cpu给占满了,导致电脑卡的不要不要的.这是CodeLens引起的,因为装了VAs ...
- POJ 1160 DP
题目: poj 1160 题意: 给你n个村庄和它的坐标,现在要在其中一些村庄建m个邮局,想要村庄到最近的邮局距离之和最近. 分析: 这道题.很经典的dp dp[i][j]表示建第i个邮局,覆盖到第j ...
- .net core2.0 自定义中间件
一.中间件(Middleware) 中间件是被组装成一个应用程序管道来处理请求和响应的软件组件. 二.编写SimpleMiddleware using Microsoft.AspNetCore.Htt ...
- elasticsearch模板 template
https://elasticsearch.cn/article/335 elasticsearch模板 template 可以考虑的学习点: mapping的 _default_类型 动态模板:dy ...
- windows 装XP系统
笔记本型号:HPCQ40-506AX 1.在BIOS中更改启动顺序:将USB设为第一启动项2.插入装有PE系统的USB设备3.开机后一直按F124.到达选择系统界面,目前我的HPCQ40用其他系统进去 ...
- 利用 html+css 画同心圆(concentric circles)——绝对布局与相对布局
一.css 绘制圆 #circle { width: 300px; height: 300px; background-color: #000000; border-radius: 300px; } ...