Cyclic Tour

                                                                               Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
                                                                                         Total Submission(s): 2709    Accepted Submission(s): 1387

Problem Description
There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of
all the tours minimum, but he is too lazy to calculate. Can you help him?
 
Input
There are several test cases in the input. You should process to the end of file (EOF).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B,
whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
 
Output
Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1. 
 
Sample Input
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
6 5
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
 
Sample Output
42
-1

Hint

In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.

 
Author
RoBa@TJU
 
Source
 
Recommend
lcy

——————————————————————————————

题目的意思是是给出一张有向图,要选择几条边使得每个点都落在一个环上,使得所选的边和最小

思路:每个点落在环上,所以每个点的入度出度均为1,这正好符合二分图性质,建立二分图,求最大权匹配,题目要求最小,权值取负数即可

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <vector>
  9. #include <set>
  10. #include <stack>
  11. #include <map>
  12. #include <climits>
  13. using namespace std;
  14.  
  15. #define LL long long
  16.  
  17. const int MAXN = 505;
  18. const int INF = 0x3f3f3f3f;
  19. int g[MAXN][MAXN];
  20. int lx[MAXN],ly[MAXN]; //顶标
  21. int linky[MAXN];
  22. int visx[MAXN],visy[MAXN];
  23. int slack[MAXN];
  24. int nx,ny;
  25. bool find(int x)
  26. {
  27. visx[x] = true;
  28. for(int y = 0; y < ny; y++)
  29. {
  30. if(visy[y])
  31. continue;
  32. int t = lx[x] + ly[y] - g[x][y];
  33. if(t==0)
  34. {
  35. visy[y] = true;
  36. if(linky[y]==-1 || find(linky[y]))
  37. {
  38. linky[y] = x;
  39. return true; //找到增广轨
  40. }
  41. }
  42. else if(slack[y] > t)
  43. slack[y] = t;
  44. }
  45. return false; //没有找到增广轨(说明顶点x没有对应的匹配,与完备匹配(相等子图的完备匹配)不符)
  46. }
  47.  
  48. int KM() //返回最优匹配的值
  49. {
  50. int i,j;
  51. memset(linky,-1,sizeof(linky));
  52. memset(ly,0,sizeof(ly));
  53. for(i = 0; i < nx; i++)
  54. for(j = 0,lx[i] = -INF; j < ny; j++)
  55. lx[i] = max(lx[i],g[i][j]);
  56. for(int x = 0; x < nx; x++)
  57. {
  58. for(i = 0; i < ny; i++)
  59. slack[i] = INF;
  60. while(true)
  61. {
  62. memset(visx,0,sizeof(visx));
  63. memset(visy,0,sizeof(visy));
  64. if(find(x)) //找到增广轨,退出
  65. break;
  66. int d = INF;
  67. for(i = 0; i < ny; i++) //没找到,对l做调整(这会增加相等子图的边),重新找
  68. {
  69. if(!visy[i] && d > slack[i])
  70. d = slack[i];
  71. }
  72. for(i = 0; i < nx; i++)
  73. {
  74. if(visx[i])
  75. lx[i] -= d;
  76. }
  77. for(i = 0; i < ny; i++)
  78. {
  79. if(visy[i])
  80. ly[i] += d;
  81. else
  82. slack[i] -= d;
  83. }
  84. }
  85. }
  86. int result = 0;
  87. int cnt=0;
  88. for(i = 0; i < ny; i++)
  89. if(linky[i]>-1)
  90. {
  91. result += g[linky[i]][i];
  92. if(g[linky[i]][i]!=-1044266559)
  93. cnt++;
  94. }
  95. if(cnt<nx)
  96. result=1;
  97. return -result;
  98. }
  99.  
  100. int main()
  101. {
  102. int n,m,u,v,c,T;
  103.  
  104. while(~scanf("%d%d",&n,&m))
  105. {
  106. nx=ny=n;
  107. memset(g,-INF,sizeof g);
  108. for(int i=0; i<m; i++)
  109. {
  110. scanf("%d%d%d",&u,&v,&c);
  111. u--,v--;
  112. g[u][v]=max(g[u][v],-c);
  113. }
  114. printf("%d\n",KM());
  115. }
  116. return 0;
  117. }

  

