【二分图匹配入门专题1】M - Cyclic Tour hdu1853【km算法--判断自重边】

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?

InputThere 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). 
OutputOutput 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.

题意:输入n个顶点m条边,让这些顶点形成环,至少包括两个顶点.输出最小的完备匹配和,如果不存在,输出-1。

思路:把一个点一分为二,就形成了两个集合,求最小匹配,则将权值取负后求最大匹配,最后返回负值。如果图由多个有向环构成,则一定存在完备匹配,如果有权值最大的完备匹配存在,则一定是这个图的最优完备匹配的值,如果有任何一个匹配边是初始化的边权值,说明这个完备匹配不存在。

之前嫌弃题水,现在又写的头疼,今天一天到现在才出了3题,自己真的好菜啊,什么时候能像师父那样厉害就好了,感觉算法真的需要自己理解了才能够应对各种变形,就这道题就超时了

5遍,我怎么知道要判断重边啊~~~

#include<stdio.h>
#include<string.h>
#define N 210
#define INF 0x3f3f3f3f

int map[N][N];
int ans,d;
int w[N][N],lx[N],ly[N];
int linker[N],visx[N],visy[N];
int n,m,nx,ny;

int dfs(int x)
{
    int y,tmp;
    visx[x] = 1;
    for(y = 1; y <= ny; y ++)
    {
        if(!visy[y])
        {
            tmp = lx[x] + ly[y] - w[x][y];
            if(!tmp)
            {
                visy[y] = 1;
                if(linker[y]==-1||dfs(linker[y]))
                {
                    linker[y] = x;
                    return 1;
                }
            }
            else if( d > tmp)
                d = tmp;
        }
    }
    return 0;
}

int KM()
{
    int x,y,i,j,sum;
    memset(linker,-1,sizeof(linker));
    memset(ly,0,sizeof(ly));
    for(x = 1; x <= nx; x ++)
        for(y = 1,lx[x] = -INF; y <= ny; y ++)
            if(lx[x] < w[x][y])
                lx[x] = w[x][y];

    for(x = 1; x <= nx; x ++)
    {
        while(1)
        {
            d = INF;
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            if(dfs(x))
                break;
            for(i = 1; i <= nx; i ++)
                if(visx[i])
                    lx[i] -= d;
            for(i = 1; i <= ny; i ++)
                if(visy[i])
                    ly[i] += d;
        }
    }
    sum = 0;
    for(i = 1; i <= ny; i ++)
    {
        if(w[linker[i]][i] != -INF)
            sum += w[linker[i]][i];
        else
            return -1;
    }
    return -sum;
}

int main()
{
    int t1,t2,t3;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        nx = ny = n;
        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 ++)
        {
            scanf("%d%d%d",&t1,&t2,&t3);
            if(-t3 > w[t1][t2])//重复输入取小值
                w[t1][t2] = -t3;
        }
        ans = KM();
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-11-11 21:37:42

【二分图匹配入门专题1】M - Cyclic Tour hdu1853【km算法--判断自重边】的相关文章

【二分图匹配入门专题1】D - Matrix hdu2119【最小顶点覆盖】

Give you a matrix(only contains 0 or 1),every time you can select a row or a column and delete all the '1' in this row or this column . Your task is to give out the minimum times of deleting all the '1' in the matrix. InputThere are several test case

【二分图匹配入门专题1】K - Going Home hdu1533【km匹配】

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, unt

【二分图匹配入门专题1】L - Card Game hdu 3722【km算法】

Jimmy invents an interesting card game. There are N cards, each of which contains a string Si. Jimmy wants to stick them into several circles, and each card belongs to one circle exactly. When sticking two cards, Jimmy will get a score. The score of

【二分图匹配入门专题1】I - Hiding Gold light oj 1152【二分图匹配】-------------------我是终于不那么水的水题分割线------------------------

You are given a 2D board where in some cells there are gold. You want to fill the board with 2 x 1 dominoes such that all gold are covered. You may use the dominoes vertically or horizontally and the dominoes may overlap. All you have to do is to cov

【二分图匹配入门专题1】E - Air Raid hdu1151【最小路径覆盖】

Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's

【二分图匹配入门专题1】B - Girls and Boys hdu1068【最大独立集】

the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find out the maximu

【二分图匹配入门专题1】H - Marriage Media light oj 1184【二分图最大匹配】

You run a marriage media. You take some profiles for men and women, and your task is to arrange as much marriages as you can. But after reading their bio-data you have found the following criteria. No man will marry a woman if their height gap is gre

【二分图匹配入门专题1】F - COURSES poj1469【最大匹配--匈牙利算法模板题】

Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions: every stude

【二分图匹配入门专题1】 N - Special Fish hdu3395 【km算法】【春风十里,都不如tle~~~】

There is a kind of special fish in the East Lake where is closed to campus of Wuhan University. It's hard to say which gender of those fish are, because every fish believes itself as a male, and it may attack one of some other fish who is believed to