2017-12-08 23:21:09

## 两个板子

### 【模板】最大流

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 1000000000
const int _ = 100005;
struct edge{int to,next,w;}a[_<<1];
queue<int>Q;
int gi()
{
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return w?x:-x;
}
{
}
int bfs()
{
memset(dep,0,sizeof(dep));
Q.push(s);dep[s]=1;
while (!Q.empty())
{
int u=Q.front();Q.pop();
if (!dep[a[e].to]&&a[e].w)
dep[a[e].to]=dep[u]+1,Q.push(a[e].to);
}
return dep[t];
}
int dfs(int u,int flow)
{
if (u==t)
return flow;
for (int &e=cur[u];e;e=a[e].next)
if (dep[a[e].to]==dep[u]+1&&a[e].w)
{
int temp=dfs(a[e].to,min(a[e].w,flow));
if (temp) {a[e].w-=temp;a[e^1].w+=temp;return temp;}
}
return 0;
}
int main()
{
n=gi();m=gi();
/*
中间是建边的过程
*/
while (bfs())
{
while (int temp=dfs(s,inf)) ans+=temp;
}
printf("%d\n",ans);
return 0;
}

### 【模板】最小费用最大流

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 1000000000
const int _ = 100005;
struct edge{int to,next,w,cost;}a[_<<1];
queue<int>Q;
int gi()
{
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
void link(int u,int v,int w,int cost)
{
}
bool spfa()
{
memset(dis,63,sizeof(dis));
dis[s]=0;Q.push(s);
while (!Q.empty())
{
int u=Q.front();Q.pop();
{
int v=a[e].to;
if (a[e].w&&dis[v]>dis[u]+a[e].cost)
{
dis[v]=dis[u]+a[e].cost;
pe[v]=e;pv[v]=u;
if (!vis[v]) vis[v]=1,Q.push(v);
}
}
vis[u]=0;
}
return dis[t]<dis[0];
}
int main()
{
n=gi();m=gi();
/*
中间是建边的过程
*/
while (spfa())
{
int sum=inf;
for (int i=t;i!=s;i=pv[i])
sum=min(sum,a[pe[i]].w);
ans+=dis[t];
for (int i=t;i!=s;i=pv[i])
a[pe[i]].w-=sum,a[pe[i]^1].w+=sum;
}
printf("%d\n",ans);
return 0;
}

## 技巧总结

### 连续转移问题

https://www.luogu.org/problemnew/show/1251

### 一些图论的相关术语

##### 最大独立集

https://www.luogu.org/problemnew/show/2774

https://www.luogu.org/problemnew/show/3355

• star
首页