zoukankan      html  css  js  c++  java
  • POJ 2135

    Farm Tour
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10840   Accepted: 4011

    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
    

    Source

     
    最小费用流
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <queue>
     7 
     8 using namespace std;
     9 
    10 const int MAX_N = 1005;
    11 const int INF = 1e9;
    12 struct Edge {int from, to, cap, flow, cost; };
    13 int N,M;
    14 vector<Edge> edges;
    15 vector<int> G[MAX_N];
    16 int dist[MAX_N],prevv[MAX_N],preve[MAX_N];
    17 bool inq[MAX_N];
    18 
    19 void add_edge(int from, int to, int cap, int cost) {
    20         edges.push_back(Edge {from, to, cap, 0, cost});
    21         edges.push_back(Edge {to, from, 0, 0, -cost});
    22         int m = edges.size();
    23         G[from].push_back(m - 2);
    24         G[to].push_back(m - 1);
    25 }
    26 
    27 int min_cost_flow(int s, int t, int f) {
    28         int res = 0;
    29         while(f > 0) {
    30                 fill(dist, dist + N, INF);
    31                 dist[s] = 0;
    32                 queue<int> q;
    33                 q.push(s);
    34 
    35                 while(!q.empty()) {
    36                         int u = q.front(); q.pop();
    37                         inq[u] = 0;
    38                         for(int i = 0; i < G[u].size(); ++i) {
    39                                 Edge& e = edges[ G[u][i] ];
    40                                 if(e.cap > e.flow && dist[e.to] > dist[u] + e.cost) {
    41                                         dist[e.to] = dist[u] + e.cost;
    42                                         prevv[e.to] = u;
    43                                         preve[e.to] = i;
    44                                         if(!inq[e.to]) {
    45                                                 inq[e.to] = 1;
    46                                                 q.push(e.to);
    47                                         }
    48                                 }
    49                         }
    50 
    51                 }
    52                 if(dist[t] == INF) {
    53                         return -1;
    54                 }
    55 
    56         int d = f;
    57         for (int v = t; v != s; v = prevv[v]) {
    58                 Edge& e = edges[ G[ prevv[v] ][preve[v]] ];
    59                 d = min(d,e.cap - e.flow);
    60         }
    61 
    62         //printf("d = %d dis = %d
    ",d,dist[t]);
    63         f -= d;
    64         res += d * dist[t];
    65 
    66         for (int v = t; v != s; v = prevv[v]) {
    67                 Edge& e = edges[ G[prevv[v]][ preve[v] ] ];
    68                 e.flow += d;
    69                 edges[ G[prevv[v]][ preve[v] ] ^ 1].flow -= d;
    70         }
    71 
    72         }
    73 
    74         return res;
    75 }
    76 
    77 
    78 int main()
    79 {
    80     //freopen("sw.in","r",stdin);
    81 
    82     scanf("%d%d",&N,&M);
    83     for(int i = 1; i <= M; ++i) {
    84             int a,b,cost;
    85             scanf("%d%d%d",&a,&b,&cost);
    86             add_edge(a - 1,b - 1,1,cost);
    87             add_edge(b - 1,a - 1,1,cost);
    88     }
    89 
    90     printf("%d
    ",min_cost_flow(0,N - 1,2));
    91     //cout << "Hello world!" << endl;
    92     return 0;
    93 }
    View Code
  • 相关阅读:
    VMWare虚拟机15.X局域网网络配置(修改网卡)
    Redis的中并发问题的解决方案小结
    搭建一个dubbo+zookeeper平台
    react与redux学习资料的整理
    如何利用python+Selenium对登录的验证码进行验证?
    (基础篇)哪些面试遇见的坑——面试必看基础题
    测试员小白必经之路----常见的linux操作命令
    测试员小白必经之路----常见的Dos命令
    Elastcisearch.Nest 7.x 系列`伪`官方翻译:通过 NEST 来快捷使用 Elasticsearch
    StackExchange.Redis 系列 1:基础使用
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3713284.html
Copyright © 2011-2022 走看看