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算法. 最小权值的时候把权值设置成相反数 /*-------------------------------------------------------------- ...
随机推荐
- PAT (Basic Level) Practice (中文)1002
1002 写出这个数 (20 分) 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1 ...
- Python3 列表,元组,字典,字符串知识小结
一.知识概要 1. 列表,元组,字典,字符串的创建方式 2. 列表,元组,字典,字符串的方法调用 3. 列表,元组,字典,字符串的常规用法 二.列表 # 列 表 # 列表基础 list_1 = ['a ...
- poj_2339
参考:https://blog.csdn.net/yzl_rex/article/details/7600906 https://blog.csdn.net/acm_JL/article/detail ...
- Python3.6中PyInstaller不能对文件进行打包问题
上篇文章<itchat和matplotlib的结合使用爬取微信信息>是用python爬取信息得到微信朋友的信息,并且用matplotlib统计信息进行画图,所以今天想将它打包成.exe可执 ...
- [Cracking the Coding Interview] 4.6 Successor 后继节点
Write an algorithm to find the 'next' node(i.e. in-order successor) of a given node in a binary sear ...
- PRO*C 函数事例 1 -- 数据库连接、事务处理
1.程序结构 每一个Pro*C程序都包括两部分:(1)应用程序首部:(2)应用程序体 应用程序首部定义了ORACLE数据库的有关变量, 为在C语言中操纵ORACLE数据库做 ...
- Python的文件输入输出,如何追加内容,读取内容,添加内容
python主要的文件打开的几种访问模式 模式可以为读模式('r').写模式('w')或追加模式('a'),当然还有rb.wb.ab.r+.w+.a+.rb+.wb+.ab+,然而实际从代码上我也没看 ...
- php用GD库给图片添加水印
php用GD库给图片添加文字水印,整个代码比较简单,DEMO如下: <?php /*打开图片*/ //1.配置图片路径 $src = "aeroplane.jpg"; //2 ...
- Scala学习笔记(二):运行脚本文件
在某个目录(如:F:\)下新建一个文本文件,命名为:hello.scala 其内容为: println("Hello World!") 那么这个时候该怎么运行这个脚本文件呢? 通过 ...
- Anytime项目开发记录0
Anytime,中文名:我很忙. 开发者:孤独的猫咪神. 这个项目会持续更新,直到我决定不再维护这个APP. 2014年3月10日:近日有事,暂时断更.希望可以会尽快完事. 2014年3月27日:很抱 ...