zoukankan      html  css  js  c++  java
  • POJ-2135-Farm Tour(最大费用最小流)模板

    Farm Tour POJ - 2135 

    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

    题意很简单,说思路;

    设源点为s,汇点为t;

    然后建图的时候,s->1   容量为2,费用为0;

    n->t,容量为2,费用为0;

    这样就能从s出发,到t,然后再走不重复的路径的再回到s;

    由于s->1,和n->t这两条路径费用为0,所以不影响最后结果。

    这样直接跑一边最大费用最小流的模板就行了。

    详见代码:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <queue>
      7 using namespace std;
      8 typedef long long ll;
      9 const int MAXX=40010;
     10 const int INF=0x3f3f3f3f;
     11 
     12 struct node
     13 {
     14     int to;
     15     int next;
     16     int cap;
     17     int flow;
     18     int cost;
     19 }edge[MAXX];
     20 
     21 int head[MAXX],tol;
     22 int pre[MAXX],dis[MAXX];
     23 bool vis[MAXX];
     24 int N;
     25 
     26 void init(int n)
     27 {
     28     N=n;
     29     tol=0;
     30     memset(head,-1,sizeof(head));
     31 }
     32 
     33 void addedge(int u,int v,int cap,int cost)
     34 {
     35     edge[tol].to=v;
     36     edge[tol].cap=cap;
     37     edge[tol].cost=cost;
     38     edge[tol].flow=0;
     39     edge[tol].next=head[u];
     40     head[u]=tol++;
     41     edge[tol].to=u;
     42     edge[tol].cap=0;
     43     edge[tol].cost=-cost;
     44     edge[tol].flow=0;
     45     edge[tol].next=head[v];
     46     head[v]=tol++;
     47 }
     48 
     49 bool SPFA(int s,int t)
     50 {
     51     queue<int> q;
     52     for(int i=0;i<N;i++)
     53     {
     54         dis[i]=INF;
     55         vis[i]=0;
     56         pre[i]=-1;
     57     }
     58     dis[s]=0;
     59     vis[s]=1;
     60     q.push(s);
     61     while(!q.empty())
     62     {
     63         int u=q.front();
     64         q.pop();
     65         vis[u]=0;
     66         for(int i=head[u];i!=-1;i=edge[i].next)
     67         {
     68             int v=edge[i].to;
     69             if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
     70             {
     71                 dis[v]=dis[u]+edge[i].cost;
     72                 pre[v]=i;
     73                 if(!vis[v])
     74                 {
     75                     vis[v]=1;
     76                     q.push(v);
     77                 }
     78             }
     79         }
     80     }
     81     if(pre[t]==-1)return 0;
     82     return 1;
     83 }
     84 
     85 int minCostMaxFlow(int s,int t)
     86 {
     87     int cost=0;
     88     while(SPFA(s,t))
     89     {
     90         int minn=INF;
     91         for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
     92         {
     93             if(minn>edge[i].cap-edge[i].flow)
     94                 minn=edge[i].cap-edge[i].flow;
     95         }
     96         for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
     97         {
     98             edge[i].flow+=minn;
     99             edge[i^1].flow-=minn;
    100             cost+=edge[i].cost*minn;
    101         }
    102     }
    103     return cost;
    104 }
    105 int main()
    106 {
    107 
    108     int n,m;
    109     while(~scanf("%d%d",&n,&m))
    110     {
    111         int u,v,g;
    112         init(n+2);
    113         for(int i=0;i<m;i++)
    114         {
    115 
    116             scanf("%d%d%d",&u,&v,&g);
    117             addedge(u,v,1,g);
    118             addedge(v,u,1,g);
    119         }
    120         addedge(0,1,2,0);
    121         addedge(n,n+1,2,0);
    122         printf("%d
    ",minCostMaxFlow(0,n+1));
    123     }
    124     return 0;
    125 }
  • 相关阅读:
    “Computer Management Snapin Launcher已停止工作”的解决方案
    IFrame与window对象(contentWindow)
    使用Emeditor转换编码(ShiftJS 到 UTF8)
    从注册表中删除程序,不要忘记这两个地方
    Visual Studio fatal error C1902: 程序数据库管理器不匹配;请检查安装
    一些TC内置的环境环境变量(注意字母必须大写,且只能在TC内用)
    使用WIN32 API CreateProcess()以无窗口方式创建DOS程序
    VC中DDX/DDV自定义
    javascript 一条语句实现随机数语句
    Emeditor
  • 原文地址:https://www.cnblogs.com/Cherry93/p/10022426.html
Copyright © 2011-2022 走看看