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视频大全-持续更新

    注意:新浪微博分享的资料和简书分享的资料,略有不同! 小码哥swift3.0版 斗鱼项目视频:点击下载  iOS开发25个项目实战:点击下载 2016PHP全套下载:点击下载  黑马刀哥iOS视频精选 ...

  2. 如何优雅的代码编写 AutoLayout

    概述 使用 Objective-C 纯代码编写 AutoLayout,看 AutoLayout 的字面理解就是自动布局,听起来好像蛮屌的样子.说白了就是适配:适应.兼容各种不同的情况,包括不同版本的操 ...

  3. Java学习之equals和==的区别

    转自:http://www.cnblogs.com/zhxhdean/archive/2011/03/25/1995431.html java中的数据类型,可分为两类: 1.基本数据类型  也称原始数 ...

  4. jackson的简单实用实例(json)

    一个json格式的字符串比如: {"status":10001,"code":"HDkGzI","pubkey":&qu ...

  5. 字节转换/编码转换全为转载GBK,BIG5,utf8,unicode

    C/C++中的字节转换 宽字节转单字节 :size_t wcstombs( char *mbstr, const wchar_t *wcstr, size_t count ); 单字节转宽字节 :si ...

  6. php 通过referer防盗链(以图片为例)

    1.在网页里访问站外图片时,服务器如何知道是在站外引用的呢? (1)对比本服务器请求与跨服务器请求 图一——本服务器请求 图二——显示盗链的referer信息 通过对比也就知道referer显示的是引 ...

  7. RAW模板开发必备知识

    写这个主要是为了让已经熟练掌握PHP的人能够快速的掌握RAW模板开发,从而享受RAW的优越! (注:在实际开发中,最好注意RAW模板开发统一规范,那样可以增强用户体验) 废话不多说,进入正题. 需要记 ...

  8. codeforces 417D. Cunning Gena 状压dp

    题目链接 D. Cunning Gena time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. C文件操作(转载)

    /*1.文件的打开(fopen函数)fopen函数用来打开一个文件,其调用的一般形式为:文件指针=fopen(文件名,文件操作方式):例如:FILE *fpinfpin=fopen("c:\ ...

  10. 什么是PCB改板及PCB改板应注意的问题

    PCB改板是指在保持原有功能一致的前提下,对原有产品设计及电路板布局走线设计的基础上进行整改设计,调整板上器件布局与线路走向,实现电子产品重新设计研发,同时又可以规避知识产权等纠纷,加快新产品研发速度 ...