HDU 1853 Cyclic Tour【最小费用最大流 OR 带权二分图匹配】

4 篇文章 0 订阅
4 篇文章 0 订阅

HDU 1853 Cyclic Tour【最小费用最大流 OR 带权二分图匹配】http://acm.hdu.edu.cn/showproblem.php?pid=1853

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
Hint
In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
 

Author
RoBa@TJU
 

Source
 
【题意】有n个点,m 条有向边,将该n个点分成若干环,最少含两个点;输出环边距的最小和,没有则输出-1。
【思路】可用最小费用最大流做,我还没研究费用流,过几天再做;
还可用二分图匹配做,求最小匹配,副权值。
【代码如下】
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

int n,m;
const int maxn = 200;
const int INF = 0xffffff;

int map[maxn][maxn], lx[maxn], ly[maxn], match[maxn];
int visx[maxn], visy[maxn], slack[maxn];

bool dfs(int x)
{
    visx[x] = true;
    for(int y=1; y<=n; y++)
    {
        if(visy[y]) continue;
        int t = lx[x]+ly[y]-map[x][y];
        if(t==0)
        {
            visy[y] = true;
            if(match[y]==-1 || dfs(match[y]))
            {
                match[y] = x;
                return true;
            }
        }
        else
            slack[y] = min(slack[y], t);
    }
    return false;
}

int km()
{
    memset(ly, 0, sizeof(ly));
    memset(match, -1, sizeof(match));
    for(int i=1; i<=n; i++){
        lx[i] = -INF;
        for(int j=1; j<=n; j++)
            lx[i] = max(map[i][j], lx[i]);
    }

    for(int x=1; x<=n; x++)
    {
        for(int i=1; i<=n; i++) slack[i] = INF;
        while(1)
        {
            memset(visx, 0, sizeof(visx));
            memset(visy, 0, sizeof(visy));
            if(dfs(x))
                break;
            int d = INF;
            for(int i=1; i<=n; i++)
            {
                if(!visy[i] && d>slack[i])
                    d = slack[i];
            }
            for(int i=1; i<=n; i++)
            {
                if(visx[i]) lx[i] -= d;
                if(visy[i]) ly[i] += d;
                else slack[i] -= d;
            }
        }
    }
    int ans = 0;
    for(int i=1; i<=n; i++)
    {
        if(match[i] == -1 || map[ match[i] ][i] == -INF)
            return 1;
        else
            ans += map[ match[i] ][i];
    }
    return ans;
}

int main()
{
    while(scanf("%d %d",&n, &m) == 2)
    {
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                map[i][j] = -INF;
        for(int i=1; i<=m; i++)
        {
            int a,b,c;
            scanf("%d %d %d", &a, &b, &c);
            if(-c > map[a][b])
                map[a][b] = -c;
        }
        printf("%d\n", -km());
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值