zoukankan      html  css  js  c++  java
  • HDU 3435A new Graph Game(网络流之最小费用流)

    题目地址:HDU 3435

    这题刚上来一看,感觉毫无头绪。

    。再细致想想。。

    发现跟我做的前两道费用流的题是差点儿相同的。

    能够往那上面转换。

    建图基本差点儿相同。仅仅只是这里是无向图。建图依旧是拆点,推断入度出度。最后推断是否满流,满流的话这时的费用流是符合要求的。输出。不能满流的话,输出NO。

    代码例如以下:

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
    #include <queue>
    #include <map>
    #include<algorithm>
    using namespace std;
    const int INF=0x3f3f3f3f;
    int head[3000], source, sink, cnt, flow, cost, num;
    int d[3000], vis[3000], pre[3000], cur[3000];
    queue<int>q;
    struct node
    {
        int u, v, cap, cost, next;
    }edge[10000000];
    void add(int u, int v, int cap, int cost)
    {
        edge[cnt].v=v;
        edge[cnt].cap=cap;
        edge[cnt].cost=cost;
        edge[cnt].next=head[u];
        head[u]=cnt++;
    
        edge[cnt].v=u;
        edge[cnt].cap=0;
        edge[cnt].cost=-cost;
        edge[cnt].next=head[v];
        head[v]=cnt++;
    }
    int spfa()
    {
        memset(d,INF,sizeof(d));
        memset(vis,0,sizeof(vis));
        int minflow=INF, i;
        q.push(source);
        d[source]=0;
        cur[source]=-1;
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            vis[u]=0;
            for(i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].v;
                if(d[v]>d[u]+edge[i].cost&&edge[i].cap)
                {
                    d[v]=d[u]+edge[i].cost;
                    minflow=min(minflow,edge[i].cap);
                    cur[v]=i;
                    if(!vis[v])
                    {
                        q.push(v);
                        vis[v]=1;
                    }
                }
            }
        }
        if(d[sink]==INF) return 0;
        flow+=minflow;
        cost+=minflow*d[sink];
        for(i=cur[sink];i!=-1;i=cur[edge[i^1].v])
        {
            edge[i].cap-=minflow;
            edge[i^1].cap+=minflow;
        }
        return 1;
    }
    void mcmf(int n)
    {
        while(spfa());
            if(flow==n)
            printf("%d
    ",cost);
            else
                printf("NO
    ");
    }
    int main()
    {
        int t, n, m, i, j, a, b, c;
        scanf("%d",&t);
        num=0;
        while(t--)
        {
            num++;
            scanf("%d%d",&n,&m);
            memset(head,-1,sizeof(head));
            cnt=0;
            source=0;
            sink=2*n+1;
            flow=0;
            cost=0;
            for(i=1;i<=n;i++)
            {
                add(source,i,1,0);
                add(i+n,sink,1,0);
            }
            while(m--)
            {
                scanf("%d%d%d",&a,&b,&c);
                add(a,b+n,1,c);
                add(b,a+n,1,c);
            }
            printf("Case %d: ",num);
            mcmf(n);
        }
        return 0;
    }
    


  • 相关阅读:
    Django之模板
    Web框架的原理
    mysql 索引
    pymysql模块的使用
    MySQl创建用户和授权
    php去除h5标签
    phpcms_完整版
    快速上手Linux 玩转典型应用_慕课网笔记
    php 微擎
    二维码接口
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6847144.html
Copyright © 2011-2022 走看看