最小费用最大流背诵用模板

来源:互联网 发布:mac没声音 编辑:程序博客网 时间:2024/05/21 09:46

这是我纯自己打的,洛谷上竟然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);
 } 

0 0
原创粉丝点击