Tour

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2462    Accepted Submission(s): 1222

Problem 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
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
 

题意:让找一个环,费用最小,这个环要包括所有的点,km算法;不过要建负边;负的最大匹配等于最小匹配,而且要考虑重边的情况;大神们好多用费用流写的。。。膜拜;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define T_T while(T--)
#define F(i,x) for(i=1;i<=x;i++)
#define PR(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define p_ printf(" ")
const int MAXN=210;
const int MAXM=30010;
int mp[MAXN][MAXN];
int lx[MAXN],ly[MAXN],usdx[MAXN],usdy[MAXN],link[MAXN];
int N;
bool dfs(int x){
int i,j;
usdx[x]=1;
F(i,N){
if(!usdy[i]&&lx[x]+ly[i]==mp[x][i]){
usdy[i]=1;
if(link[i]==-1||dfs(link[i])){
link[i]=x;return true;
}
}
}
return false;
}
int km(){
mem(ly,0);mem(link,-1);
int i,j,k;
F(i,N){
lx[i]=-INF;
F(j,N){
lx[i]=max(lx[i],mp[i][j]);
}
}
F(i,N){
mem(usdx,0);mem(usdy,0);
while(!dfs(i)){
int d=INF;
F(j,N){
if(usdx[j]){
F(k,N)
if(!usdy[k])
d=min(d,lx[j]+ly[k]-mp[j][k]);
}
}
F(j,N){
if(usdx[j])lx[j]-=d;
if(usdy[j])ly[j]+=d;
}
mem(usdx,0);mem(usdy,0);
}
}
int ans=0;
F(i,N)ans+=lx[i]+ly[i];
return -ans;
}
void initial(){
int i,j;
F(i,N)F(j,N)mp[i][j]=-INF;
}
int main(){
int T,M;
SI(T);
T_T{
int a,b,c;
SI(N);SI(M);
initial();
while(M--){
SI(a);SI(b);SI(c);
if(-c>mp[a][b])mp[a][b]=-c;
}
printf("%d\n",km());
}
return 0;
}

  

Tour(KM算法)的更多相关文章

  1. 图论(二分图,KM算法):HDU 3488 Tour

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  2. HDU3488 Tour —— 二分图最大权匹配 KM算法

    题目链接:https://vjudge.net/problem/HDU-3488 Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit ...

  3. hdoj 3488 Tour 【最小费用最大流】【KM算法】

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submi ...

  4. HDU 3488 Tour (最大权完美匹配)【KM算法】

    <题目链接> 题目大意:给出n个点m条单向边边以及经过每条边的费用,让你求出走过一个哈密顿环(除起点外,每个点只能走一次)的最小费用.题目保证至少存在一个环满足条件. 解题分析: 因为要求 ...

  5. hdu 3488(KM算法||最小费用最大流)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  6. 匈牙利算法与KM算法

    匈牙利算法 var i,j,k,l,n,m,v,mm,ans:longint; a:..,..]of longint; p,f:..]of longint; function xyl(x,y:long ...

  7. 【HDU2255】奔小康赚大钱-KM算法

    Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  8. HDU2255-奔小康赚大钱-二分图最大权值匹配-KM算法

    二分图最大权值匹配问题.用KM算法. 最小权值的时候把权值设置成相反数 /*-------------------------------------------------------------- ...

  9. KM算法及其优化的学习笔记&&bzoj2539: [Ctsc2000]丘比特的烦恼

    感谢  http://www.cnblogs.com/vongang/archive/2012/04/28/2475731.html 这篇blog里提供了3个链接……基本上很明白地把KM算法是啥讲清楚 ...

随机推荐

  1. 不直接用NSLog

    公司中不直接使用NSLog,而是利用宏定义自己的打印函数,将该打印函数写在项目的.pch文件中.调试的时候往往用到好多打印,但发布的时候确不需要.(一下是在公司中的一些处理) 自定义NSLog 一,固 ...

  2. Queue(队列)

    队列是一种后进后出的数据结构,下面介绍一下队列中常见的函数: 一.queue 中的 empty 函数 queue<int> q ; q.empty() ;  // 若队列中无元素,返回tr ...

  3. 在raw_input()中使用中文提示,在CMD下中文乱码问题解决。。。

    错误的程序及结果: 解决1: guess = int(raw_input('请输入一个整数:'.decode('utf-8').encode('gbk'))) 解决2: guess = int(raw ...

  4. 利用python进行数据分析之数据聚合和分组运算

    对数据集进行分组并对各分组应用函数是数据分析中的重要环节. group by技术 pandas对象中的数据会根据你所提供的一个或多个键被拆分为多组,拆分操作是在对象的特定轴上执行的,然后将一个函数应用 ...

  5. linux用户管理最常用的三个文件说明(不完整版)

    涉及到三个文本文件:/etc/passwd /etc/shadow /etc/group 文件相关: /etc/passwd和用户名相关 /etc/shadow和密码相关 /etc/group和用户所 ...

  6. python函数abs()

    详解: 返回绝对值 参数可以是:负数.正数.浮点数或者长整形 实例: abs(-1.2) #返回 1.2 abs(1.2) #返回 1.2 abs(-11216.5) #返回 11216.5 abs( ...

  7. Qt的Model/View Framework解析(数据是从真正的“肉(raw)”里取得,Model提供肉,所以读写文件、操作数据库、网络通讯等一系列与数据打交道的工作就在model中做了)

    最近在看Qt的Model/View Framework,在网上搜了搜,好像中文的除了几篇翻译没有什么有价值的文章.E文的除了Qt的官方介绍,其它文章也很少.看到一个老外在blog中写道Model/Vi ...

  8. 一劳永逸让windows 64位操作系统 禁止强制驱动签名

    如何让WINDOWS7 64位直接加载“禁用强制驱动程序签名”方式启动  Windows Client 论坛 > Windows 7 问题 0 登录进行投票 因为开发需要,要装一台设备的驱动,但 ...

  9. ThinkPHP 3.1.2 模板中的变量

    一.变量输出 (重点) 1.标量输出 2.数组输出 {$name[1]} {$name['k2']} {$name.k1} 3.对象输出 {$name:k} {$name->k} 二.系统变量 ...

  10. opencv-python 学习笔记2:实现目光跟随(又叫人脸跟随)

    如果机器人的脸能随着前方人脸而转动,你会不会觉得这种互动很有意思.年前的时候,学习了一下opencv,通过opencv可以简单的实现人脸跟随.再加上几个舵机控制头部转动,机器人就可以互动了.呵呵 这里 ...