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. iOS工程上传AppStore时遇到的问题“ERROR ITMS-90046”解析

    在我们将代码写完整,测试没有bug之后,我们就可以将它上传到AppStore了,上传的过程只要操作正确并不会有太大的问题,但是打包的过程中会出现一些小问题,导致打的包不能上传或者上传的时候会出现错误. ...

  2. redis之入门操作

    下载安装 $ wget http://download.redis.io/releases/redis-3.2.3.tar.gz $ tar xzf redis-3.2.3.tar.gz $ cd r ...

  3. Jsp指令有那些?

    <%@page language="java" contentType="text/html;charset=gb2312" session=" ...

  4. WinSock网络编程基础(2)客户端

    接下来说一下如何用WinSock创建基于TCP/IP模型的客户端和服务器. TCP可以提供两个计算机间可靠无误的数据传输,应用程序使用TCP通信时,会在两台计算机之间建立一个虚拟连接,连接之后计算机之 ...

  5. BZOJ 2337: [HNOI2011]XOR和路径( 高斯消元 )

    一位一位考虑异或结果, f(x)表示x->n异或值为1的概率, 列出式子然后高斯消元就行了 --------------------------------------------------- ...

  6. 5.6.1 Boolean类型

    Boolean类型是与布尔值对应的引用类型.要创建Boolean对象,可以像下面这样调用Boolean构造函数并传入true或false值. var booleanObject=new Boolean ...

  7. thinkphp phpexcel导入

    上次做了一个基于tp3.2.3的phpexcel导出,这次是phpexcel导入,准备材料phpexcel(不知道下载地址的查看我上一篇博文),虽说是基于thinkphp3.2.3来的,也只不过是引入 ...

  8. codeforces 374D. Inna and Sequence 线段树

    题目链接 给m个数, n个操作, 一个数列, 初始为空.一共有3种操作, 在数列末尾加0, 加1, 或删除位置为a[i]的数, a[i]为初始给的m个数, 如果a[i]大于数列长度, 那么什么也不发生 ...

  9. c风格字符串函数

    十一.C 风格字符串  1)字符串操作  strcpy(p, p1) 复制字符串  strncpy(p, p1, n) 复制指定长度字符串  strcat(p, p1) 附加字符串  strncat( ...

  10. 名叫Florida的女孩问题

    近读列纳德·蒙洛迪诺的<醉汉的脚步>,读到第六章,作者举了一个超级变态的例子来解释条件概率或者说是贝叶斯概率. 1.先来看一个简单点的问题:已知所有有2个孩子的家庭,其中一个家庭2个孩子都 ...