Tour

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 4279    Accepted Submission(s): 2041

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3488

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
 
题意:
我又读错题了...这题我感觉有点歧义,题目中说的是一条路线包含至少一个环,每个城市在一条路线中,反正我是这么理解的...
后来才发现,这个题路线可以多条,但是每一条都至少一个环,然后就是要求到达所有的城市,问最短的路程是多少。
是我英语水平不行么...
 
题解:
题目有几个要求,1.到达所有点;2.路程最短;3.环。
通过思考发现,求二分图的最小权匹配便可以满足以上几个条件,构造二分图时我们是需要把一个点拆分为出度点和入度点,因为每个点只有一次,所以只有一个入度一个出度。对于环中的所有点也是这样。
所以当所有点都为匹配点时,即可满足环的要求。
至于最小权,将边置为其负数然后用KM求最大匹配即可。
 
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define INF 1e9+7
#define mem(x) memset(x,0,sizeof(x))
using namespace std; const int N = , M = ;
int t,n,m,ans;
int match[N],w[N][N],l[N],r[N],slack[N],visx[N],visy[N]; void init(){
mem(match);mem(r);ans=;
for(int i=;i<=N;i++) for(int j=;j<=N;j++) w[i][j]=INF;
for(int i=;i<=N;i++) l[i]=-INF;
} int dfs(int x){
visx[x]=;
for(int i=n+;i<=*n;i++){
if(w[x][i]==INF || visy[i]) continue ;
int tmp = l[x]+r[i]-w[x][i];
if(!tmp){
visy[i]=;
if(!match[i] || dfs(match[i])){
match[i]=x;
return ;
}
}else{
slack[i]=min(slack[i],tmp);
}
}
return ;
} void update(){
int d=INF;
for(int i=n+;i<=*n;i++) if(!visy[i]) d=min(d,slack[i]);
for(int i=;i<=n;i++) if(visx[i]) l[i]-=d;
for(int i=n+;i<=*n;i++) if(visy[i]) r[i]+=d;else slack[i]-=d;
} int KM(){
for(int i=;i<=n;i++){
fill(slack,slack+N,INF);
while(){
mem(visx);mem(visy);
if(dfs(i)) break;
update();
}
}
for(int i=n+;i<=n*;i++) ans+=w[match[i]][i];
return ans;
} int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
init();
for(int i=,x,y,c;i<=m;i++){
scanf("%d%d%d",&x,&y,&c);
w[x][y+n]=min(w[x][y+n],c);
}
for(int i=;i<=n;i++)
for(int j=n+;j<=*n;j++)
if(w[i][j]!=INF) w[i][j]=-w[i][j],l[i]=max(l[i],w[i][j]);
printf("%d\n",-KM());
}
return ;
}

HDU3488:Tour(KM算法)的更多相关文章

  1. Tour(KM算法)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submi ...

  2. HDU3488 Tour KM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8284304.html 题目传送门 - HDU3488 题意概括 给一个n的点m条边的有向图. 然后让你把这个图分 ...

  3. HDU3488 Tour —— 二分图最大权匹配 KM算法

    题目链接:https://vjudge.net/problem/HDU-3488 Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit ...

  4. 图论(二分图,KM算法):HDU 3488 Tour

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  5. hdoj 3488 Tour 【最小费用最大流】【KM算法】

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submi ...

  6. HDU 3488 Tour (最大权完美匹配)【KM算法】

    <题目链接> 题目大意:给出n个点m条单向边边以及经过每条边的费用,让你求出走过一个哈密顿环(除起点外,每个点只能走一次)的最小费用.题目保证至少存在一个环满足条件. 解题分析: 因为要求 ...

  7. hdu 3488(KM算法||最小费用最大流)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  8. 匈牙利算法与KM算法

    匈牙利算法 var i,j,k,l,n,m,v,mm,ans:longint; a:..,..]of longint; p,f:..]of longint; function xyl(x,y:long ...

  9. 【HDU2255】奔小康赚大钱-KM算法

    Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  10. HDU2255-奔小康赚大钱-二分图最大权值匹配-KM算法

    二分图最大权值匹配问题.用KM算法. 最小权值的时候把权值设置成相反数 /*-------------------------------------------------------------- ...

随机推荐

  1. vue2018年5月报错No parser and no file path given

    mac电脑直接: rm -rf node_modules rm package-lock.json npm install npm install prettier@~1.12.1 执行完这四个命令, ...

  2. 10 TCP 传输控制协议 UDP区别

    1.tcp和udp区别 2.TCP通信模型 生活中的电话机 如果想让别人能更够打通咱们的电话获取相应服务的话,需要做一下几件事情: 买个手机 插上手机卡 设计手机为正常接听状态(即能够响铃) 静静的等 ...

  3. 【数据库】 SQLite 介绍

    [数据库] SQLite 介绍 一. 特点 : 小而精悍 1. 轻量级 : 占用资源低 2. 嵌入式 : 无需安装,直接引用就可用 3. 支持 SQL 语法, 大部分兼容 Sql Server 语法, ...

  4. 剖析DI

    0x00.前言 当我们研究一些晦涩的源码,上网查阅资料的时候,映入眼帘的总有这么些名词:DIP.IOC.DI.DL.IOC容器这些专业名词.如果不懂这些名词背后的含义,我们内心有可能是这样的: 0x0 ...

  5. js字符编码笔记

    一.  什么是unicode? ascii码能表示的字符非常有限(128个字符),这对英文来说足够了,但是对法文.中文.土耳奇文等文字则远远不够,于是就产生了新的编码规则-unicode,unicod ...

  6. linux中wget 、apt-get、yum rpm区别

    wget 类似于迅雷,是一种下载工具, 通过HTTP.HTTPS.FTP三个最常见的TCP/IP协议下载,并可以使用HTTP代理名字是World Wide Web”与“get”的结合. yum: 是r ...

  7. 【题解搬运】PAT_L1-009 N个数求和

    从我原来的博客上搬运.原先blog作废. (伪)水题+1,旨在继续摸清这个blog(囧 题目 就是求N个数字的和.麻烦的是,这些数字是以有理数"分子/分母"的形式给出的,你输出的和 ...

  8. 第十篇 Python的字符串格式化

    字符串格式化:就是按照你的意愿做一个拼接的过程. 1. 字符串格式化的第一种方式:百分号方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存. %[ ...

  9. Python中send()和sendall()的区别

    Python中send()和sendall()的区别 估计每个学习Python网络编程的人,都会遇到过这样的问题: send()和sendall()到底有什么区别? send()和sendall()原 ...

  10. 孤荷凌寒自学python第七十四天开始写Python的第一个爬虫4

    孤荷凌寒自学python第七十四天开始写Python的第一个爬虫4 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...