zoukankan      html  css  js  c++  java
  • TZOJ 1513 Farm Tour(最小费用最大流)

    描述

    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.

    输入

    * 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.

    输出

    A single line containing the length of the shortest tour.

    样例输入

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

    样例输出

    6

    题意

    N个点M条路,M行每行u,v,w,计算从1到N回到1,所有边只能走一次求最短路,保证有解

    题解

    每条边流量为1,花费为w,设源点S=1,汇点T=n+1流量为2,花费为0

    求S到T的最小费用最大流

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int N=1e4+5;
     5 const int M=1e5+5;
     6 const int INF=0x3f3f3f3f;
     7 
     8 int FIR[N],TO[M],CAP[M],FLOW[M],COST[M],NEXT[M],tote;
     9 int pre[N],dist[N],q[400000];
    10 bool vis[N];
    11 int n,m,S,T;
    12 void init()
    13 {
    14     tote=0;
    15     memset(FIR,-1,sizeof(FIR));
    16 }
    17 void add(int u,int v,int cap,int cost)
    18 {
    19     TO[tote]=v;
    20     CAP[tote]=cap;
    21     FLOW[tote]=0;
    22     COST[tote]=cost;
    23     NEXT[tote]=FIR[u];
    24     FIR[u]=tote++;
    25 
    26     TO[tote]=u;
    27     CAP[tote]=0;
    28     FLOW[tote]=0;
    29     COST[tote]=-cost;
    30     NEXT[tote]=FIR[v];
    31     FIR[v]=tote++;
    32 }
    33 bool SPFA(int s, int t)
    34 {
    35     memset(dist,INF,sizeof(dist));
    36     memset(vis,false,sizeof(vis));
    37     memset(pre,-1,sizeof(pre));
    38     dist[s] = 0;vis[s]=true;q[1]=s;
    39     int head=0,tail=1;
    40     while(head!=tail)
    41     {
    42         int u=q[++head];vis[u]=false;
    43         for(int v=FIR[u];v!=-1;v=NEXT[v])
    44         {
    45             if(dist[TO[v]]>dist[u]+COST[v]&&CAP[v]>FLOW[v])
    46             {
    47                 dist[TO[v]]=dist[u]+COST[v];
    48                 pre[TO[v]]=v;
    49                 if(!vis[TO[v]])
    50                 {
    51                     vis[TO[v]] = true;
    52                     q[++tail]=TO[v];
    53                 }
    54             }
    55         }
    56     }
    57     return pre[t]!=-1;
    58 }
    59 void MCMF(int s, int t, int &cost, int &flow)
    60 {
    61     flow=cost=0;
    62     while(SPFA(s,t))
    63     {
    64         int Min=INF;
    65         for(int v=pre[t];v!=-1;v=pre[TO[v^1]])
    66             Min=min(Min, CAP[v]-FLOW[v]);
    67         for(int v=pre[t];v!=-1;v=pre[TO[v^1]])
    68         {
    69             FLOW[v]+=Min;FLOW[v^1]-=Min;
    70             cost+=COST[v]*Min;
    71         }
    72         flow+=Min;
    73     }
    74 }
    75 int main()
    76 {
    77     while(scanf("%d%d",&n,&m)!=EOF)
    78     {
    79         init();
    80         for(int i=0,u,v,w;i<m;i++)
    81         {
    82             scanf("%d%d%d",&u,&v,&w);
    83             add(u,v,1,w);
    84             add(v,u,1,w);
    85         }
    86         S=1,T=n+1;
    87         add(n,T,2,0);
    88         int cost,flow;
    89         MCMF(S,T,cost,flow);
    90         printf("%d
    ",cost);
    91     }
    92     return 0;
    93 }
  • 相关阅读:
    Centos7.3防火墙配置
    Centos7使用yum安装MySQL5.6的正确姿势
    Connect C# to MySQL
    ADO.NET操作MySQL数据库
    Mac OS 上 VIM 8.0 安装体验
    WebSocket实战之————Workerman服务器的安装启动
    C语言实现文件复制功能(包括文本文件和二进制文件)
    Gateway/Worker模型 数据库使用示例
    [转]Using the Group Pane to Repeat Page Titles
    [转]学习 WCF (6)--学习调用WCF服务的各种方法
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9526105.html
Copyright © 2011-2022 走看看