zoukankan      html  css  js  c++  java
  • Tour HDU

    Tour

    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. 

    InputAn 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.OutputFor 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

    The route should contain one or more loops.

    一个或多个环。。二分匹配足以。。  把权值取反 然后套最大权值匹配即可

    注意有重边。。但我们是要最小值  取反后在输入的时候只保留max的值即可

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <cmath>
    #define mem(a, b) memset(a, b, sizeof(a))
    using namespace std;
    const int maxn = 220, INF = 0x7fffffff;
    int usedx[maxn], usedy[maxn], cy[maxn], cx[maxn], w[maxn][maxn], bx[maxn], by[maxn];
    int nx, ny, n, minn, min_val, m;
    int dfs(int u)
    {
        usedx[u] =1 ;
        for(int i=1; i<=ny; i++)
        {
            if(usedy[i] == -1)
            {
                int t = bx[u] + by[i] - w[u][i];
                if(t == 0)
                {
                    usedy[i] = 1;
                    if(cy[i] == -1 || dfs(cy[i]))
                    {
                        cy[i] = u;
                        cx[u] = i;
                        return 1;
                    }
                }
                else if(t > 0)
                    minn = min(minn, t);
            }
        }
        return 0;
    }
    
    void km()
    {
        mem(cy, -1);
        mem(cx, -1);
        for(int i=0; i<=nx; i++) bx[i] = -INF;
        mem(by, 0);
        for(int i=1; i<=nx; i++)
            for(int j=1; j<=ny; j++)
                    bx[i] = max(bx[i], w[i][j]);
        for(int i=1; i<=nx; i++)
        {
            while(1)
            {
                minn = INF;
                mem(usedx, -1);
                mem(usedy, -1);
                if(dfs(i)) break;
                for(int j=1; j<=nx; j++)
                    if(usedx[j] != -1) bx[j] -= minn;
                for(int j=1; j<=ny; j++)
                    if(usedy[j] != -1) by[j] += minn;
            }
        }
        min_val = 0;
        for(int i=1; i<=nx; i++)
            if(cx[i] != -1)
                min_val += w[i][cx[i]];
        printf("%d
    ",-min_val);
    }
    
    
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            for(int i=1; i<=n; i++)
                for(int j=1; j<=n; j++) w[i][j] = -INF;
            for(int i=1; i<=m; i++)
            {
                int u, v, c;
                scanf("%d%d%d",&u,&v,&c);
                    w[u][v] = max(w[u][v], -c);
            }
            nx = ny = n;
            km();
        }
        return 0;
    }
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    Configuration Error <providerOption name="CompilerVersion" value="v3.5"/>
    destoon3.0 用户模板首页应用新模板无效不能立即刷新的解决方法
    改变kingcms默认拼音路径格式/修改kingcms拼音路径
    VB正则表达式
    kingcms 友情链接中 设置为推荐 取消推荐 设置为头条 取消头条,以及设置为up的bug
    kingcms5部分BUG修复和修改技巧
    SqlServer 函数 大全
    图文讲解NTFS和FAT32硬盘下 asp.net 生成word 错误: 80070005 和 错误:8000401a 的解决方法
    destoon3.0 后台公司模板 安装新模板提示“CSS文件不存在”及其解决方法
    asp.net报错:“System.NullReferenceException: 未将对象引用设置到对象的实例”
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9311819.html
Copyright © 2011-2022 走看看