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. jQuery 插件 flexslider 初步使用

    发现了个不错的 jQuery 幻灯片插件 flexslider,有 接近 3000 Star,应该说是很靠谱的,下面是简单使用教程. 引入代码 所有代码都可以在 flexlslider 的 Githu ...

  2. ELK(ElasticSearch, Logstash, Log4j)系统日志搭建

    1.elk平台介绍 Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等. Logsta ...

  3. ubuntu 16.04环境配置

    ubuntu 16:1.源cp /etc/apt/sources.list /etc/apt/sources.list.bkpvi /etc/apt/sources.list-+{    deb ht ...

  4. 在Mac pro上如何配置adb命令?

    在Mac pro上如何将Android SDK的adb命令添加到环境变量中,这里将进行说明! 方法/步骤 1 启动终端,可以在Spotlight中搜索“终端” 2 进入当前用户的HOME目录,命令如下 ...

  5. [LeetCode]题解(python):077-Combinations

    题目来源: https://leetcode.com/problems/combinations/ 题意分析: 给定一个n和k,输出1到n的所有k个数的组合.比如n = 4,k=2 [ [2,4], ...

  6. linq to entity asp.net mvc 多字段排序

    字段1 降序 字段2 降序 var str = db.xxx.OrderByDescending(p=>p.字段1).ThenByDescending(p=>p.字段2) ThenBy - ...

  7. .NET Core 安装

    Visual Studio 2015 和 .NET Core 安装 安装 Visual Studio 和 .NET Core 1.安装 Visual Studio Community 2015,选择 ...

  8. xfire发布的Webservice中Spring注入为空的解决方案

    Spring框架使用中注入为空是一个比较头疼的问题,遇到Webservice和Spring框架配合时,这个问题更容易出现并很难发现问题的原因. 在做SSO系统中就遇到这样的问题,在Service的实现 ...

  9. 不用注册热键方式在Delphi中实现定义快捷键(又简单又巧妙,但要当前窗体处在激活状态)

    第一步:在要实现快捷键的窗体中更改属性“KeyPreview”为True:第二步:在要实现快捷键的窗体中的OnKeyPress事件中填入一个过程名称(在Object Inspector中),填写好后回 ...

  10. 面试题:对一个正整数n,算得到1需要的最少操作次数

    实现一个函数,对一个正整数n,算得到1需要的最少操作次数.操作规则为:如果n为偶数,将其除以2:如果n为奇数,可以加1或减1:一直处理下去.例子:func(7) = 4,可以证明最少需要4次运算n = ...