HDU(1853),最小权匹配,KM
题目链接: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): 2289    Accepted Submission(s): 1162
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?
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).
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
-1
In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = ;
const int INF = 0x3f3f3f3f; int N,NX,NY;
int match[MAXN],lx[MAXN],ly[MAXN],slack[MAXN];
int visx[MAXN],visy[MAXN];
int Map[MAXN][MAXN]; bool FindPath(int u)
{
visx[u] = true;
for(int i = ; i <= NY; ++i)
{
if(visy[i])
continue;
int temp = lx[u] + ly[i] - Map[u][i];
if(temp == )
{
visy[i] = true;
if(match[i] == - || FindPath(match[i]))
{
match[i] = u;
return true;
}
}
else if(slack[i] > temp)
slack[i] = temp;
}
return false;
} int KM()
{
memset(ly,,sizeof(ly));
memset(lx,-,sizeof(lx));
memset(match,-,sizeof(match));
for(int i = ; i <= NX; ++i)
{
for(int j = ; j <= NY; ++j)
if(Map[i][j] > lx[i])
lx[i] = Map[i][j];
} for(int i = ; i <= NX; ++i)
{
for(int j = ; j <= NY; ++j)
slack[j] = INF;
while()
{
memset(visx,,sizeof(visx));
memset(visy,,sizeof(visy));
if(FindPath(i))
break;
int d = INF;
for(int j = ; j <= NY; ++j)
if(!visy[j] && d > slack[j])
d = slack[j];
for(int j = ; j <= NX; ++j)
if(visx[j])
lx[j] -= d;
for(int j = ; j <= NY; ++j)
if(visy[j])
ly[j] += d;
else
slack[j] -= d;
}
} int res = ;
int cnt = ;
for(int i = ; i <= NY; ++i)
if(match[i]!=-&&Map[match[i]][i]!=-INF)
{
res += Map[match[i]][i];
cnt++;
}
if(cnt<NY) return -;
return -res;
} int main()
{ int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
Map[i][j] = -INF;
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
Map[u][v] = max(Map[u][v],-w);
}
NX = NY = n;
printf("%d\n",KM());
}
return ;
}
HDU(1853),最小权匹配,KM的更多相关文章
- Poj(3686),最小权匹配,多重匹配,KM
		
题目链接 The Windy's | Time Limit: 5000MS | Memory Limit: 65536K | | Total Submissions: 4939 | Accepted: ...
 - poj 2195(KM求最小权匹配)
		
题目链接:http://poj.org/problem?id=2195 思路:我们都知道KM使用来求最大权匹配的,但如果要求最小权匹配,只需把图中的权值改为负值,求一次KM,然后权值和取反即可. ht ...
 - 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)
		
[POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS Memory Limit ...
 - 【POJ 2195】 Going Home(KM算法求最小权匹配)
		
[POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
 - Assignment (HDU 2853 最大权匹配KM)
		
Assignment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
 - POJ 3565 Ants (最小权匹配)
		
题意 给出一些蚂蚁的点,给出一些树的点,两两对应,使他们的连线不相交,输出一种方案. 思路 一开始没想到怎么用最小权匹配--后来发现是因为最小权匹配的方案一定不相交(三角形两边之和大于第三边)--还是 ...
 - UVALIVE 4970 最小权匹配
		
首先贴一下这道题的BNU地址,UVA地址自己找吧. http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=11852 题意:这道题的意思就是,给你N个棋子的 ...
 - POJ 2400 最小权匹配
		
吐槽:首先,这道题的输入居然是错的.要将上下两个矩阵的位置换一下才可以出样例,也就是上面那个矩阵是employee对Supervisor的打分,下面那个矩阵才是Supervisor对employee的 ...
 - 二分图带权匹配 KM算法与费用流模型建立
		
[二分图带权匹配与最佳匹配] 什么是二分图的带权匹配?二分图的带权匹配就是求出一个匹配集合,使得集合中边的权值之和最大或最小.而二分图的最佳匹配则一定为完备匹配,在此基础上,才要求匹配的边权值之和最大 ...
 
随机推荐
- log4net面面观之工作原理
			
转自:逗逼的博客:http://itrust.cnblogs.com/archive/2005/01/25/97225.html 要知道Log4net究竟是咋干活的,咱们可以从下面这个脉络简图入手.你 ...
 - codevs 1204 寻找子串位置
			
http://codevs.cn/problem/1204/ 1204 寻找子串位置 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题解 查看运行结果 ...
 - c#经典俄罗斯方块 vs2012开发
			
把网上两个开源的俄罗斯方块,整合到一起了,开发环境vs2012+.net 4.0,有问题.建议可以加群沟通哦 复古的 c#写的一个俄罗斯方块小游戏,友好的人机交互,具体功能如下: 1.游戏分七个关卡, ...
 - 1029 C语言文法
			
program -> external_declaration | program external_declaration <程序> -> <外部声明> ...
 - 夺命雷公狗---微信开发52----网页授权(oauth2.0)获取用户基本信息接口(2)
			
我们在上一节课已经发送code给第三方了,那么这就要获取code去换取到用户的openid. 第一步:编写create_baseurl.php(上一节课程已经写完了) 第二步:编写vote1.php( ...
 - Axis2、Axis1 以及其他接口的调用方式
			
在请求的时候出现问题,使用下面的方式请求就不会出现问题. package webservice.client.utils; import java.util.Iterator; import java ...
 - 链接库lib和dl的概念,加载方式的区别
			
使用LR进行基于windows socket协议做接口测试,只提供了lr_load_dll方法来动态加载动态链接库.之前学习阶段,对TinyXML的学习,使用的静态链接库,当时在程序调用的时候方法也跟 ...
 - div内的img下出现几像素的空白间距解决办法
			
现象描述: 解决方法: 1.设父元素的font-size:0; 2.设img为display:block; 3.设img为vertical-align:middle;
 - 必备的 Java 参考资源列表(转)
			
包含必备书籍.站点.博客.活动等参考资源的完整清单级别: 初级 Ted Neward, 主管,ThoughtWorks, Neward & Associates 2009 年 3 月 02 日 ...
 - android 学习随笔十九(对话框、样式、主题、国际化 )
			
1.对话框 package com.itheima.dialog; import android.os.Bundle; import android.app.Activity; import andr ...