最小费用最大流背诵用模板
2017-02-28 17:06
274 查看
这是我纯自己打的,洛谷上竟然A了
不敢相信、
丑陋的代码:
#include<iostream>
#include<cstdio>
using namespace std;
#include<cstring>
bool you[5001];
int dui[500000];
int pre[5001],xia[5001],hou[50001<<1],x,y,z,hehe,dis[5001],zhen1,zhen2,cost,sum,n,m,s,t,i;
struct lal
{
int zhi,fei,zhong;
}bian[50001<<1];
int tot=-1;
void jia(int x,int y,int z,int hehe)
{
hou[++tot]=xia[x];xia[x]=tot;bian[tot].zhi=z;bian[tot].fei=hehe;bian[tot].zhong=y;
}
void jian(int x,int y,int z,int hehe)
{
jia(x,y,z,hehe);
jia(y,x,0,-hehe);
}
bool spfa()
{
memset(you,0,sizeof(you));
memset(dis,0x7f,sizeof(dis));
memset(pre,0,sizeof(pre));
zhen1=0;zhen2=1;
dui[1]=s;
you[s]=1;
dis[s]=0;
while(zhen1!=zhen2)
{
int st=dui[++zhen1];
you[st]=0;
for(int i=xia[st];i!=-1;i=hou[i])
{
if(!bian[i].zhi)continue;
int nd=bian[i].zhong;
if(dis[st]+bian[i].fei<dis[nd])
{
dis[nd]=dis[st]+bian[i].fei;
pre[nd]=i^1;
if(!you[nd])
{
dui[++zhen2]=nd;
you[nd]=1;
}
}
}
}
if(!pre[t])return 0;
int min1=99999999;
for(int i=pre[t];i!=0;i=pre[bian[i].zhong])
{
min1=min(min1,bian[i^1].zhi);
}
for(int i=pre[t];i!=0;i=pre[bian[i].zhong])
{
bian[i].zhi+=min1;
bian[i^1].zhi-=min1;
cost+=bian[i^1].fei*min1;
}
sum+=min1;
return 1;
}
void mcmf(int s,int t)
{
while(spfa() ) ;
}
int main()
{
memset(xia,-1,sizeof(xia));
scanf("%d%d%d%d",&n,&m,&s,&t);
for(i=1;i<=m;i++)
{
scanf("%d%d%d%d",&x,&y,&z,&hehe);
jian(x,y,z,hehe);
}
mcmf(s,t);
printf("%d %d",sum,cost);
}
不敢相信、
丑陋的代码:
#include<iostream>
#include<cstdio>
using namespace std;
#include<cstring>
bool you[5001];
int dui[500000];
int pre[5001],xia[5001],hou[50001<<1],x,y,z,hehe,dis[5001],zhen1,zhen2,cost,sum,n,m,s,t,i;
struct lal
{
int zhi,fei,zhong;
}bian[50001<<1];
int tot=-1;
void jia(int x,int y,int z,int hehe)
{
hou[++tot]=xia[x];xia[x]=tot;bian[tot].zhi=z;bian[tot].fei=hehe;bian[tot].zhong=y;
}
void jian(int x,int y,int z,int hehe)
{
jia(x,y,z,hehe);
jia(y,x,0,-hehe);
}
bool spfa()
{
memset(you,0,sizeof(you));
memset(dis,0x7f,sizeof(dis));
memset(pre,0,sizeof(pre));
zhen1=0;zhen2=1;
dui[1]=s;
you[s]=1;
dis[s]=0;
while(zhen1!=zhen2)
{
int st=dui[++zhen1];
you[st]=0;
for(int i=xia[st];i!=-1;i=hou[i])
{
if(!bian[i].zhi)continue;
int nd=bian[i].zhong;
if(dis[st]+bian[i].fei<dis[nd])
{
dis[nd]=dis[st]+bian[i].fei;
pre[nd]=i^1;
if(!you[nd])
{
dui[++zhen2]=nd;
you[nd]=1;
}
}
}
}
if(!pre[t])return 0;
int min1=99999999;
for(int i=pre[t];i!=0;i=pre[bian[i].zhong])
{
min1=min(min1,bian[i^1].zhi);
}
for(int i=pre[t];i!=0;i=pre[bian[i].zhong])
{
bian[i].zhi+=min1;
bian[i^1].zhi-=min1;
cost+=bian[i^1].fei*min1;
}
sum+=min1;
return 1;
}
void mcmf(int s,int t)
{
while(spfa() ) ;
}
int main()
{
memset(xia,-1,sizeof(xia));
scanf("%d%d%d%d",&n,&m,&s,&t);
for(i=1;i<=m;i++)
{
scanf("%d%d%d%d",&x,&y,&z,&hehe);
jian(x,y,z,hehe);
}
mcmf(s,t);
printf("%d %d",sum,cost);
}
相关文章推荐
- POJ2135 最小费用最大流模板题
- 最小费用最大流模板
- 最小费用最大流的模板
- 关于最小费用最大流的求解思路与模板
- luogu3381 【模板】最小费用最大流
- 最小费用最大流模板
- 最小费用最大流(详解+模板)
- luogu3381 【模板】最小费用最大流
- 【洛谷】3381 【模板】最小费用最大流
- k-d tree背诵用模板
- Going Home hdu 1533 最小费用最大流,费用流好用模板
- dinic背诵用模板
- HDU 3376 Matrix Again 费用流(不MLE的最小费用最大流模板)
- MCMF最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流模板
- Farm Tour POJ - 2135 最小费用最大流(模板)
- 最小费用最大流模板
- 最小费用最大流,模板