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.

题意:循环访问一个国家的所有城市,城市之间有边,求最小的花费

把所有城市拆成入点和出点,然后进行二分图最佳匹配即可。

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
int g[maxn][maxn],match[maxn],lx[maxn],ly[maxn],visx[maxn],visy[maxn],s[maxn],n,m;
bool hungary(int u){
visx[u]=;
for(int i=;i<=n;++i){
if(!visy[i]&&lx[u]+ly[i]==g[u][i]){
visy[i]=;
if(!match[i]||hungary(match[i])){
match[i]=u;
return ;
}
}
else if(!visy[i])s[i]=min(s[i],lx[u]+ly[i]-g[u][i]);
}
return ;
}
int KM(){
for(int i=;i<=n;++i){
for(int j=;j<=n;++j)s[j]=INF;
while(){
memset(visx,,sizeof(visx));
memset(visy,,sizeof(visy));
if(hungary(i))break;
int d=INF;
for(int j=;j<=n;++j)
if(!visy[j])d=min(d,s[j]);
for(int j=;j<=n;++j){
if(visx[j])lx[j]-=d;
if(visy[j])ly[j]+=d;
else s[j]-=d;
}
}
}
int ans=;
for(int i=;i<=n;++i)ans+=g[match[i]][i];
return ans;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(lx,,sizeof(lx));
memset(ly,,sizeof(ly));
memset(match,,sizeof(match));
memset(g,0xc0,sizeof(g));
for(int i=;i<=m;++i){
int a,b,v;
scanf("%d%d%d",&a,&b,&v);
if(-v>g[a][b])g[a][b]=-v;
}
for(int i=;i<=n;++i){
for(int j=;j<=n;++j){
lx[i]=max(lx[i],g[i][j]);
}
}
printf("%d\n",-KM());
}
return ;
}

hdu3488 Tour 拆点+二分图最佳匹配的更多相关文章

  1. HDU2255 奔小康赚大钱【二分图最佳匹配】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=2255 题目大意: 村里要分房子. 有N家老百姓,刚好有N间房子.考虑到每家都要有房住,每家必须分配 ...

  2. hdu2255 奔小康赚大钱 二分图最佳匹配--KM算法

    传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住(如果有老百姓没房子住 ...

  3. hdu 2063 过山车(二分图最佳匹配)

    经典的二分图最大匹配问题,因为匈牙利算法我还没有认真去看过,想先试试下网络流的做法,即对所有女生增加一个超级源,对所有男生增加一个超级汇,然后按照题意的匹配由女生向男生连一条边,跑一个最大流就是答案( ...

  4. HDU 2426 Interesting Housing Problem(二分图最佳匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=2426 题意:每n个学生和m个房间,现在要为每个学生安排一个房间居住,每个学生对于一些房间有一些满意度,如果满意度 ...

  5. HDU 3315 My Brute(二分图最佳匹配+尽量保持原先匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=3315 题意: 有S1到Sn这n个勇士要和X1到Xn这n个勇士决斗,初始时,Si的决斗对象是Xi. 如果Si赢了X ...

  6. 【网络流24题】No.18 分配问题 (二分图最佳匹配 费用流|KM)

    [题意] 有 n 件工作要分配给 n 个人做.第 i 个人做第 j 件工作产生的效益为 cij . 试设计一个将n 件工作分配给 n 个人做的分配方案, 使产生的总效益最大. 输入文件示例input. ...

  7. HDU 2255 二分图最佳匹配

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  8. HDU 2255 二分图最佳匹配 模板题

    题目大意: 给定每一个人能支付的房子价值,每个人最多且必须拥有一套房子,问最后分配房子可得到的最大收益 抄了个别人的KM模板,就这样了... #include <cstdio> #incl ...

  9. ZOJ-3933-Team Formation【二分图最佳匹配】【KM】

    http://blog.csdn.net/loy_184548/article/details/51154195    一开始对不同组合得不同分数(mm1,mg2,gg3),想用sap来写,但是保证了 ...

随机推荐

  1. windows 网络操作

    ver 命令 显示当前机器上的操作系统版本信息 ipconfig/release 释放IP地址 ipconfig/renew 重新获取IP地址 cmd下使用ssh 如果想在cmd中输入 ssh xx@ ...

  2. learning ddr write leveling

  3. QuickStart系列:docker部署之MongoDB

    MongoDB[1]  是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB[2]  是一个介于关系数据库和非关系数据库之间的产品, ...

  4. python-递归,二分查找

    # print(list("胡辣汤")) # lst = ["河南话", "四川话", "东北", "山东&q ...

  5. Annotation方式配置AOP

    package com.xk.spring.kp04_aop.aop.s02_annotation; public interface IStudentService { public void sa ...

  6. javascript es6系列教程 - 不定参数与展开运算符(...)

    三个点(...)在es6中,有两个含义: 用在形参中, 表示传递给他的参数集合, 类似于arguments, 叫不定参数. 语法格式:  在形参面前加三个点( ... ) 用在数组前面,可以把数组的值 ...

  7. JavaScript -基础- 变量、常量

    一.变量 <script> var a=1 var b=3 var a= 1;   //使用var 定义变量,分号结尾(可不加,换行符也可) var b=3; var a= 1; var ...

  8. find 命令实战巧用

    一.命令简介 find 命令的 -size 参数 单位b(不是byte而是block).c.w.k.M.G.默认是单位b ,也就是1block = 512byte = 0.5kb (文件系统ext4) ...

  9. 十二. Python基础(12)--生成器

    十二. Python基础(12)--生成器 1 ● 可迭代对象(iterable) An object capable of returning its members one at a time. ...

  10. css选择器思维导图