Tour

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. 

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 ;
}

最新文章

  1. Linux下make与makefile
  2. 2018. The Debut Album
  3. Web前端优化最佳实践及工具集锦
  4. Hdu4349 Xiao Ming&#39;s Hope
  5. Intro to Filtering with Network Monitor 3.0
  6. css样式单位取整,去掉&#39;px&#39;
  7. FastDFS的安装配置
  8. lintcode:二叉树的所有路径
  9. [OpenXml] Read/Write row/cell from excel
  10. Oracle RAC ORACLE_SID的设置,报错(ORA-01078, LRM-00109)
  11. 【 java版坦克大战--事件处理】 坦克动起来了
  12. Nginx将项目配置在子目录
  13. SDN学习之Mininet验证OpenFlow协议版本
  14. Java中的String、StringBuilder以及StringBuffer
  15. js随机数的取整
  16. cocos creator 中的粒子效果
  17. ubuntu 简单安装配置gitlab
  18. Delphi xe8 FMX StringGrid根据内容自适应列宽。
  19. 【Java入门提高篇】Day28 Java容器类详解(十)LinkedHashMap详解
  20. Emacs org-mode导出html出错

热门文章

  1. P1550 [USACO08OCT]打井Watering Hole
  2. cloudstack网络部分知识点汇总
  3. (转)CloudStack 安装及使用过程中常见问题汇总
  4. Newtonsoft.Json.Linq对象读取DataSet数据
  5. 20155227《网络对抗》Exp8 Web基础
  6. 【最详细最完整】在Linux 下如何打包免安装的QT程序?
  7. 模拟赛 sutoringu
  8. stl源码剖析 详细学习笔记 算法(5)
  9. Python RASP 工程化:一次入侵的思考
  10. let和const----你所不知道的JavaScript系列(2)