图论(二分图,KM算法):HDU 3488 Tour
Tour
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2628 Accepted Submission(s): 1285
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.
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
#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的更多相关文章
- Hdu 3488 Tour (KM 有向环覆盖)
题目链接: Hdu 3488 Tour 题目描述: 有n个节点,m条有权单向路,要求用一个或者多个环覆盖所有的节点.每个节点只能出现在一个环中,每个环中至少有两个节点.问最小边权花费为多少? 解题思路 ...
- HDU 3488 Tour (最大权完美匹配)【KM算法】
<题目链接> 题目大意:给出n个点m条单向边边以及经过每条边的费用,让你求出走过一个哈密顿环(除起点外,每个点只能走一次)的最小费用.题目保证至少存在一个环满足条件. 解题分析: 因为要求 ...
- 图论:KM算法
如果,将求二分图的最大匹配的所有匹配边的权重看做1 那么用匈牙利算法求二分图的最大匹配的问题也可以看成求二分图的最大权匹配 如果边权是特例,我们就要使用KM算法来做了 这个算法其实还是比较难的,会用就 ...
- HDU - 3488 Tour (KM最优匹配)
题意:对一个带权有向图,将所有点纳入一个或多个环中,且每个点只出现一次,求其所有环的路径之和最小值. 分析:每个点都只出现一次,那么换个思路想,每个点入度出度都为1.将一个点拆成两个点,一个作为入度点 ...
- hdu 3488 Tour
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3488 题意:给你一个N个顶点M条边的带权有向图,要你把该图分成1个或多个不相交的有向环.且所有定点都只 ...
- HDU 3488 Tour(最小费用流:有向环最小权值覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3488 题意: 给出n个点和m条边,每条边有距离,把这n个点分成1个或多个环,且每个点只能在一个环中,保证有解. ...
- 带权二分图——KM算法hdu2255 poj3565
进阶指南的板子好像有点问题..交到hdu上会T 需要了解的一些概念: 交错树,顶标,修改量 #include<iostream> #include<stdio.h> #incl ...
- 【UVA11383】 Golden Tiger Claw 【二分图KM算法(板子)】
题目 题目传送门:https://www.luogu.com.cn/problem/UVA11383 分析 最近刚刚学了二分图,然后来了一个这样的题,看完题意之后,稍微想一想就能想出来是一个二分图,然 ...
- 图论(KM算法,脑洞题):HNOI 2014 画框(frame)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABPoAAANFCAIAAABtIwXVAAAgAElEQVR4nOydeVxTV/r/n9ertaJEC4
随机推荐
- poj 3463 Sightseeing(次短路+条数统计)
/* 对dij的再一次理解 每个点依旧永久标记 只不过这里多搞一维 0 1 表示最短路还是次短路 然后更新次数相当于原来的两倍 更新的时候搞一下就好了 */ #include<iostream& ...
- web前端:js
内嵌样式<script></script> alert(“123”)弹出对话框 document.write(“test”) 引入方式 <title></ti ...
- OSI七层模型理解
物理层功能1,为数据端设备提供传送数据的通路 功能2,传输数据 接口.传输介质.信号的传输.网络设备 有线介质:双绞线(普通的网线),光纤. 无线介质:无线电.微波.激光.红外线. 例如手机.电视接收 ...
- C# 各种集合
大多数集合都在 System.Collections,System.Collections.Generic两个命名空间. 其中System.Collections.Generic专门用于泛型集合. ...
- dnsever 邮件记录
记录,备忘
- NSNumber 转 NSString
之前number 转string时候调用stringValue,后来发现未完全转 NSNumber * a_num = [NSNumber numberWithInteger: ]; NSString ...
- TextField的文字距左边框的距离偏移
默认情况下,当向textField输入文字时,文字会紧贴在textField左边框上. 我们可以通过设置textField的leftView,设置一个只有宽度的leftView. 这样还不够,因为默认 ...
- JavaScript HTML DOM 元素(节点)
JavaScript HTML DOM 元素(节点) 创建新的 HTML 元素 创建新的 HTML 元素 如需向 HTML DOM 添加新元素,您必须首先创建该元素(元素节点),然后向一个已存在的元素 ...
- opencv安装及学习资料
第一次装时win7+VS2010+opencv3.0,结果不成功,原因解压出来的没有vc10,可能新版本不在支持vc的旧版本了.所以换了VS2013+opencv3.0,比较经典的安装时VS2010+ ...
- ios开发相关网站
1.苹果开发者中心(ios Dev Center):最权威的学习ios开发的地方,提供ios开发所能用到的所有内容(包含文档.指南以及实例代码). https://developer.apple.co ...