hdu 1853 Cyclic Tour && hdu 3435 A new Graph Game(简单KM算法)

Cyclic Tour

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)

Total Submission(s): 1478    Accepted Submission(s): 750

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. 

这是1853代码,后一题建成双向边就行了,代码类似。

#include"stdio.h"
#include"string.h"
#define N 105
#define mmax(a,b) ((a)>(b)?(a):(b))
#define mmin(a,b) ((a)<(b)?(a):(b))
const int inf=(int)1e8;
int g[N][N],slack[N],n;
int link[N],lx[N],ly[N];
int visx[N],visy[N];
int find(int k)
{
    int i;
    visx[k]=1;
    for(i=1;i<=n;i++)
    {
        if(visy[i])
            continue;
        int d=lx[k]+ly[i]-g[k][i];
        if(d==0)
        {
            visy[i]=1;
            if(link[i]==-1||find(link[i]))
            {
                link[i]=k;
                return 1;
            }
        }
        else
            slack[i]=mmin(slack[i],d);
    }
    return 0;
}
int KM()
{
    int i,j;
    memset(ly,0,sizeof(ly));
    memset(link,-1,sizeof(link));
    for(i=1;i<=n;i++)
    {
        lx[i]=-inf;
        for(j=1;j<=n;j++)
        {
            lx[i]=mmax(lx[i],g[i][j]);
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            slack[j]=inf;
        }
        while(1)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            if(find(i))
                break;
            else
            {
                int d=inf;
                for(j=1;j<=n;j++)
                {
                    if(!visy[j])
                        d=mmin(slack[j],d);
                }
                for(j=1;j<=n;j++)
                {
                    if(visx[j])
                        lx[j]-=d;
                    if(visy[j])
                        ly[j]+=d;
                }
            }
        }
    }
    int ans=0;
    for(i=1;i<=n;i++)
    {
        if(link[i]==-1||g[link[i]][i]==-inf)
            break;
        ans+=g[link[i]][i];
    }
    if(i<=n)
        return -1;
    return -ans;
}
int main()
{
    int i,j,u,v,w,m;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        for(i=1;i<=n;i++)        //注意初始化边为最小值
        {
            for(j=1;j<=n;j++)
            {
                g[i][j]=-inf;
            }
        }
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            g[u][v]=mmax(g[u][v],-w);
        }
        printf("%d\n",KM());
    }
    return 0;
}

hdu 1853 Cyclic Tour && hdu 3435 A new Graph Game(简单KM算法)

时间: 2024-10-21 15:54:05

hdu 1853 Cyclic Tour && hdu 3435 A new Graph Game(简单KM算法)的相关文章

HDU 1853 Cyclic Tour(KM完美匹配)

HDU 1853 Cyclic Tour 题目链接 题意:一个有向图,边有权值,求把这个图分成几个环,每个点只能属于一个环,使得所有环的权值总和最小,求这个总和 思路:KM完美匹配,由于是环,所以每个点出度入度都是1,一个点拆成两个点,出点和入点,每个点只能用一次,这样就满足了二分图匹配,然后用KM完美匹配去就最小权值的匹配即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <

hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1904    Accepted Submission(s): 951 Problem Description There are N cities in our c

hdu 1853 Cyclic Tour 最大权值匹配 所有点连成环的最小边权和

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1904    Accepted Submission(s): 951 Problem Description There are N cities in our c

HDU 1853 Cyclic Tour(最小费用最大流)

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1879    Accepted Submission(s): 938 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L

hdu 1853 Cyclic Tour 最小费用最大流

题意:一个有向图,现在问将图中的每一个点都划分到一个环中的最少代价(边权和). 思路:拆点,建二分图,跑最小费用最大流即可.若最大流为n,则说明是最大匹配为n,所有点都参与,每个点的入度和出度又是1,所以就是环. /********************************************************* file name: hdu1853.cpp author : kereo create time: 2015年02月16日 星期一 17时38分51秒 *******

最大流增广路(KM算法) HDOJ 1853 Cyclic Tour

题目传送门 1 /* 2 KM: 相比HDOJ_1533,多了重边的处理,还有完美匹配的判定方法 3 */ 4 #include <cstdio> 5 #include <cmath> 6 #include <algorithm> 7 #include <cstring> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3f3f; 12 int x

hdoj 3488 Tour 【最小费用最大流】【KM算法】

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2299    Accepted Submission(s): 1151 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 3000

[ACM] HDU 2255 奔小康赚大钱 (二分图最大权匹配,KM算法)

奔小康赚大钱 Problem Description 传说在遥远的地方有一个很富裕的村落,有一天,村长决定进行制度改革:又一次分配房子. 这但是一件大事,关系到人民的住房问题啊. 村里共同拥有n间房间,刚好有n家老百姓,考虑到每家都要有房住(假设有老百姓没房子住的话.easy引起不安定因素),每家必须分配到一间房子且仅仅能得到一间房子. 还有一方面,村长和另外的村领导希望得到最大的效益,这样村里的机构才会有钱.因为老百姓都比較富裕,他们都能对每一间房子在他们的经济范围内出一定的价格,比方有3间房

Cyclic Tour (hdu 1853 二分图最小权问题)

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1883    Accepted Submission(s): 941 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L