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 }

     

     

  • 相关阅读:
    Vue的响应式和双向绑定的实现
    JS-跨域请求豆瓣API提供的数据
    豆瓣电影API接口
    JS/PHP-表单文件域上传文件和PHP后台处理
    jQuery-attr,prop及自定义属性
    PHP-关于php代码和html,js混编
    JS-Chrome控制台console.log会访问即时数据
    JS-time和timeEnd
    JS-用ID值直接操作DOM
    CSS-07 行内设置点击事件
  • 原文地址:https://www.cnblogs.com/CXCXCXC/p/5207954.html
Copyright © 2011-2022 走看看