• HDU3488 Tour —— 二分图最大权匹配 KM算法


    题目链接:https://vjudge.net/problem/HDU-3488

    Tour

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 3720    Accepted Submission(s): 1777


    Problem Description
    In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way roads connecting them. You are lucky enough to have a chance to have a tour in the kingdom. The route should be designed as: The route should contain one or more loops. (A loop is a route like: A->B->……->P->A.)
    Every city should be just in one route.
    A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.)
    The total distance the N roads you have chosen should be minimized.
     
    Input
    An integer T in the first line indicates the number of the test cases.
    In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W.
    It is guaranteed that at least one valid arrangement of the tour is existed.
    A blank line is followed after each test case.
     
    Output
    For each test case, output a line with exactly one integer, which is the minimum total distance.
     
    Sample Input
    1 6 9 1 2 5 2 3 5 3 1 10 3 4 12 4 1 8 4 6 11 5 4 7 5 6 9 6 5 4
     
    Sample Output
    42
     
    Source
     
    Recommend
    zhouzeyong

    题解:

    题意:有n座城市,m条带权有向边。有人想要游历所有城市,于是制定了计划:游历的路径是一个或者多个环,且所有城市都必须仅存在于一个环中。问怎样设计路线使得总路程最短?

    我们可以用最大权匹配去求:

    1.对于每个点u,我们拆成u和u'。u代表着出点,在二分图的左侧;u'代表着入点,在二分图的右侧。

    2.如果有一条有向边u-->v,那么在二分图中,我们加一条边u-->v',并且权值取反。

    3.利用KM()算法,求出最大权匹配,再将结果取反,即为答案。

    问:为何KM()算法求出来的就一定是一个或多个环呢?

    1.可知,题目已经说明了必定有解。那么对应的二分图必定存在完全匹配,完全匹配也必定是最大匹配。

    2.我们用KM()算法求出了最大权匹配。根据性质:最大权匹配必定为最大匹配。所以,如果最大匹配是完全匹配,那么最大权匹配也是完全匹配。

    3.因为最大权匹配是完全匹配。所以所求出的解必定是一个或多个环。

    代码如下:

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 typedef long long LL;
      4 const int INF = 0x3f3f3f3f;
      5 const LL LNF = 9e18;
      6 const int mod = 1e9+7;
      7 const int MAXN = 2e2+10;
      8 
      9 int nx, ny;
     10 int g[MAXN][MAXN];
     11 int linker[MAXN], lx[MAXN], ly[MAXN];
     12 int slack[MAXN];
     13 bool visx[MAXN], visy[MAXN];
     14 
     15 bool DFS(int x)
     16 {
     17     visx[x] = true;
     18     for(int y = 1; y<=ny; y++)
     19     {
     20         if(visy[y]) continue;
     21         int tmp = lx[x] + ly[y] - g[x][y];
     22         if(tmp==0)
     23         {
     24             visy[y] = true;
     25             if(linker[y]==-1 || DFS(linker[y]))
     26             {
     27                 linker[y] = x;
     28                 return true;
     29             }
     30         }
     31         else
     32             slack[y] = min(slack[y], tmp);
     33     }
     34     return false;
     35 }
     36 
     37 int KM()
     38 {
     39     memset(linker, -1, sizeof(linker));
     40     memset(ly, 0, sizeof(ly));
     41     for(int i = 1; i<=nx; i++)
     42     {
     43         lx[i] = -INF;
     44         for(int j = 1; j<=ny; j++)
     45             lx[i] = max(lx[i], g[i][j]);
     46     }
     47 
     48     for(int x = 1; x<=nx; x++)
     49     {
     50         for(int i = 1; i<=ny; i++)
     51             slack[i] = INF;
     52         while(true)
     53         {
     54             memset(visx, 0, sizeof(visx));
     55             memset(visy, 0, sizeof(visy));
     56 
     57             if(DFS(x)) break;
     58             int d = INF;
     59             for(int i = 1; i<=ny; i++)
     60                 if(!visy[i])
     61                     d = min(d, slack[i]);
     62 
     63             for(int i = 1; i<=nx; i++)
     64                 if(visx[i])
     65                     lx[i] -= d;
     66             for(int i = 1; i<=ny; i++)
     67             {
     68                 if(visy[i]) ly[i] += d;
     69                 else slack[i] -= d;
     70             }
     71         }
     72     }
     73 
     74     int res = 0;
     75     for(int i = 1; i<=ny; i++)
     76         if(linker[i]!=-1)
     77             res += g[linker[i]][i];
     78     return res;
     79 }
     80 
     81 int main()
     82 {
     83     int T, n, m;
     84     scanf("%d", &T);
     85     while(T--)
     86     {
     87         scanf("%d%d", &n,&m);
     88         nx = ny = n;
     89         memset(g, 0, sizeof(g));
     90         for(int i = 1; i<=nx; i++)
     91             for(int j = 1; j<=ny; j++)
     92                 g[i][j] = -INF;
     93         for(int i = 1; i<=m; i++)
     94         {
     95             int u, v, w;
     96             scanf("%d%d%d", &u, &v, &w);
     97             g[u][v] = max(g[u][v], -w);
     98         }
     99 
    100         printf("%d
    ", -KM());
    101     }
    102 }
    View Code
  • 相关阅读:
    python三级菜单
    python购物车
    crontab计划任务
    shell脚本相关关系、浮点、循环
    find及其他命令
    awk命令
    sed命令
    linux正则表达式
    shell脚本编写nginx部署脚本
    shell脚本编写监控内存并发送邮件
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7827846.html
Copyright © 2020-2023  润新知