CH Round #17-C

这个算是一个技能点吧,不点不会,点了就没什么了。懒得写看书吧书上的1应该是0。。。

我又回来了太懒了不想翻书还是写写吧

必须边的判定条件:该边流量为0且两端的点在残余网络不在同一个联通分量

可行边的判定条件:该边流量为0或两端的点在残余网络在同一个联通分量

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int inf=(<<); struct node
{
int x,y,c,id,next,other;
}a[];int len,last[];
void ins(int x,int y,int c,int id)
{
int k1,k2; len++;k1=len;
a[len].x=x;a[len].y=y;a[len].c=c;a[len].id=id;
a[len].next=last[x];last[x]=len; len++;k2=len;
a[len].x=y;a[len].y=x;a[len].c=;a[len].id=-;
a[len].next=last[y];last[y]=len; a[k1].other=k2;
a[k2].other=k1;
}
int st,ed;
int h[],list[];
bool bt_h()
{
int head=,tail=;list[]=st;
memset(h,,sizeof(h));h[st]=;
while(head!=tail)
{
int x=list[head];
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(h[y]==&&a[k].c>)
{
h[y]=h[x]+;
list[tail++]=y;
}
}
head++;
}
if(h[ed]==)return false;
return true;
}
int findflow(int x,int f)
{
if(x==ed)return f;
int s=;
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(a[k].c>&&h[y]==h[x]+&&f>s)
{
int t=findflow(y,min(a[k].c,f-s));
s+=t;a[k].c-=t;a[a[k].other].c+=t;
}
}
if(s==)h[x]=;
return s;
} //------------------dicnic---------------------- bool b[];
struct enode
{
int x,y,id,next;
}e[];int elen,elast[];
void eins(int x,int y,int id)
{
elen++;
e[elen].x=x;e[elen].y=y;e[elen].id=id;
e[elen].next=elast[x];elast[x]=elen;
}
int z,dfn[],low[];
int top,sta[];bool v[];
int cnt,bel[];
void SCC(int x)
{
dfn[x]=low[x]=++z;
sta[++top]=x;v[x]=true;
for(int k=elast[x];k;k=e[k].next)
{
int y=e[k].y;
if(dfn[y]==)
{
SCC(y);
low[x]=min(low[x],low[y]);
}
else if(v[y]==true)
low[x]=min(low[x],dfn[y]);
}
if(dfn[x]==low[x])
{
int k;cnt++;
do
{
k=sta[top];top--;
v[k]=false;
bel[k]=cnt;
}while(k!=x);
}
} int aslen,as[];
int main()
{
int n,m,T,x,y;
scanf("%d%d%d",&n,&m,&T);
st=n+m+,ed=n+m+;
len=;memset(last,,sizeof(last));
for(int i=;i<=T;i++)
scanf("%d%d",&x,&y), ins(x,y+n,,i);
for(int i=;i<=n;i++)ins(st,i,,-);
for(int i=;i<=m;i++)ins(i+n,ed,,-); int ans=;
while(bt_h())
{
ans+=findflow(st,inf);
} //----------------------------------------- elen=;memset(elast,,sizeof(elast));
memset(b,true,sizeof(b));
for(int i=;i<=len;i++)
{
if(a[i].c==&&a[i].y>n&&a[i].x!=st&&a[i].y!=ed)b[a[i].id]=false;
if(a[i].c==)
eins(a[i].x,a[i].y,a[i].id);
} z=,top=,cnt=;
for(int i=;i<=n+m+;i++)
if(dfn[i]==)SCC(i); for(int i=;i<=elen;i++)
if(bel[e[i].x]==bel[e[i].y])b[e[i].id]=false; aslen=;
for(int i=;i<=T;i++)
if(b[i]==true)as[++aslen]=i;
printf("%d\n",aslen);
for(int i=;i<aslen;i++)printf("%d ",as[i]);
if(aslen!=)printf("%d\n",as[aslen]);
return ;
}

舞动的夜晚