HDU1853 Cyclic Tour的更多相关文章

  1. hdu1853 Cyclic Tour (二分图匹配KM)

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

  2. HDU1853 Cyclic Tour(最小费用最大流)

    题目大概说给一张有向图,每条边都有权值,要选若干条边使其形成若干个环且图上各个点都属于且只属于其中一个环,问选的边的最少权值和是多少. 各点出度=入度=1的图是若干个环,考虑用最小费用最大流: 每个点 ...

  3. HDU-1853 Cyclic Tour

    最小权值环覆盖问题:用几个环把所有点覆盖,求所选取的边最小的权值之和. 拆点思想+求最小转求最大+KM算法 #include <cstdlib> #include <cstdio&g ...

  4. hdu1853 Cyclic Tour 完美匹配 验证模版

    题意: 给出n个城市和m条路,每个城市只能经过一次,想要旅游所有的城市,求需要的最小花费(路径的长度). 分析: 做题之前,首先要知道什么是完美匹配.不然题目做了却不知道为什么可以用这个方法来做.完美 ...

  5. Cyclic Tour HDUOJ 费用流

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

  6. hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...

  7. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

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

  8. HDU 1853 Cyclic Tour[有向环最小权值覆盖]

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

  9. 最大流增广路(KM算法) HDOJ 1853 Cyclic Tour

    题目传送门 /* KM: 相比HDOJ_1533,多了重边的处理,还有完美匹配的判定方法 */ #include <cstdio> #include <cmath> #incl ...

随机推荐

  1. Liunx Mkdir

    linux mkdir命令: 创建目录 介绍:该命令创建指定的目录名,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录1语法: mkdir [-m] [-p] 目录 ...

  2. iis日志分析软件及大文本切割软件下载

    在网上找了好几个日志分析软件,觉得这个是最简单.实用的,至少对我来说. 但这个软件有个缺点,就是日志比较大时,分析详细的会溢出,需要用到文本切割工具. 软件下载: iis日志分析软件 大文本切割软件 ...

  3. PAT 1087 有多少不同的值(20)(STL—set)

    1087 有多少不同的值(20 分) 当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然数 ...

  4. 1.1 Java 的概述

    [什么是java]:Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是有SunMicrosystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaSE,Jav ...

  5. Servlet API

    Servlet API的查询网址:通过Tomcat的官网链接找到 可见,Servlet API有4个packages javax.servlet // 包含定义Servlet和Servlet容器之间契 ...

  6. [AI]神经网络章2 神经网络中反向传播与梯度下降的基本概念

    反向传播和梯度下降这两个词,第一眼看上去似懂非懂,不明觉厉.这两个概念是整个神经网络中的重要组成部分,是和误差函数/损失函数的概念分不开的. 神经网络训练的最基本的思想就是:先“蒙”一个结果,我们叫预 ...

  7. jquery单行文字上下循环滚动

    html代码: <div class="box"> <div class="t_news"> <b>已关联奖励账号.昵称:& ...

  8. Java,JavaScript,jQuery,jSP,js

    js是javascript文件的文件后缀,就像 a.txt 这个.txt是后缀一样 jsp是jsp网页文件的后缀,而jsp是java web 的表现层的一种技术 jquery 是一个函数库,基于jav ...

  9. 【Redis】Redis cluster集群搭建

    Redis集群基本介绍 Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施installation. Redis 集群不支持那些需要同时处理多个键的 Redis 命令, 因为执行 ...

  10. 数字统计(NOIP2010)

    题目链接:数字统计 这题很水. 思路就是:枚举每一个区间内的数,然后对于每一个数,每个位去判断是否为2,就行了. 下面上代码: #include<bits/stdc++.h> using ...