zoukankan      html  css  js  c++  java
  • poj 2135: Farm Tour

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14059   Accepted: 5354

    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

    题解:

      最小费用最大流模板直接上,注意是双向边。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<cstring>
     7 #include<queue>
     8 #include<vector>
     9 using namespace std;
    10 const int inf=1e9;
    11 const int maxn=2000,maxm=3000000;
    12 struct node{
    13     int to,rest,next,cost;
    14 }e[maxm];
    15 int head[maxn],cnt=1;
    16 inline void addedge(int x,int y,int z,int c){
    17     e[++cnt].to=y; e[cnt].rest=z; e[cnt].next=head[x]; head[x]=cnt; e[cnt].cost= c;
    18     e[++cnt].to=x; e[cnt].rest=0; e[cnt].next=head[y]; head[y]=cnt; e[cnt].cost=-c;    
    19 }
    20 int N,M,S,T;
    21 int dis[maxn],pre[maxn];
    22 bool vis[maxn];
    23 int maxflow,mincost;
    24 bool SPFA(){
    25     static queue<int> Q;
    26     for(int i=S;i<=T;i++) dis[i]=inf,vis[i]=false;
    27     Q.push(S); dis[S]=0; vis[S]=true;
    28     while(!Q.empty()){
    29         int x=Q.front(); Q.pop();
    30         vis[x]=false;
    31         for(int i=head[x];i;i=e[i].next){
    32             int y=e[i].to;
    33             if(e[i].rest&&dis[y]>dis[x]+e[i].cost){
    34                 dis[y]=dis[x]+e[i].cost;
    35                 pre[y]=i;
    36                 if(vis[y]==false){
    37                     vis[y]=true;
    38                     Q.push(y);
    39                 }
    40             }
    41         }
    42     }
    43     return dis[T]<inf;
    44 }
    45 void update(){
    46     int flow=inf;
    47     for(int i=pre[T];i;i=pre[e[i^1].to])
    48         flow=min(flow,e[i].rest);
    49     for(int i=pre[T];i;i=pre[e[i^1].to]){
    50         e[i].rest-=flow;
    51         e[i^1].rest+=flow;
    52     }
    53     maxflow+=flow; mincost+=flow*dis[T];
    54 }
    55 void MCF(){
    56     maxflow=mincost=0;
    57     while(SPFA()) update();
    58 }
    59 int main(){
    60     scanf("%d%d",&N,&M);
    61     S=0; T=N+1;
    62     addedge(S,1,2,0);
    63     for(int i=1,u,v,c;i<=M;i++){
    64         scanf("%d%d%d",&u,&v,&c);
    65         addedge(u,v,1,c);
    66         addedge(v,u,1,c);
    67     }
    68     addedge(N,T,2,0);
    69     MCF();
    70     printf("%d",mincost);
    71     return 0;
    72 }

     

     

  • 相关阅读:
    VS2013 此模板尝试加载组件程序集”NuGet.VisualStudio.interop,Version=1.0.0.0 的解决办法
    备份集中的数据库备份与现有的xx数据库不同”解决方法
    Couchbase 集群小实践
    EF5+MVC4系列(1) Podwerdesigner15.1设计数据库;PD中间表和EF实体模型设计器生成中间表的区别;EF5.0 表关系插入数据(一对多,多对多)
    Vue个人笔记
    [转载] HashMap的工作原理-hashcode和equals的区别
    用VS Code写C#
    MongoDB学习笔记一:MongoDB基础
    Vue学习笔记十三:Vue+Bootstrap+vue-resource从接口获取数据库数据
    Vue学习笔记十二:vue-resource的基本使用
  • 原文地址:https://www.cnblogs.com/CXCXCXC/p/5207954.html
Copyright © 2011-2022 走看看