zoukankan      html  css  js  c++  java
  • 费用流

    Code

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    using namespace std;
    int tot=1,h[500000],n,m,s,t,flow[500000],up[500000],q[500000],dis[500000],pre[500000];
    bool vis[500000];
    
    struct edge{
    	int to,nxt,z,w;
    }e[500000]; 
    void add(int x,int y,int z,int w)
    {
    	e[++tot].to=y;
    	e[tot].nxt=h[x];
    	e[tot].w=w;
    	e[tot].z=z;
    	h[x]=tot;
    }
    bool spfa(int x,int y)
    {
    	memset(flow,127,sizeof(flow));
    	memset(vis,false,sizeof(vis));
    	memset(dis,127,sizeof(dis));
    	int head=0,tail=0;
    	q[++tail]=x;
    	dis[x]=0,pre[y]=-1,vis[x]=true;
    	while (head<tail)
    	{
    		head++;
    		int u=q[head];
    		vis[u]=false;
    		for (int i=h[u];i;i=e[i].nxt)
    		{
    			int v=e[i].to;
    			if (dis[v]>dis[u]+e[i].w&&e[i].z>0)
    			{
    				dis[v]=dis[u]+e[i].w,pre[v]=u,up[v]=i;
    				flow[v]=min(flow[u],e[i].z);
    				if (vis[v]==false)
    					vis[v]=true,q[++tail]=v;
    			}
    		}
    	}
    	if (pre[y]!=-1) return true;
    	return false;
    }
    int main()
    {
    	scanf("%d%d%d%d",&n,&m,&s,&t);
    	for (int i=1;i<=m;i++)
    	{
    		int q,p,c,c1;
    		scanf("%d%d%d%d",&q,&p,&c,&c1);
    		add(q,p,c,c1),add(p,q,0,-c1);
    	}
    	int ansf=0,ansc=0;
    	while (spfa(s,t)==true)
    	{
    		int o=t;
    		ansf+=flow[t];
    		ansc+=flow[t]*dis[t];
    		while (o!=s)
    		{
    			e[up[o]].z-=flow[t];
    			e[up[o]^1].z+=flow[t];
    			o=pre[o];
    		}
    	}
    	printf("%d %d
    ",ansf,ansc);
    } 
    
  • 相关阅读:
    VS1053 datasheet 解读笔记
    C# List Find方法
    git push & git pull 推送/拉取指定分支
    Python 匿名函数
    Python 函数
    java jdk安装与环境变量配置
    Anroid开发环境配置
    Asponse.Cell操作Excel
    C#调试DeBug
    Ext.gridPanel中内容对齐
  • 原文地址:https://www.cnblogs.com/nibabadeboke/p/13498880.html
Copyright © 2011-2022 走看看