zoukankan      html  css  js  c++  java
  • hdu 3488 Tour【KM】同hdu 1853

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3488

    我的链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=17728#overview

    Tour

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 1147    Accepted Submission(s): 604


    Problem Description
    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.
     

    Input
    An 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.
     

    Output
    For 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
     

    Source

    2010 ACM-ICPC Multi-University Training Contest6——Host by BIT

    Recommend

    Zhouzeyong

    题意:和hdu 1853几乎完全一样。

          给你一个带权值的有向图,用若干个环覆盖所有的点,每个点只可被覆盖一次(起点  

          除外),求出权值和最小的环的权值。

    算法:KM求完美匹配。

       以城市为点建立二分图,求最小权值二分图。

       如果有完美匹配,则一定存在若干包含每个顶点的环。

    PS:注意初始化w[i][j]为大负数的取值,否则可能超时。

    Accepted	424 KB	140 ms	C++	1943 B	2013-02-26 16:11:00
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=210;
    const int minn=-1000000;
    char map[maxn][maxn];
    int w[maxn][maxn];
    int lx[maxn],ly[maxn];  
    bool s[maxn],t[maxn]; 
    int match[maxn];
    int n;
    int hungary(int u)//匈牙利,匹配(找增广路) 
    {
    	s[u]=true;//标记入匈牙利树 
    	for(int v=1;v<=n;v++)
    	{
    		if(!t[v] && lx[u]+ly[v]==w[u][v])
    		{
    			t[v]=true;//标记入匈牙利树 易忘记 
    			if(match[v]==-1 || hungary(match[v]))
    			{
    				match[v]=u;
    				return true;
    			}
    		}
    	}
    	return false;
    }
    int KM()
    {
    	int sum=0;
    	memset(match,-1,sizeof(match));//初始化,易写错 
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=n;j++)
    			lx[i]=-1<<30;//初始化 顶标 
    	memset(ly,0,sizeof(ly));
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=n;j++)
    			lx[i]=max(lx[i],w[i][j]);//最大权 
    	for(int i=1;i<=n;i++)//匹配每一个点  
    	{
    		while(true)
    		{
    			memset(s,false,sizeof(s));//每次匹配前,找增广路,初始化为0 
    			memset(t,false,sizeof(t));
    			if(hungary(i)) break;
    			else{
    				int a=1<<30;
    				for(int j=1;j<=n;j++) if(s[j])//j匹配过(即在匈牙利树中) 
    					for(int k=1;k<=n;k++) if(!t[k] && a>lx[j]+ly[k]-w[j][k])
    						a=lx[j]+ly[k]-w[j][k];
    				for(int j=1;j<=n;j++)//修改顶标 
    				{
    					if(s[j]) lx[j]-=a;
    					if(t[j]) ly[j]+=a;
    				}
    			}
    		}
    	}
    	for(int i=1;i<=n;i++) 
    		sum+=w[match[i]][i]; 
    	return sum;
    }
    int main()
    {
    	int test;
    	int N,M;
    	int u,v,length;
    	while(scanf("%d",&test)!=EOF)
    	{
    		while(test--)
    		{
    			scanf("%d%d",&N,&M);
    			n=N;
    			for(int i=1;i<=N;i++)
    				for(int j=1;j<=N;j++)
    					w[i][j]=minn;
    			for(int i=1;i<=M;i++)
    			{
    				scanf("%d%d%d",&u,&v,&length);
    				if(-length>w[u][v]) w[u][v]=-length;
    			}
    			printf("%d\n",-KM());
    		}
    	}
    	return 0;
    }


  • 相关阅读:
    PHP---无限极分类数组处理
    PHPExcel数据导入(含图片)
    PHP数组与xml互相转换
    微信APP支付【签名失败】
    winform窗体关闭方案
    ss的优先级 和 权重
    Anaconda 与 conda 区别
    c#FileStream文件读写
    C# DataTable 某一列取算
    关于解决DevExpress用DevExpress patch工具破解后经常弹出试用框的问题
  • 原文地址:https://www.cnblogs.com/freezhan/p/2950438.html
Copyright © 2011-2022 走看看