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. JS 模拟C# 字符串格式化操作

    /*** ** 功能: 字符串格式化替换操作 ***/ String.prototype.format = function () { var args = arguments; return thi ...

  2. 关于git status

    如果只在本地修改,还没有commit,那么用git status, 打印信息为: 如果我本地没有修改文件,就是:

  3. 别人走的路--uap

    首先,我先谈谈我个人的经历,我今年34岁了,做了10多年的ERP实施顾问,大学刚毕业的时候是做ERP软件开发的,后来转岗做了实施顾问.根据我的个人经验,我给你几点建议.1.既然是很大的公司,那么ERP ...

  4. Ant工具

    Ant工具 Ant是一种基于Java的build工具.理论上来说,它有些类似于(Unix)C中的make ,但没有make的缺陷.目前的最新版本为:Ant 1.9.4[1] .   Ant的概念 当一 ...

  5. onContextItemSelected 用法

    http://blog.csdn.net/kavensu/article/details/8045041 onCreateOptionsMenu :此方法为创建菜单方法,这个菜单就是你在点击手机men ...

  6. angularjs 将带标签的内容解析后返回

    参考地址:http://okashii.lofter.com/post/1cba87e8_29e0fab 引入angular-sanitize.js文件 注入ngSanitize 页面数据绑定 ng- ...

  7. JS计算指定日期是距今的第几周,星期几

    无意中在百度知道上发现这样一个问题,就抽时间见写了一个函数. 首先我们需要明确,既然是指定日期距今的第几周,那么就要知道指定的日期是什么,而且是不能确定的,会根据使用者不同而得到不同的日期,所以我们需 ...

  8. DX笔记之一---Direct3D基础

    一.预备知识 1.表面 表面就是Direct3D用于储存2D图像数据的一个像素矩阵.width和height以像素为单位,pitch以字节单位,用接口IDirect3DSurface来描述表面 Loc ...

  9. cuda中时间用法

    转载:http://blog.csdn.net/jdhanhua/article/details/4843653 在CUDA中统计运算时间,大致有三种方法: <1>使用cutil.h中的函 ...

  10. 【ZOJ2112】【整体二分+树状数组】带修改区间第k大

    The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...