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算法. 最小权值的时候把权值设置成相反数 /*-------------------------------------------------------------- ...
随机推荐
- ruby URI类
一. URI require 'uri' uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413") # ...
- 【Leetcode】413. Arithmetic Slices
Description A sequence of number is called arithmetic if it consists of at least three elements and ...
- oracle查询优化,存储过程select表循环插入另一个表,以及索引重建
查询语句pl/sql中用F5优化语句 ORACLE的explain plan工具的作用只有一个,获取语句的执行计划1.语句本身并不执行,ORACLE根据优化器产生理论上的执行计划2.语句的分析结果存放 ...
- .net core 新建一个web api 的步骤 初级
1.使用VS2017 选择 .net core web应用程序. 2.选择web api(空). 3.如果需要用iis express调试,则需要修改 program.cs. 4.在Controlle ...
- Hibernate-ORM:07.Hibernate中的参数绑定
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客会讲解Hibernate中的参数绑定,就是相当于sql语句中的where后面的条件 一,讲解概述: 1 ...
- Delphi实例之橡皮筋画图的实现
Delphi实例之橡皮筋画图的实现 在<Delphi7基础教程>这本书的练习中提到过一个橡皮筋画图的例子,书上的源码是错误的!不知道是打印的错误还是本身源码就有问题,我将它改了过来. 在F ...
- Django笔记 —— 高级视图和URL配置
最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...
- Linux 文件属性及修改权限
输入 ll 或 ls -l 命令显示当前目录中文件的属性及文件所属的用户和组 root@user:/home/www# ll test total 880 drwxr-xr-x 2 root root ...
- Jmeter和Charles下载文件
有时候我们jmeter做自动化测试是会遇到文件上传和文件下载的接口,这里我将接结合Charles来Jmeter 文件下载进行讲解 一.用Charles抓包分析文件下载接口 1.1.业务中文件下载链接如 ...
- Gym101981I Magic Potion(最大流)
Problem I. Magic Potion There are n heroes and m monsters living in an island. The monsters became v ...