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. Win10系列:UWP界面布局进阶9

    Grid Grid元素用来定义一个由行和列构成的网格,这是一个功能强大的布局容器,当新建一个页面时会默认选用Grid作为顶级布局元素,下面将通过三个示例来介绍Grid的使用方法. (1)定义Grid的 ...

  2. centos6.5+python2.7+flask+apache+mod-wsgi部署

    flask部署,使用的是centos6.5,python2.7,版本很重要.基本步骤如下: 一.创建虚拟环境,创建目录把项目拷进去 二.安装mod-wsgi和apache easy_install m ...

  3. java高级⑴

    1.之前我们学过 数组: 数组的特点: 01. 长度一旦被定义,不允许被改变 02. 在内存中开辟一连串连续的空间! 那么现在有一个需求: 让我们定义一个数组 来 保存 新闻信息!!! 问题: 01. ...

  4. python 数据如何保存到excel中--xlwt

    第一步:下载xlwt 首先要下载xlwt,(前提是你已经安装好了Python) 下载地址:  https://pypi.python.org/pypi/xlwt/   下载第二个   第二步:安装xl ...

  5. Linux学习 :移植linux-3.4.83到JZ2440开发板

    一.编译环境搭建: 1.linux源码下载:https://www.kernel.org/ (最新)  https://mirrors.edge.kernel.org/pub/linux/kernel ...

  6. 用postman做自动化测试

    pre-request script: pm.environment.set("title", data.title);pm.environment.set("tab&q ...

  7. transclude

    http://jsfiddle.net/ospatil/A969Z/157/ transclude :true  允许指令内部的dom元素, 保留到 自定义指令的template属性里的含有 ng-t ...

  8. SQLServer查询当前数据库所有索引及统计,并使用游标批量删除

    --查询现有所有数据库表的索引情况 Select indexs.Tab_Name As [表名],indexs.Index_Name As [索引名] ,indexs.[Co_Names] As [索 ...

  9. 如何正确认识Docker Kubernetes 和 Apache Mesos

    参考链接: http://geek.csdn.net/news/detail/229382

  10. 51nod1009

    给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数.   例如:n = 12,包含了5个1.1,10,12共包含3个1,11包含2个1,总共5个1. Input 输入N( ...