题目链接: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

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条路,现在汤姆想要旅游所有的城市,而且每个城市只能经过一次,当然,旅游路上有一点的花费,现在问汤姆怎么走才能使总花费最小。
分析:刚开始,被题目误导了,说什么跑环啊什么的,好多好多的环啊,我越看越懵逼了。感觉像最小生成树,又没有回来,感觉像一个跑两遍的最短路,一个点又只能经过一次。
其实可以这样思考:
由于图是单向的,每条边的两个点,分别放到入点的集合,和出点的集合,我然后做最小权匹配,然后扫一遍出点,因为要成环,必然每个点既是入点,也是出点。要是每个出点都匹配到了,不管你匹配的点是什么,反正这个环,或者几个环都匹配好了,就返回最小权值,否则返回-1.
然后这里要注意重边,还有,匹配好了,也可能是之前的无穷大,所以加一个判断。
#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的更多相关文章

  1. Poj(3686),最小权匹配,多重匹配,KM

    题目链接 The Windy's | Time Limit: 5000MS | Memory Limit: 65536K | | Total Submissions: 4939 | Accepted: ...

  2. poj 2195(KM求最小权匹配)

    题目链接:http://poj.org/problem?id=2195 思路:我们都知道KM使用来求最大权匹配的,但如果要求最小权匹配,只需把图中的权值改为负值,求一次KM,然后权值和取反即可. ht ...

  3. 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

    [POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit ...

  4. 【POJ 2195】 Going Home(KM算法求最小权匹配)

    [POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  5. Assignment (HDU 2853 最大权匹配KM)

    Assignment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. POJ 3565 Ants (最小权匹配)

    题意 给出一些蚂蚁的点,给出一些树的点,两两对应,使他们的连线不相交,输出一种方案. 思路 一开始没想到怎么用最小权匹配--后来发现是因为最小权匹配的方案一定不相交(三角形两边之和大于第三边)--还是 ...

  7. UVALIVE 4970 最小权匹配

    首先贴一下这道题的BNU地址,UVA地址自己找吧. http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=11852 题意:这道题的意思就是,给你N个棋子的 ...

  8. POJ 2400 最小权匹配

    吐槽:首先,这道题的输入居然是错的.要将上下两个矩阵的位置换一下才可以出样例,也就是上面那个矩阵是employee对Supervisor的打分,下面那个矩阵才是Supervisor对employee的 ...

  9. 二分图带权匹配 KM算法与费用流模型建立

    [二分图带权匹配与最佳匹配] 什么是二分图的带权匹配?二分图的带权匹配就是求出一个匹配集合,使得集合中边的权值之和最大或最小.而二分图的最佳匹配则一定为完备匹配,在此基础上,才要求匹配的边权值之和最大 ...

随机推荐

  1. C++Primer 第十九章

    //1.控制内存分配: //A:某些应用程序对内存分配有特殊的需求,因此我们无法将标准内存管理机制直接应用于这些程序.它们常常需要自定义内存分配的细节,比如使用关键字new将对象放置在特定的内存空间中 ...

  2. PostgreSQL数据库系统的进程结构

    PostgreSQL数据库系统的主要功能都集中于Postgres程序,其入口是Main模块中的main函数,在初始化数据集簇,启动数据库服务器是,都将从这里开始执行.Main模块主要的工作时确定当前的 ...

  3. 源码搭建LNMP

      源码安装LNMP 作者:尹正杰   版权声明:原创作品,谢绝转载!否则将追究法律责任.       欢迎加入:高级运维工程师之路 598432640 前言:非常简单的一个平台LNMP,在生产实际环 ...

  4. [Reprint]c++中typename和class的区别介绍

    在c++Template中,很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢?   相信学习C++的人对class这个关键字都非常明白,clas ...

  5. C++的STL在C#中的应用

    这里主要讲几个重要的STL在C#中的应用:vector, map, hash_map, queue, set, stack, list. vector: 在C#中换成了list using Syste ...

  6. linux第12天 线程

    今天主要学习了共享内存,信号量的封装,还有线程. POSIX线程库 与线程有关的函数构成了一个完整的系列,绝大多数函数的名字都是以“pthread_”打头的 要使用这些函数库,要通过引入头文<p ...

  7. HDU 4900 NO ACM NO LIFE(概率+枚举+搜索)(2014 Multi-University Training Contest 4)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4900 Problem Description There is an old country and ...

  8. AS-demo09

    ,mainifast: <uses-permission android:name="android.permission.SET_WALLPAPER"/> , < ...

  9. Logstash学习1-logstash的简单例子

    如何安装ELK Redis插件 1. 安装好logstash后.2. 最简单的logstash.logstash -e 'input { stdin { } } output { stdout {} ...

  10. Visual Studio 2012 怪异的自动重启

    学生在做项目的过程中遇到这种问题: -------------- 用 Visual Studio 2012  开发W中eb 项目时,最近总是莫名其妙的自动重启. 后来试了一下,发现是只要在页面中输入 ...