Tour HDU - 3488(最大权值匹配)
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.
InputAn 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.OutputFor 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
The route should contain one or more loops.
一个或多个环。。二分匹配足以。。 把权值取反 然后套最大权值匹配即可
注意有重边。。但我们是要最小值 取反后在输入的时候只保留max的值即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
int usedx[maxn], usedy[maxn], cy[maxn], cx[maxn], w[maxn][maxn], bx[maxn], by[maxn];
int nx, ny, n, minn, min_val, m;
int dfs(int u)
{
usedx[u] = ;
for(int i=; i<=ny; i++)
{
if(usedy[i] == -)
{
int t = bx[u] + by[i] - w[u][i];
if(t == )
{
usedy[i] = ;
if(cy[i] == - || dfs(cy[i]))
{
cy[i] = u;
cx[u] = i;
return ;
}
}
else if(t > )
minn = min(minn, t);
}
}
return ;
} void km()
{
mem(cy, -);
mem(cx, -);
for(int i=; i<=nx; i++) bx[i] = -INF;
mem(by, );
for(int i=; i<=nx; i++)
for(int j=; j<=ny; j++)
bx[i] = max(bx[i], w[i][j]);
for(int i=; i<=nx; i++)
{
while()
{
minn = INF;
mem(usedx, -);
mem(usedy, -);
if(dfs(i)) break;
for(int j=; j<=nx; j++)
if(usedx[j] != -) bx[j] -= minn;
for(int j=; j<=ny; j++)
if(usedy[j] != -) by[j] += minn;
}
}
min_val = ;
for(int i=; i<=nx; i++)
if(cx[i] != -)
min_val += w[i][cx[i]];
printf("%d\n",-min_val);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=; i<=n; i++)
for(int j=; j<=n; j++) w[i][j] = -INF;
for(int i=; i<=m; i++)
{
int u, v, c;
scanf("%d%d%d",&u,&v,&c);
w[u][v] = max(w[u][v], -c);
}
nx = ny = n;
km();
}
return ;
}
Tour HDU - 3488(最大权值匹配)的更多相关文章
- hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...
- 奔小康赚大钱 HDU - 2255(最大权值匹配 KM板题)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU2255-奔小康赚大钱-二分图最大权值匹配-KM算法
二分图最大权值匹配问题.用KM算法. 最小权值的时候把权值设置成相反数 /*-------------------------------------------------------------- ...
- POJ 2400 Supervisor, Supervisee(KM二分图最大权值匹配)题解
题意:n个老板n个员工,先给你n*n的数据,i行j列代表第i个老板第j喜欢的员工是谁,再给你n*n的数据,i行j列代表第i个员工第j喜欢的老板是谁,如果匹配到第k喜欢的人就会产生一个分数k-1.现在让 ...
- 二分图最大权值匹配 KM算法 模板
KM算法详解+模板 大佬讲的太好了!!!太好了!!! 转载自:http://www.cnblogs.com/wenruo/p/5264235.html KM算法用来求二分图最大权完美匹配. 本文配合该 ...
- Q - Tour - hdu 3488(最小匹配值)
题意:一个王国有N个城市,M条路,都是有向的,现在可以去旅游,不过走的路只能是环(至少也需要有两个城市),他们保证这些城市之间的路径都是有环构成的,现在至少需要走多少路. 分析:因为是有向图所以,而且 ...
- Tour HDU - 3488 有向环最小权值覆盖 费用流
http://acm.hdu.edu.cn/showproblem.php?pid=3488 给一个无源汇的,带有边权的有向图 让你找出一个最小的哈密顿回路 可以用KM算法写,但是费用流也行 思路 1 ...
- HDU 2255 KM算法 二分图最大权值匹配
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- ZOJ-2362 Beloved Sons 最大权值匹配
题意:国王有N个儿子,现在每个儿子结婚都能够获得一定的喜悦值,王子编号为1-N,有N个女孩的编号同样为1-N,每个王子心中都有心仪的女孩,现在问如果安排,能够使得题中给定的式子和最大. 分析:其实题目 ...
随机推荐
- ansible 常用方法
测试:用shell执行一个脚本很麻烦的,用script执行 1)推送脚本过去,并授权 ansible george -m copy -a "src=/tmp/test.sh dest=/tm ...
- 转自《https安全链接的配置教程:startSSl免费证书申请与nginx的https支持配置》
一.什么是 SSL 证书,什么是 HTTPS 网站? SSL证书是数字证书的一种,类似于驾驶证.护照和营业执照的电子副本.SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secu ...
- 关于PCB的线宽与过孔
关于PCB的线宽与过孔 我们在画PCB时一般都有一个常识,即走大电流的地方用粗线(比如50mil,甚至以上),小电流的信号可以用细线(比如10mil). 对于某些机电控制系统来说,有时候走线里流过的瞬 ...
- VBA删除 语法
Option Explicit '清空数据 Private Sub CommandButton1_Click() Dim qknum As Integer '选择是或者否 来确认删除数据 '中对话 ...
- 【chrome】"您的连接不是私密连接" 解决办法
1.启用显示证书选项 在Chrome的地址栏中输入:chrome://flags/#show-cert-link,选择"启用",重启Chrome浏览器. (操作过无用) 2.安装 ...
- controlfile作为RMAN的repository时,对 keep time 的测试
4月2日,首先查看系统状况: SQL> show parameter control NAME TYPE VALUE ...
- scala学习——(1)scala基础(上)
scala> val x = 1 x: Int = 1 一.值与变量 值(val):赋值后不可变 val值名称:类型 = XXX 变量(var):赋值后可以改变 var变量名称:类型 = XX ...
- 将WebService部署到 SharePoint 2010 gac 缓存中,并用Log4Net记录日志到数据库
最近做了一个sharePoint项目,需要实现的功能是,第三方网站访问我们sharePoint中的数据,通过Webservice方式实现文件的上传和下载. 于是代码工作完成了之后,本地调试没什么问题, ...
- RPG游戏开发基础教程
RPG游戏开发基础教程 第一步 下载RPG Maker 开发工具包 1.RPG Maker 是什么? RPG Maker 是由Enterbrain公司推出的RPG制作工具. 中文译名为RPG制作大师. ...
- 【翻译】Brewer's CAP Theorem CAP定理
Brewer's CAP Theorem 原文地址:http://www.julianbrowne.com/article/brewers-cap-theorem Brewer’s (CAP) The ...