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算法. 最小权值的时候把权值设置成相反数 /*-------------------------------------------------------------- ...
随机推荐
- springmvc基础篇—修改默认的配置文件名称及位置
springmvc的默认配置文件是放在WEB-INF下的,叫action-servlet.xml.根据咱们编程的习惯,一般都将配置文件放到src的根目录下,那么如何将这个文件迁移过来呢?其实很简单,请 ...
- React开发时候注意点
JSX 使用jsx的使用,用一个{}包裹起来,例如 const method = {<div> 123 </div>} 使用()小括号,防止分号自动插入 const eleme ...
- oracle 数据库字段名与实体类字段名称不匹配的处理方法
之前公司一直都使用sql server 即使数据库字段名称与实体类名称不相同 可以使用诸如: select id as userId from tb_user 这种写法,可换到了oracle 之后坑爹 ...
- Prim求MST最小生成树
最小生成树即在一个图中用最小权值的边将所有点连接起来.prim算法求MST其实它的主要思路和dijkstra的松弛操作十分相似 prim算法思想:在图中随便找一个点开始这里我们假定起点为“1”,以点1 ...
- cut 与 awk
cut和awk都能分割显示需要的内容 但在需要以空格为分隔符的情况下: # free -m|grep Mem Mem: cut是以单一空格为分隔符的: # free -m|grep Mem|cut - ...
- MyBatis实例教程--以接口的方式编程
以接口的方式编程: 只需要修改两个地方即可, 1.mapper.xml(实体类)配置文件, 注意mapper的namespace的名字是mapper对象的完整路径名com.xiamen.mapper. ...
- HDU 4729 An Easy Problem for Elfness(主席树)(2013 ACM/ICPC Asia Regional Chengdu Online)
Problem Description Pfctgeorge is totally a tall rich and handsome guy. He plans to build a huge wat ...
- 【Python】Python内置函数dir详解
1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: 复制代码代码如下: >>> help(dir)Help on built-in function ...
- Android问题:could not install *smartsocket* listener;Address already in use
今天启动genymotion后,发现没有ip地址,运行项目时报错: 可见,没有连接到模拟器,无法运行,而先前说过没有ip,自然而然连接不上, 解决放法:将资源管理器打开,将adb全部退出 ...
- JavaScript中注册时间处理程序的方式
基本的方式有两种: 一.第一种方式,出现在Web初期,给时间目标对象或文档元素设置属性. 1.设置JavaScript对象属性为事件处理程序. 示例: 缺点,这种设计都是围绕着假设每个事件目标对于每种 ...