Tour

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

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
 
   这题就是KM算法的模板。
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
const int INF=;
int w[maxn][maxn],sx[maxn],sy[maxn],lx[maxn],ly[maxn];
int match[maxn],slack[maxn];
int n,m; bool Search(int x){
sx[x]=true;
for(int y=;y<=n;y++){
if(sy[y])continue;//?
int t=lx[x]+ly[y]-w[x][y];
if(t)
slack[y]=min(slack[y],t);
else{
sy[y]=true;
if(!match[y]||Search(match[y])){
match[y]=x;
return true;
}
}
}
return false;
} int KM(){
memset(lx,0x80,sizeof(lx));
memset(ly,,sizeof(ly));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
lx[i]=max(lx[i],w[i][j]); for(int i=;i<=n;i++){
memset(slack,,sizeof(slack));
while(true){
memset(sx,,sizeof(sx));
memset(sy,,sizeof(sy));
if(Search(i))
break;
int minn=INF;
for(int j=;j<=n;j++)
if(!sy[j]&&minn>slack[j])
minn=slack[j]; for(int j=;j<=n;j++)
if(sx[j])
lx[j]-=minn; for(int j=;j<=n;j++)
if(sy[j])
ly[j]+=minn;
else
slack[j]-=minn;
}
}
int ret=;
for(int i=;i<=n;i++)
ret+=w[match[i]][i];
return ret;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(w,0x80,sizeof(w));
memset(match,,sizeof(match));
for(int i=,a,b,c;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
w[a][b]=max(w[a][b],-c);
}
printf("%d\n",-KM());
}
return ;
}

图论(二分图,KM算法):HDU 3488 Tour的更多相关文章

  1. Hdu 3488 Tour (KM 有向环覆盖)

    题目链接: Hdu 3488 Tour 题目描述: 有n个节点,m条有权单向路,要求用一个或者多个环覆盖所有的节点.每个节点只能出现在一个环中,每个环中至少有两个节点.问最小边权花费为多少? 解题思路 ...

  2. HDU 3488 Tour (最大权完美匹配)【KM算法】

    <题目链接> 题目大意:给出n个点m条单向边边以及经过每条边的费用,让你求出走过一个哈密顿环(除起点外,每个点只能走一次)的最小费用.题目保证至少存在一个环满足条件. 解题分析: 因为要求 ...

  3. 图论:KM算法

    如果,将求二分图的最大匹配的所有匹配边的权重看做1 那么用匈牙利算法求二分图的最大匹配的问题也可以看成求二分图的最大权匹配 如果边权是特例,我们就要使用KM算法来做了 这个算法其实还是比较难的,会用就 ...

  4. HDU - 3488 Tour (KM最优匹配)

    题意:对一个带权有向图,将所有点纳入一个或多个环中,且每个点只出现一次,求其所有环的路径之和最小值. 分析:每个点都只出现一次,那么换个思路想,每个点入度出度都为1.将一个点拆成两个点,一个作为入度点 ...

  5. hdu 3488 Tour

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3488 题意:给你一个N个顶点M条边的带权有向图,要你把该图分成1个或多个不相交的有向环.且所有定点都只 ...

  6. HDU 3488 Tour(最小费用流:有向环最小权值覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=3488 题意: 给出n个点和m条边,每条边有距离,把这n个点分成1个或多个环,且每个点只能在一个环中,保证有解. ...

  7. 带权二分图——KM算法hdu2255 poj3565

    进阶指南的板子好像有点问题..交到hdu上会T 需要了解的一些概念: 交错树,顶标,修改量 #include<iostream> #include<stdio.h> #incl ...

  8. 【UVA11383】 Golden Tiger Claw 【二分图KM算法(板子)】

    题目 题目传送门:https://www.luogu.com.cn/problem/UVA11383 分析 最近刚刚学了二分图,然后来了一个这样的题,看完题意之后,稍微想一想就能想出来是一个二分图,然 ...

  9. 图论(KM算法,脑洞题):HNOI 2014 画框(frame)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABPoAAANFCAIAAABtIwXVAAAgAElEQVR4nOydeVxTV/r/n9ertaJEC4

随机推荐

  1. Java 8 被动迭代式特性介绍(转自IBM)

    编程语言一般都需要提供一种机制用来遍历软件对象的集合,现代的编程语言支持更为复杂的数据结构,如列表.集合.映射和数组.遍历能力是通过公共方法提供,而内部细节都隐藏在类的私有部分,所以程序员不需要了解其 ...

  2. 9.27 noip模拟试题

    工资 (money/money.in/money.out) 时限1000ms 内存256MB 聪哥在暑假参加了打零工的活动,这个活动分为n个工作日,每个工作日的工资为Vi.有m个结算工钱的时间,聪哥可 ...

  3. 关于ADO.NET的一些知识整理

    ADO.NET是什么 虽然我们都知道ADO.NET是对数据库的操作,但是要真的说出ADO.NET的具体含义还不是很容易. ADO.NET是ActiveX Data Objects的缩写,它是一个COM ...

  4. 关于封装的一个小问题和TA的例子

    写个小例子吧 --  很多细节(如校验.判断等等)都略了 其实不是有意写成这样,而是很多朋友都这么写(当然里面也有点夸张的写法) 这么写其实也没什么不好,简单明了,不用动脑子,一看就很直白, 但是如果 ...

  5. 为Activity设置特定权限才能启动

    1.在AndroidManifest文件中,声明一个权限,并在activity中添加属性 <!--声明权限,权限名一般为包名+permission+类名 --> <permissio ...

  6. get 和 post的使用.

    Two commonly used methods for a request-response between a client and server are: GET and POST. GET  ...

  7. oracle安装遇到的问题

    这两天要做一个项目,教师招聘系统.要用oracle.就安装了oracle 12c,安装的过程中遇到了一些问题,最后自己解决了.我是win7系统. 第一个报错: [INS-30131]执行安装程序验证所 ...

  8. asp.net 批量下载实现(打包压缩下载)

    1.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default ...

  9. 关于 ASP.NET 验证码

    Session["CheckCode"] 这个..不懂神马意思.. .创建一个用户控件 用户名:TextBox 密码: TextBox 验证码:TextBox 验证码图片 < ...

  10. php利用smtp类轻松的发送电子邮件

    当你还在纠结php内置的mail()函数不能发送邮件时,那么你现在很幸运,此时的这篇文章可以帮助到你! php利用smtp类来发邮件真是屡试不爽,我用过很久了,基本上没出过问题.本博客后台,当博主回复 ...