• UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)


    UVA - 1349          

                                       Optimal Bus Route Design

    Time Limit: 3000MS

     

    Memory Limit: Unknown

     

    64bit IO Format: %lld & %llu

     

    Description

    A big city wants to improve its bus transportation system. One of the improvement is to add scenic routes which go es through attractive places. Your task is to construct a bus-route-plan for sight-seeing buses in a city.

    You are given a set of scenic lo cations. For each of these given lo cations, there should be only one bus route that passes this lo cation, and that bus route should pass this lo cation exactly once. The number of bus routes is unlimited. However, each route should contain at least two scenic lo cations.

    From location i to location j , there may or may not be a connecting street. If there is a street from location i to location j , then we say j is an out-neighbor of i . The length of the street from i to j is d (i, j) . The streets might be one way. So it may happen that there is a street from i to j , but no street from j to i . In case there is a street from i to j and also a street from j to i , the lengths d (i, j) and d (j, i) might be different. The route of each bus must follow the connecting streets and must be a cycle. For example, the route of Bus A might be from location 1 to location 2, from location 2 to location 3, and then from location 3 to location 1. The route of Bus B might be from location 4 to location 5, then from location 5 to location 4. The length of a bus route is the sum of the lengths of the streets in this bus route. The total length of the bus-route-plan is the sum of the lengths of all the bus routes used in the plan. A bus-route-plan is optimal if it has the minimum total length. You are required to compute the total length of an optimal bus-route-plan.

    Input 

    The input file consists of a number of test cases. The first line of each test case is a positive integer n , which is the number of locations. These n locations are denoted by positive integers 1, 2,..., n . The next n lines are information about connecting streets between these lo cations. The i -th line of these n lines consists of an even number of positive integers and a 0 at the end. The first integer is a lo cation j which is an out-neighbor of location i , and the second integer is d (i, j) . The third integer is another location j' which is an out-neighbor of i , and the fourth integer is d (i, j') , and so on. In general, the (2k - 1) th integer is a location t which is an out-neighbor of location i , and the 2k th integer is d (i, t) .

    The next case starts immediately after these n lines. A line consisting of a single ` 0' indicates the end of the input file.

    Each test case has at most 99 locations. The length of each street is a positive integer less than 100.

    Output 

    The output contains one line for each test case. If the required bus-route-plan exists, then the output is a positive number, which is the total length of an optimal bus-route-plan. Otherwise, the output is a letter `N'.

    Sample Input 

    3

    2 2 3 1 0

    1 1 3 2 0

    1 3 2 7 0 

    8

    2 3 3 1 0

    3 3 1 1 4 4 0

    1 2 2 7 0

    5 4 6 7 0

    4 4 3 9 0

    7 4 8 5 0

    6 2 5 8 8 1 0

    6 6 7 2 0

    3

    2 1 0

    3 1 0

    2 1 0

    0

    Sample Output 

    7

    25

    N

    【思路】

           二分图最佳完美匹配。

           “只要每个点有唯一的后继,每个点恰好属于一个圈”。拆点后问题转化为二分图的最佳完美匹配问题。

    【代码】

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 #include<vector>
     5 #include<iostream>
     6 #define FOR(a,b,c) for(int a=(b);a<(c);a++)
     7 using namespace std;
     8 
     9 const int maxn = 400+10;
    10 const int INF = 1e9;
    11 
    12 struct Edge{ int u,v,cap,flow,cost;
    13 };
    14 
    15 struct MCMF {
    16     int n,m,s,t;
    17     int inq[maxn],a[maxn],d[maxn],p[maxn];
    18     vector<int> G[maxn];
    19     vector<Edge> es;
    20     
    21     void init(int n) {
    22         this->n=n;
    23         es.clear();
    24         for(int i=0;i<n;i++) G[i].clear();
    25     }
    26     void AddEdge(int u,int v,int cap,int cost) {
    27         es.push_back((Edge){u,v,cap,0,cost});
    28         es.push_back((Edge){v,u,0,0,-cost});
    29         m=es.size();
    30         G[u].push_back(m-2);
    31         G[v].push_back(m-1);
    32     }
    33     
    34     bool SPFA(int s,int t,int& flow,int& cost) {
    35         for(int i=0;i<n;i++) d[i]=INF;
    36         memset(inq,0,sizeof(inq));
    37         d[s]=0; inq[s]=1; p[s]=0; a[s]=INF; 
    38         queue<int> q; q.push(s);
    39         while(!q.empty()) {
    40             int u=q.front(); q.pop(); inq[u]=0;
    41             for(int i=0;i<G[u].size();i++) {
    42                 Edge& e=es[G[u][i]];
    43                 int v=e.v;
    44                 if(e.cap>e.flow && d[v]>d[u]+e.cost) {
    45                     d[v]=d[u]+e.cost;
    46                     p[v]=G[u][i];
    47                     a[v]=min(a[u],e.cap-e.flow);        //min(a[u],..)
    48                     if(!inq[v]) { inq[v]=1; q.push(v);
    49                     }
    50                 }
    51             }
    52         }
    53         if(d[t]==INF) return false;
    54         flow+=a[t] , cost+=a[t]*d[t];
    55         for(int x=t; x!=s; x=es[p[x]].u) {
    56             es[p[x]].flow+=a[t]; es[p[x]^1].flow-=a[t];
    57         }
    58         return true;
    59     }
    60     int Mincost(int s,int t,int& cost) {
    61         int flow=0; cost=0;
    62         while(SPFA(s,t,flow,cost)) ;
    63         return flow;
    64     }
    65 } mc;
    66 
    67 int n;
    68 
    69 int main() {
    70     while(scanf("%d",&n)==1 && n) {
    71         mc.init(n+n+2);
    72         int s=n+n,t=s+1;
    73         int u,v,w;
    74         FOR(u,0,n) {
    75             while(scanf("%d",&v)==1 && v) {
    76                 scanf("%d",&w);
    77                 v--;
    78                 mc.AddEdge(u,n+v,1,w);
    79             }
    80             mc.AddEdge(s,u,1,0); mc.AddEdge(u+n,t,1,0);
    81         }
    82         int flow,cost;
    83         flow=mc.Mincost(s,t,cost);
    84         if(flow<n) printf("N
    ");
    85         else printf("%d
    ",cost);
    86     }
    87     return 0;
    88 }
  • 相关阅读:
    tzselect
    tzfile
    ttytype
    tty
    TRUNCATE
    true
    troff
    touch
    Open vSwitch
    Web 在线文件管理器学习笔记与总结(5)修改文件内容
  • 原文地址:https://www.cnblogs.com/lidaxin/p/5060308.html
Copyright © 2020-2023  润新知