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,每个王子心中都有心仪的女孩,现在问如果安排,能够使得题中给定的式子和最大. 分析:其实题目 ...
随机推荐
- D. Jzzhu and Cities
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 ...
- cf244D. Match & Catch 字符串hash (模板)或 后缀数组。。。
D. Match & Catch 能够用各种方法做.字符串hash.后缀数组,dp.拓展kmp,字典树.. . 字符串hash(模板) http://blog.csdn.net/gdujian ...
- 解决eth0网卡无法自动加载的问题
问题:输入ifup eth0显示无法加载网卡所在的文件 解决办法: vi /etc/rc.d/rc.local 最后一行加入: ifup eth0 然后重启虚拟机即可解决问题. 本文为博主原创文章,未 ...
- 9.Libraries and visibility 库和可见性
import和liabrary指令可以帮助你创建模块化,可复用的代码.库不仅仅提供API,也是一个私有化单元:库中已下划线(_)开头的类都是对外不可访问的.每个Dart的应用也是一个包,尽管它没有使用 ...
- 20155308『网络对抗技术』Exp5 MSF基础应用
20155308『网络对抗技术』Exp5 MSF基础应用 一.原理与实践说明 实践内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 一个主动攻击实 ...
- MFC CHotKeyCtrl控件
知识点: CHotKeyCtrl控件 获取热键数据 注册热键 响应热键事件 一.CHotKeyCtrl控件 void SetHotKey( WORD wVirtualKeyCode, WORD wMo ...
- UCOSII笔记---信号量、邮箱、消息队列、信号量集、软件定时器
一.接收邮箱函数的参数:timeout表示的是滴答定时器的节拍数,比如设定5ms为一个节拍,超时为100ms,则timeout=20. void *OSMboxPend (OS_EVENT *peve ...
- CS229笔记:生成学习算法
在线性回归.逻辑回归.softmax回归中,学习的结果是\(p(y|x;\theta)\),也就是给定\(x\)的条件下,\(y\)的条件概率分布,给定一个新的输入\(x\),我们求出不同输出的概率, ...
- libgdx学习记录25——Rectangle与Circle是否重叠
Rect与Circle重叠有三种情况: 1. Rect至少有一个角在Circle里面 2. Circle与Rect的左边或右边相交,或者Circle在Rect内 3. Circle与Rect的顶边或底 ...
- vue中v-if 和 v-show的区别
简单来说,v-if 的初始化较快,但切换代价高:v-show 初始化慢,但切换成本低 1.共同点 v-if 和 v-show 都可以动态地显示DOM元素 2.区别 (1)手段: v-if 是动态的向D ...