poj1966 不难。拆点,删除一个点相当于把他的两个点之间的边割掉。有趣的是,这题枚举起始点和结束点,意在把这两个点分在不同的集合,使得图不联通。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int inf=(<<); struct node
{
int x,y,c,next,other;
}a[],e[];int len,last[],elen,elast[];
void ins(int x,int y,int c)
{
int k1,k2; len++;k1=len;
a[len].x=x;a[len].y=y;a[len].c=c;
a[len].next=last[x];last[x]=len; len++;k2=len;
a[len].x=y;a[len].y=x;a[len].c=;
a[len].next=last[y];last[y]=len; a[k1].other=k2;
a[k2].other=k1;
}
int st,ed;
int h[],list[];
bool bt_h()
{
int head=,tail=;list[]=st;
memset(h,,sizeof(h));h[st]=;
while(head!=tail)
{
int x=list[head];
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(h[y]==&&a[k].c>)
{
h[y]=h[x]+;
list[tail++]=y;
}
}
head++;
}
if(h[ed]==)return false;
return true;
}
int findflow(int x,int f)
{
if(x==ed)return f;
int s=;
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(a[k].c>&&h[y]==h[x]+&&f>s)
{
int t=findflow(y,min(a[k].c,f-s));
s+=t;a[k].c-=t;a[a[k].other].c+=t;
}
}
if(s==)h[x]=;
return s;
} char ch;
void sc(int &x,int &y)
{
ch=getchar();
while(ch!='(')ch=getchar();
scanf("%d",&x);x++;
ch=getchar();
while(ch!=',')ch=getchar();
scanf("%d",&y);y++;
ch=getchar();
while(ch!=')')ch=getchar();
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
len=;memset(last,,sizeof(last));
for(int i=;i<=n;i++)ins(i,i+n,);
for(int i=;i<=m;i++)
{
int x,y;sc(x,y);
ins(x+n,y,inf);ins(y+n,x,inf);
} memcpy(e,a,sizeof(e));
elen=len;memcpy(elast,last,sizeof(elast));
int mmin=inf;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(i!=j)
{
memcpy(a,e,sizeof(a));
len=elen;memcpy(last,elast,sizeof(last)); st=i+n,ed=j;
int ans=;
while(bt_h())
{
ans+=findflow(st,inf);
}
mmin=min(ans,mmin);
}
if(mmin==inf)printf("%d\n",n);
else printf("%d\n",mmin);
}
return ;
}

poj1966

poj3422 算是套路题吧,拆点后对于一个点的自连,连一条流量为1,费用为点权的边,连一条流量为K-1,费用为0的边。开始我从1,1的出边为起始到n,n的入边,问题在于无法控制只跑K次。天真的我还写了while(K--&&spfa())事实证明这样错得一批

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int inf=(<<); struct node
{
int x,y,c,d,next,other;
}a[];int len,last[];
void ins(int x,int y,int c,int d)
{
int k1,k2; len++;k1=len;
a[len].x=x;a[len].y=y;a[len].c=c;a[len].d=d;
a[len].next=last[x];last[x]=len; len++;k2=len;
a[len].x=y;a[len].y=x;a[len].c=;a[len].d=-d;
a[len].next=last[y];last[y]=len; a[k1].other=k2;
a[k2].other=k1;
}
int ans,st,ed;
int list[],d[],pre[],c[];
bool v[];
bool spfa()
{
memset(d,,sizeof(d));d[st]=;
memset(v,false,sizeof(v));v[st]=true;
int head=,tail=;list[]=st;c[st]=inf;
while(head!=tail)
{
int x=list[head];
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(a[k].c>&&d[y]>d[x]+a[k].d)
{
d[y]=d[x]+a[k].d;
pre[y]=k;
c[y]=min(a[k].c,c[x]);
if(v[y]==false)
{
v[y]=true;
list[tail++]=y;
if(tail==)tail=;
}
}
}
v[x]=false;
head++;if(head==)head=;
}
if(d[ed]==d[])return false;
else
{
ans+=d[ed]*c[ed];
int y=ed;
while(y!=st)
{
int k=pre[y];
a[k].c-=c[ed];
a[a[k].other].c+=c[ed];
y=a[k].x;
}
return true;
}
} int n,mp[][];
int pt(int x,int y){return n*(x-)+y;}
int main()
{
int K;
scanf("%d%d",&n,&K);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&mp[i][j]); for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(i!=n)ins(n*n+pt(i,j),pt(i+,j),K,);
if(j!=n)ins(n*n+pt(i,j),pt(i,j+),K,);
ins(pt(i,j),n*n+pt(i,j),,-mp[i][j]);
ins(pt(i,j),n*n+pt(i,j),K-,);
}
st=pt(,);ed=n*n+pt(n,n);
ans=;
while(spfa()==true);
printf("%d\n",-ans);
return ;
}

poj3422

