HDU3488:Tour(KM算法)
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:
#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算法)的更多相关文章
- Tour(KM算法)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submi ...
- HDU3488 Tour KM
原文链接http://www.cnblogs.com/zhouzhendong/p/8284304.html 题目传送门 - HDU3488 题意概括 给一个n的点m条边的有向图. 然后让你把这个图分 ...
- HDU3488 Tour —— 二分图最大权匹配 KM算法
题目链接:https://vjudge.net/problem/HDU-3488 Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit ...
- 图论(二分图,KM算法):HDU 3488 Tour
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- hdoj 3488 Tour 【最小费用最大流】【KM算法】
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submi ...
- HDU 3488 Tour (最大权完美匹配)【KM算法】
<题目链接> 题目大意:给出n个点m条单向边边以及经过每条边的费用,让你求出走过一个哈密顿环(除起点外,每个点只能走一次)的最小费用.题目保证至少存在一个环满足条件. 解题分析: 因为要求 ...
- hdu 3488(KM算法||最小费用最大流)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- 匈牙利算法与KM算法
匈牙利算法 var i,j,k,l,n,m,v,mm,ans:longint; a:..,..]of longint; p,f:..]of longint; function xyl(x,y:long ...
- 【HDU2255】奔小康赚大钱-KM算法
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...
- HDU2255-奔小康赚大钱-二分图最大权值匹配-KM算法
二分图最大权值匹配问题.用KM算法. 最小权值的时候把权值设置成相反数 /*-------------------------------------------------------------- ...
随机推荐
- PublicCMS 网站漏洞 任意文件写入并可提权服务器权限
PublicCMS是目前网站系统中第一个采用JAVA架构 TOMCAT+Apcche+Mysql数据库架构的CMS网站,开源,数据承载量大,可以承载到上千万的数据量,以及用户的网站并发可达到上千万的P ...
- 密码发生器 南阳acm519
密码发生器 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 在对银行账户等重要权限设置密码的时候,我们常常遇到这样的烦恼:如果为了好记用生日吧,容易被破解,不安全:如 ...
- array_x
import java.util.*; public class array_x { public static void main(String args[]) { int a[][]={{2,4, ...
- spring boot打包问题
java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories ...
- HTC Vive小场地与大场景空间的解决方案
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52780621 作者:car ...
- 使用TFS需要注意的地方
1. 用管理员添加了本地映射,然后用其他用户就添加不了映射,一定要先用管理员账户去把映射 删除掉: 2. 在正式使用TFS时,一定需要在VS工具的设置里面,设置一下,签出时自动获取最新的代码.(默认是 ...
- Bug是一种财富-------研发同学的错题集、测试同学的遗漏用例集
此文已由作者王晓明授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 各位看官,可能看到标题的你一定认为这是一篇涉嫌"炒作"的文章,亦或是为了吸引眼球而起的标 ...
- Django笔记 —— 表单(form)
最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...
- SVN脱离锁定的几种方法
SVN经常出现被锁定而无法提交的问题,选择解锁又提示没有文件被锁定,很是头疼.这里整理了一下SVN 被锁定的几种解决方法: 1.出现这个问题后使用“清理”即"Clean up"功能 ...
- 树莓派3_win10下使用"远程桌面连接"与树莓派通信(使用VNC实现连接后)
-----------------------------------------------------------学无止境------------------------------------- ...