zoukankan      html  css  js  c++  java
  • hdu 6118 最小费用可行流(注意与最大流的区别)

    题意:


    思路:。。。。注意是可行流,在找增广路的时候条件要进行修改。。。。修改的地方看代码注释


    代码:

    #include<bits/stdc++.h>
    using namespace std;
    #define MEM(a,b) memset(a,b,sizeof(a))
    #define bug puts("bug");
    #define PB push_back
    #define MP make_pair
    #define X first
    #define Y second
    typedef unsigned long long ll;
    typedef pair<int,int> pii;
    const int maxn=4e5+10;
    const int mod=1000000007;
    using namespace std;
    int a[maxn],b[maxn],c[maxn],d[maxn],n,m;
    
    const int MAXN = 10000;
    const int MAXM = 100000;
    const int INF = 0x3f3f3f3f;
    struct Edge{
        int to,next,cap,flow,cost;
    } edge[MAXM];
    int head[MAXN],tol;
    int pre[MAXN],dis[MAXN];
    bool vis[MAXN];
    int N;
    void init(int n){
        N = n;
        tol = 0;
        memset(head,-1,sizeof(head));
    }
    void addedge(int u,int v,int cap,int cost){
        edge[tol].to = v;
        edge[tol].cap = cap;
        edge[tol].cost = cost;
        edge[tol].flow = 0;
        edge[tol].next = head[u];
        head[u] = tol++;
        edge[tol].to = u;
        edge[tol].cap = 0;
        edge[tol].cost = -cost;
        edge[tol].flow = 0;
        edge[tol].next = head[v];
        head[v] = tol++;
    }
    bool spfa(int s,int t){
        queue<int>q;
        for(int i = 0; i < N; i++){
            dis[i] = INF;
            vis[i] = false;
            pre[i] = -1;
        }
        dis[s] = 0;
        vis[s] = true;
        q.push(s);
        while(!q.empty()){
            int u = q.front();
            q.pop();
            vis[u] = false;
            for(int i = head[u]; i != -1; i = edge[i].next){
                int v = edge[i].to;
                if(edge[i].cap > edge[i].flow &&dis[v] > dis[u] + edge[i].cost ){
                    dis[v] = dis[u] + edge[i].cost;
                    pre[v] = i;
                    if(!vis[v]){
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
        }
        return dis[t]<=0;//可行流!!!!!!
        //if(pre[t] == -1)return false;
        //else return true;//最大流
    }
    
    int minCostMaxflow(int s,int t,int &cost){
        int flow = 0;
        cost = 0;
        while(spfa(s,t)){
            int Min = INF;
            for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]){
                if(Min > edge[i].cap - edge[i].flow)
                    Min = edge[i].cap - edge[i].flow;
            }
            for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]){
                edge[i].flow += Min;
                edge[i^1].flow -= Min;
                cost += edge[i].cost * Min;
            }
            flow += Min;
        }
        return flow;
    }
    int main(){
        while(~scanf("%d%d",&n,&m)){
            init(n+3);
            for(int i=1;i<=n;i++){
                scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
                addedge(0,i,b[i],a[i]);
                addedge(i,n+1,d[i],-c[i]);
            }
            int x,y,z;
            for(int i=1;i<=m;i++){
                scanf("%d%d%d",&x,&y,&z);
                addedge(x,y,1e9,z);
                addedge(y,x,1e9,z);
            }
            int ans=0;
            minCostMaxflow(0,n+1,ans);
            cout<<-ans<<endl;
        }
        return 0;
    }


  • 相关阅读:
    童年
    转:如何破解超星打印页数限制
    2016.7.14.2014noip模拟赛D1(网上貌似搜不到
    商店购物
    几道有趣的题
    2016.7.11.第27套测试题(noip2013提D1)
    2016.7.12.第28套测试题(2013noip题D2)
    跨域访问http接口的使用
    心得之——程序的修改和拓展
    不使用<script>导入js文件
  • 原文地址:https://www.cnblogs.com/zhangxianlong/p/10672500.html
Copyright © 2011-2022 走看看