• 算法复习——费用流模板(poj2135)


    题目:

    Farm Tour
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 16898   Accepted: 6543

    Description

    When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000. 

    To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again. 

    He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.

    Input

    * Line 1: Two space-separated integers: N and M. 

    * Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length. 

    Output

    A single line containing the length of the shortest tour. 

    Sample Input

    4 5
    1 2 1
    2 3 1
    3 4 1
    1 3 2
    2 4 2

    Sample Output

    6
    

    Source

    方法:

    题目相当于是从 1 到 N 找两条不相交的路径并使得总长度最小。由于每条边不能重复经过,所以我们将每条边容量视为 1,费用为边的长度,新建源点 s 向 1 连一条容量 2 费用0 的边,新建汇点 t,N 向 t 连一条容量为 2 费用为 0 的边,则题目就转化为求从 s 到 t 的最小费用最大流问题。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<ctime>
    #include<cctype>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int N=1005;
    const int M=50000;
    const int inf=0x3f3f3f3f;
    queue<int> que; 
    int n,m,ans=0;
    int first[50000],next[50000],go[50000],rest[50000],cost[50000],dis[1005],tot=1;
    bool visit[50000],work[50000];
    int src,des;
    void combin(int u,int v,int r,int w)
    {
      next[++tot]=first[u],first[u]=tot,go[tot]=v,rest[tot]=r,cost[tot]=w;
      next[++tot]=first[v],first[v]=tot,go[tot]=u,rest[tot]=0,cost[tot]=-w;
    }
    void init(int n,int m)
    {
      src=0,des=n+1;
      for(int i=1;i<=m;i++)
      {  
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        combin(u,v,1,w);
        combin(v,u,1,w);
      }
      combin(src,1,2,0);
      combin(n,des,2,0);
    }
    bool spfa()
    {
      memset(dis,inf,sizeof(dis));
      memset(work,false,sizeof(work));
      int u;
      que.push(src),dis[src]=0;
      while(!que.empty())
      {
        u=que.front(),que.pop();  
        visit[u]=false;
        for(int e=first[u];e;e=next[e])
        {
          int v=go[e];
          if(rest[e]&&dis[u]+cost[e]<dis[v])
          {
            dis[v]=dis[u]+cost[e];
            if(!visit[v])
            {
              que.push(v);
              visit[v]=true;
            }
          }
        }
      }
      return dis[des]<inf;
    }
    int dinic(int u,int flow)
    { 
      if(u==des) 
      {
        ans+=flow*dis[des];
        return flow;
      }
      work[u]=true;
      int res=0,temp,v,e;
      for(e=first[u];e;e=next[e])
      {
        if(!work[v=go[e]]&&rest[e]&&dis[v]==dis[u]+cost[e])
        {
          temp=dinic(v,min(rest[e],flow-res));
          if(temp)
          {
            rest[e]-=temp,rest[e^1]+=temp;
            res+=temp;
            if(res==flow)  break;
          }
        }
      }
      return res;
    }
    int maxflow()
    { 
      while(spfa())  dinic(src,inf);
      return ans;
    }
    int main()
    {
      //freopen("a.in","r",stdin);
      scanf("%d%d",&n,&m);
      init(n,m);
      cout<<maxflow()<<endl;
      return 0;
    }
  • 相关阅读:
    java内存管理之内存模型
    HTTP协议
    12 数值的整数次方
    11 二进制中1的个数+二进制补充知识点
    10 矩形覆盖
    9 变态跳台阶
    8 跳台阶
    7斐波那契数列
    6旋转数组的最小数字
    5用两个栈实现队列
  • 原文地址:https://www.cnblogs.com/AseanA/p/7106237.html
Copyright © 2020-2023  润新知