0x6A 网络流初步的更多相关文章

  1. 二分图&网络流初步

    链接 : 最小割&网络流应用 EK太低级了,不用. 那么请看:#6068. 「2017 山东一轮集训 Day4」棋盘,不用EK你试试? dinic模板及部分变形应用见zzz大佬的博客:网络流学 ...

  2. 网络流初步——增广路算法(EK)模板

    #include <iostream> #include <queue> #include<string.h> using namespace std; #defi ...

  3. 网络流初步:<最大流>——核心(增广路算法)(模板)

    增广路的核心就是引入了反向边,使在进行道路探索选择的时候增加了类似于退路的东西[有一点dp的味道??] 具体操作就是:1.首先使用结构体以及数组链表next[ MAXN ]进行边信息的存储 2.[核心 ...

  4. 网络流学习(转载自ssw 的博客)

    众所周知,网络流是探究网络上运输的一种图论分支.但是大多数人在第一次接触这个题时都有些畏惧感(比如说我),大佬可以自信跳过.. 本文包括: 1.网络流的概念及基本性质 2.略谈 Edmonds-Kar ...

  5. 最小割&网络流应用

    重要链接 基础部分链接 : 二分图 & 网络流初步 zzz大佬博客链接 : 网络流学习笔记 重点内容:最小割二元关系新解(lyd's ppt) 题目:网络流相关题目 lyd神犇课件链接 : 网 ...

  6. woj1008feedinganimals2-贪心-网络流

    title: woj1008feedinganimals2-贪心-网络流 date: 2020-03-07 categories: acm tags: [acm,woj,网络流,贪心] 中等题. 标准 ...

  7. ACM暑假集训第三周小结

    这一周学的图论,学了这么些 两种存图的方法:邻接矩阵( map[n][n] ) , 邻接表( headlis[n] , vector<int> G[n] )存图的方法,各有各的好,我的理解 ...

  8. ASP.Net MVC开发基础学习笔记:五、区域、模板页与WebAPI初步

    一.区域—麻雀虽小,五脏俱全的迷你MVC项目 1.1 Area的兴起 为了方便大规模网站中的管理大量文件,在ASP.NET MVC 2.0版本中引入了一个新概念—区域(Area). 在项目上右击创建新 ...

  9. Graph Cuts初步理解

    一些知识点的初步理解_8(Graph Cuts,ing...) Graph cuts是一种十分有用和流行的能量优化算法,在计算机视觉领域普遍应用于前背景分割(Image segmentation).立 ...

随机推荐

  1. SAS学习笔记之《SAS编程与数据挖掘商业案例》(2)数据获取与数据集操作

    SAS学习笔记之<SAS编程与数据挖掘商业案例>(2)数据获取与数据集操作 1. SET/SET效率高,建立的主表和建表索引的查询表一般不排序, 2. BY语句,DATA步中,BY语句规定 ...

  2. MonoBehaviour简述

    Unity中的脚本都是继承自MonoBehaviour. 一.基础函数: 创建脚本就默认的update.start方法:(这些官方的文档都是有的) Start:Update函数第一次运行前调用,一般用 ...

  3. 01--TCP状态转换

    参考大牛文章: http://www.cnblogs.com/qlee/archive/2011/07/12/2104089.html

  4. JS——try catch throw

    本例检测输入变量的值.如果值是错误的,会抛出一个异常(错误).catch 会捕捉到这个错误,并显示一段自定义的错误消息: <script> function myFunction() { ...

  5. Python语言之变量2(命名规则,类型转换)

    1.命名规则 1.起始位为字母(大小写)或下划线('_') 2.其他部分为字母(大小写).下划线('_')或数字(0-9) 3.大小写敏感 2.先体验一把: #Ask the user their n ...

  6. @Override注解

    @Override注解对于代码可读性的提升十分巨大 而且良好的可读性是一个优秀程序员必备的基础素养

  7. Linux之iptables(三、命令--->单主机)

    iptables命令规则格式: iptables [-t table] SUBCOMMAND chain [-m matchname[per-match-options]] -j targetname ...

  8. ansible - 基本用法

    目录 ansible - 01 一. 安装与使用 ansible命令格式 查看ansible生成的配置文件 ssh认证方式 ansible的第一个命令 弱口令校验 host-pattern的格式 模块 ...

  9. BZOJ 1230 Usaco2008 Nov 开关灯

    [题意概述] 给出一个01序列,初始时序列全为0,每次有修改操作或询问操作,修改操作要求把L~R区间中的0变成1,1变成0,查询操作要求输出L~R区间的1的个数 [题解] 线段树. 每次区间修改把区间 ...

  10. go 语言优势

    一:为什么用Go来做抽奖系统 1.Go  vs PHP/JAVA ①:高并发,Go协程优于PHP多进程,JAVA多线程模式 ②:高并发,编译后的二进制优于PHP解释型,JAVA虚拟机 3:高效网络模型 ...