zoukankan      html  css  js  c++  java
  • Hdu 1853 Cyclic Tour

    Cyclic Tour

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
    Total Submission(s): 2674    Accepted Submission(s): 1371

    Problem Description

    There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?

    Input

    There are several test cases in the input. You should process to the end of file (EOF).
    The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).

    Output

    Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1.

    Sample Input

    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

    6 5

    1 2 1

    2 3 1

    3 4 1

    4 5 1

    5 6 1

    Sample Output

    42

    -1

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<climits>
    using namespace std;
    #define N 505
    #define MAXN 1<<28int map[N][N];
    int lx[N], ly[N];
    int slack[N];
    int match[N];
    bool visitx[N], visity[N];
    int n;
    
    bool Hungary(int u)
    {
        visitx[u] = true;
        for(int i = 1; i <= n; ++i)
        {
            if(visity[i])
                continue;
            else
            {
                if(lx[u] + ly[i] == map[u][i])
                {
                    visity[i] = true;
                    if(match[i] == -1 || Hungary(match[i]))
                    {
                        match[i] = u;
                        return true;
                    }
                }
                else
                    slack[i] = min(slack[i], lx[u] + ly[i] - map[u][i]);
            }
        }
        return false;
    }
    
    void KM_perfect_match()
    {
        int temp;
        for(int i = 1; i <= n; ++i)
            lx[i] = -MAXN;
        memset(ly, 0, sizeof(ly));
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                lx[i] = max(lx[i], map[i][j]);
        for(int i = 1; i <= n; ++i)
        {
            for(int j = 1; j <= n; ++j)
                slack[j] = MAXN;
            while(1)
            {
                memset(visitx, false, sizeof(visitx));
                memset(visity, false, sizeof(visity));
                if(Hungary(i))
                    break;
                else
                {
                    temp = MAXN;
                    for(int j = 1; j <= n; ++j)
                        if(!visity[j])
                            temp = min(temp, slack[j]);
                    for(int j = 1; j <= n; ++j)
                    {
                        if(visitx[j])
                            lx[j] -= temp;
                        if(visity[j])
                            ly[j] += temp;
                        else
                            slack[j] -= temp;
                    }
                }
            }
        }
    }
    
    int main()
    {
        int m;
        int a, b, cost;
        int ans;
        bool flag;
        while(scanf("%d%d", &n, &m) != EOF)
        {
            ans = 0;
            flag = true;
            memset(match, -1, sizeof(match));
            for(int i = 1; i <= n; ++i)
                for(int j = 1; j <= n; ++j)
                    map[i][j] = -MAXN;
            for(int i = 1; i <= m; ++i)
            {
                scanf("%d%d%d", &a, &b, &cost); //防止有重边。取较小值(负数实现)            if(-cost > map[a][b])
                    map[a][b] = -cost;
            }
            KM_perfect_match();
            for(int i = 1; i <= n; ++i) //是否有完美匹配        {
                if(match[i] == -1 || map[ match[i] ][i] == -MAXN)
                {
                    flag = false;
                    break;
                }
                ans += map[match[i]][i];
            }
            if(flag)
                printf("%d
    ", -ans);
            else
                printf("-1
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    Python命令行下退格、删除、方向键乱码问题解决(亲测有效)
    提取MySQL binlog中指定表的操作记录
    windows下面安装Python和pip终极教程
    day1-作业2
    day1-作业
    centos6.5python2.6环境下安装ansible
    安装inotify-tools,用inotifywait命令监听文件或目录的访问信息(自动同步文件夹)
    GitHub开源MySQL Online DDL工具gh-ost参数解析
    GitHub开源MySQL Online DDL工具gh-ost安装文档
    mysql磁盘IO%util 居高不下之RAID卡 BBU Learn Cycle周期
  • 原文地址:https://www.cnblogs.com/zhangliu/p/7058051.html
Copyright © 2011-2022 走看看