• POJ 2135 最小费用最大流 入门题


    Farm Tour
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 19207   Accepted: 7441

    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

    解析 题目是无向图 求两条最短路径的长度 ,两条路径不允许有相交边。其实就是求一条最短路,把边删掉,再找一条。其实可以转化为网络流来写。

    无向图把我们建边要建两次ab,ba;ba,ab(模板可以有平行边)   把每个边的容量设为1 花费为边的长度。然后找两次增广路退出来的花费就是答案。

    也可以建立超源点汇点 0,n+1   建立两条边  0—1 容量为2 花费为0   n—n+1 容量为2 花费为0   跑一边最小费用就是答案了

     锦囊:

    以下板子求的是最小费用最大流 如果要固定流量k 可以在增广的时候检查一下 在flow+a>=k 的时候只增广k-flow单位的流量,然后终止程序。同样有费用限制也一样。

      1 #include<iostream>
      2 #include<stdio.h>
      3 #include<string.h>
      4 #include<stdlib.h>
      5 #include<vector>
      6 #include<queue>
      7 using namespace std;
      8 const int maxn=1e3+20,mod=1e9+7,inf=0x3f3f3f3f;
      9 struct edge
     10 {
     11     int to,next,cap,flow,cost;
     12 } edge[maxn*maxn];
     13 int head[maxn],tol;
     14 int pre[maxn],dis[maxn];
     15 bool vis[maxn];
     16 int N;
     17 void init(int n)
     18 {
     19     N=n,tol=0;
     20     memset(head,-1,sizeof(head));
     21 }
     22 void addedge(int u,int v,int cap,int cost)
     23 {
     24     edge[tol].to=v;
     25     edge[tol].cap=cap;
     26     edge[tol].flow=0;
     27     edge[tol].cost=cost;
     28     edge[tol].next=head[u];
     29     head[u]=tol++;
     30     edge[tol].to=u;
     31     edge[tol].cap=0;
     32     edge[tol].flow=0;
     33     edge[tol].cost=-cost;
     34     edge[tol].next=head[v];
     35     head[v]=tol++;
     36 }
     37 bool spfa(int s,int t)
     38 {
     39     queue<int> q;
     40     for(int i=0; i<=N; i++)
     41     {
     42         dis[i]=inf;
     43         vis[i]=false;
     44         pre[i]=-1;
     45     }
     46     dis[s]=0;
     47     vis[s]=true;
     48     q.push(s);
     49     while(!q.empty())
     50     {
     51         int u=q.front();
     52         q.pop();
     53         vis[u]=false;
     54         for(int i=head[u]; i!=-1; i=edge[i].next)
     55         {
     56             int v=edge[i].to;
     57             if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
     58             {
     59                 dis[v]=dis[u]+edge[i].cost;
     60                 pre[v]=i;
     61                 if(!vis[v])
     62                 {
     63                     vis[v]=true;
     64                     q.push(v);
     65                 }
     66             }
     67         }
     68     }
     69     if(pre[t]==-1) return false;
     70     else return true;
     71 }
     72 int mincostflow(int s,int t,int &cost)
     73 {
     74     int flow=0;
     75     cost=0;
     76     while(spfa(s,t))
     77     {
     78         int Min=inf;
     79         for(int i=pre[t]; i!=-1; i=pre[edge[i^1].to]) //更新流量
     80         {
     81             if(Min>edge[i].cap-edge[i].flow)
     82                 Min=edge[i].cap-edge[i].flow;
     83         }
     84         for(int i=pre[t]; i!=-1; i=pre[edge[i^1].to])
     85         {
     86             edge[i].flow+=Min;
     87             edge[i^1].flow-=Min;
     88             cost+=edge[i].cost*Min;
     89         }
     90         flow+=Min;
     91        // if(flow>=f)break;    //达到想要的f直接弹出来
     92     }
     93     return flow;
     94 }
     95 int main()
     96 {
     97     int n,m;
     98     while(~scanf("%d%d",&n,&m))
     99     {
    100         init(n+1);
    101         for(int i=0; i<m; i++)
    102         {
    103             int u,v,cap,cost;
    104             scanf("%d%d%d",&u,&v,&cost);
    105             addedge(u,v,1,cost);
    106             addedge(v,u,1,cost);
    107         }
    108         addedge(0,1,2,0);
    109         addedge(n,n+1,2,0);
    110         int ans,maxflow;
    111         maxflow=mincostflow(0,n+1,ans);
    112         printf("%d
    ",ans);
    113     }
    114 }
  • 相关阅读:
    【转】36个经典的JQuery导航菜单演示+下载
    【转】ASP.NET 3.5 开发范例精讲精析读书笔记
    【转】js弹出框详解
    【转】谈谈三层架构中MODEL的作用
    【转】制作.net实体类生成器(1)
    ASP.NET开发实战宝典.pdf配套视频源码iso| 开发实战宝典pdf和配套视频源码iso
    【转】if (!IsPostBack)
    jquery
    取web.config 连接字符串
    js接收传递过来的参数
  • 原文地址:https://www.cnblogs.com/stranger-/p/9350515.html
Copyright © 2020-2023  润新知