图论(二分图,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
随机推荐
- C# 父类的属性赋值给子类的方法
遍历父类的属性赋值给子类的方法: private static ChildClass AutoCopy(ParentClass parent) { ChildClass child = new Chi ...
- 阿里大于验证码发送 (ThinkPhp框架)
1.登录平台 阿里大于2.登陆之后我们可以看到资费,使用场景等,在进入正题之前我们需要一些准备工作,首先我们先了解下短信的请求参数,在这里我们需要注意的是sms_param这个参数,在接下来我们申请短 ...
- ASP.NET html转图片
using System.IO; using System.Drawing; using System.Threading; using System.Windows.Forms; public cl ...
- 巧用hidden传递参数
- List指定字段赋特定值(非循环) asp.net
List<Cart> cartd=cartd.Where(p => (p.Id= "123").Length > -1).ToList(); 把Id的值都赋 ...
- android 简单的开机自启
今天我们主要来探讨android怎么让一个service开机自动启动功能的实现.Android手机在启动的过程中会触发一个Standard Broadcast Action,名字叫android.in ...
- MVC中javascript直接调用Model
最近做一个统计页面, Model从后台已经获取了数据集合,想直接在前台展示,而这个展示是需要用js生成图表的. 控制器部分代码: public ActionResult Index() { var m ...
- git代码库的使用
代码库/使用指南 http://learn.zone.jd.com/cmsuser/index.htm 在win7系统下使用TortoiseGit(乌龟git)简单操作Git@OSC http://m ...
- POJ3285 River Hopscotch(最大化最小值之二分查找)
POJ3285 River Hopscotch 此题是大白P142页(即POJ2456)的一个变形题,典型的最大化最小值问题. C(x)表示要求的最小距离为X时,此时需要删除的石子.二分枚举X,直到找 ...
- Proxy 模式
在以下集中情况下可以用 Proxy模式解决问题: 1)创建开销大的对象时候,比如显示一幅大的图片,我们将这个创建的过程交给代理去完成,GoF 称之为虚代理(Virtual Proxy): 2)为网络上 ...