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算法;不过要建负边;负的最大匹配等于最小匹配,而且要考虑重边的情况;大神们好多用费用流写的。。。膜拜;

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<vector>
  7. using namespace std;
  8. const int INF=0x3f3f3f3f;
  9. const double PI=acos(-1.0);
  10. #define mem(x,y) memset(x,y,sizeof(x))
  11. typedef long long LL;
  12. #define SI(x) scanf("%d",&x)
  13. #define SL(x) scanf("%lld",&x)
  14. #define T_T while(T--)
  15. #define F(i,x) for(i=1;i<=x;i++)
  16. #define PR(x) printf("%d",x)
  17. #define PL(x) printf("%lld",x)
  18. #define p_ printf(" ")
  19. const int MAXN=210;
  20. const int MAXM=30010;
  21. int mp[MAXN][MAXN];
  22. int lx[MAXN],ly[MAXN],usdx[MAXN],usdy[MAXN],link[MAXN];
  23. int N;
  24. bool dfs(int x){
  25. int i,j;
  26. usdx[x]=1;
  27. F(i,N){
  28. if(!usdy[i]&&lx[x]+ly[i]==mp[x][i]){
  29. usdy[i]=1;
  30. if(link[i]==-1||dfs(link[i])){
  31. link[i]=x;return true;
  32. }
  33. }
  34. }
  35. return false;
  36. }
  37. int km(){
  38. mem(ly,0);mem(link,-1);
  39. int i,j,k;
  40. F(i,N){
  41. lx[i]=-INF;
  42. F(j,N){
  43. lx[i]=max(lx[i],mp[i][j]);
  44. }
  45. }
  46. F(i,N){
  47. mem(usdx,0);mem(usdy,0);
  48. while(!dfs(i)){
  49. int d=INF;
  50. F(j,N){
  51. if(usdx[j]){
  52. F(k,N)
  53. if(!usdy[k])
  54. d=min(d,lx[j]+ly[k]-mp[j][k]);
  55. }
  56. }
  57. F(j,N){
  58. if(usdx[j])lx[j]-=d;
  59. if(usdy[j])ly[j]+=d;
  60. }
  61. mem(usdx,0);mem(usdy,0);
  62. }
  63. }
  64. int ans=0;
  65. F(i,N)ans+=lx[i]+ly[i];
  66. return -ans;
  67. }
  68. void initial(){
  69. int i,j;
  70. F(i,N)F(j,N)mp[i][j]=-INF;
  71. }
  72. int main(){
  73. int T,M;
  74. SI(T);
  75. T_T{
  76. int a,b,c;
  77. SI(N);SI(M);
  78. initial();
  79. while(M--){
  80. SI(a);SI(b);SI(c);
  81. if(-c>mp[a][b])mp[a][b]=-c;
  82. }
  83. printf("%d\n",km());
  84. }
  85. return 0;
  86. }

  

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. javascript操作JSON字符

    1.先要区分JSON字符串和JSON对象 JSON字符串: Var strJSON = “{“Area”:[{“AreaId”:”123”},{“AreaId”:”345”}]}”,   或者 Var ...

  2. Jquery filter()方法简介

    利用filter函数可以从wrapper set中过滤符合条件的dom元素. 如下图html代码,假如我们要获取类名为filter的<a>标签,用filter方法可以很轻松的获得. < ...

  3. MFC 简单实现 DES 算法

    前言 徐旭东老师说过学者就应该对知识抱有敬畏之心,所以我的博客的标题总喜欢加上"简单"二字,就是为了提醒自己,自己所学知识只是皮毛,离真理还远矣. DES 算法 DES算法是密码体 ...

  4. 一种CentOS图形界面的修复方法

    刚跳槽来这个公司,第一个任务是一块PCIE8120卡的应用开发.尼玛,别人来培训过.演示过的,现在居然没一个人能把别人演示的东西演示给我看!只好自己折腾去了.把服务器搬到自己旁边空位方便折腾,结果发现 ...

  5. android 子线程更新UI

    参考http://examples.javacodegeeks.com/android/core/os/handler/android-handler-example/package com.exam ...

  6. 激活Windows 10 正式版

    原文 http://jingyan.baidu.com/article/27fa732684b5f646f8271ff4.html Windows 10只提供为期一年的免费升级.因此,不要无限拖延期自 ...

  7. C/C++ 基础教程

          自从做IOS后,就比较少用纯C++的方式写代码了,因为Obj-C的代码风格和C++的风格还是有一点区别的.怕自己忘记了C/C++的基础.整理了一些C/C++基础的网站,供大家学习C/C++ ...

  8. 一个异或加密方案--C语言实现

    核心代码: char encrypt( char f , char c) { return f^c; } int OutEncrypt( char *FilePath, char *SecretWor ...

  9. HDU1181【有向图的传递闭包】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1181 题意很简单. 有用并查集做的.我这里用传递闭包做. 有向图的传递闭包采用Floyd思想,可以判断 ...

  10. 编译和安装shibboleth-sp遇到的问题

    In file included from mod_shib_20.cpp:68: mod_shib.cpp:118: warning: deprecated conversion from stri ...