HDU1853 Cyclic Tour
Cyclic Tour
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 2709 Accepted Submission(s): 1387
all the tours minimum, but he is too lazy to calculate. Can you help him?
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).
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
-1
In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
——————————————————————————————
题目的意思是是给出一张有向图,要选择几条边使得每个点都落在一个环上,使得所选的边和最小
思路:每个点落在环上,所以每个点的入度出度均为1,这正好符合二分图性质,建立二分图,求最大权匹配,题目要求最小,权值取负数即可
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- #include <queue>
- #include <vector>
- #include <set>
- #include <stack>
- #include <map>
- #include <climits>
- using namespace std;
- #define LL long long
- const int MAXN = 505;
- const int INF = 0x3f3f3f3f;
- int g[MAXN][MAXN];
- int lx[MAXN],ly[MAXN]; //顶标
- int linky[MAXN];
- int visx[MAXN],visy[MAXN];
- int slack[MAXN];
- int nx,ny;
- bool find(int x)
- {
- visx[x] = true;
- for(int y = 0; y < ny; y++)
- {
- if(visy[y])
- continue;
- int t = lx[x] + ly[y] - g[x][y];
- if(t==0)
- {
- visy[y] = true;
- if(linky[y]==-1 || find(linky[y]))
- {
- linky[y] = x;
- return true; //找到增广轨
- }
- }
- else if(slack[y] > t)
- slack[y] = t;
- }
- return false; //没有找到增广轨(说明顶点x没有对应的匹配,与完备匹配(相等子图的完备匹配)不符)
- }
- int KM() //返回最优匹配的值
- {
- int i,j;
- memset(linky,-1,sizeof(linky));
- memset(ly,0,sizeof(ly));
- for(i = 0; i < nx; i++)
- for(j = 0,lx[i] = -INF; j < ny; j++)
- lx[i] = max(lx[i],g[i][j]);
- for(int x = 0; x < nx; x++)
- {
- for(i = 0; i < ny; i++)
- slack[i] = INF;
- while(true)
- {
- memset(visx,0,sizeof(visx));
- memset(visy,0,sizeof(visy));
- if(find(x)) //找到增广轨,退出
- break;
- int d = INF;
- for(i = 0; i < ny; i++) //没找到,对l做调整(这会增加相等子图的边),重新找
- {
- if(!visy[i] && d > slack[i])
- d = slack[i];
- }
- for(i = 0; i < nx; i++)
- {
- if(visx[i])
- lx[i] -= d;
- }
- for(i = 0; i < ny; i++)
- {
- if(visy[i])
- ly[i] += d;
- else
- slack[i] -= d;
- }
- }
- }
- int result = 0;
- int cnt=0;
- for(i = 0; i < ny; i++)
- if(linky[i]>-1)
- {
- result += g[linky[i]][i];
- if(g[linky[i]][i]!=-1044266559)
- cnt++;
- }
- if(cnt<nx)
- result=1;
- return -result;
- }
- int main()
- {
- int n,m,u,v,c,T;
- while(~scanf("%d%d",&n,&m))
- {
- nx=ny=n;
- memset(g,-INF,sizeof g);
- for(int i=0; i<m; i++)
- {
- scanf("%d%d%d",&u,&v,&c);
- u--,v--;
- g[u][v]=max(g[u][v],-c);
- }
- printf("%d\n",KM());
- }
- return 0;
- }
HDU1853 Cyclic Tour的更多相关文章
- hdu1853 Cyclic Tour (二分图匹配KM)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- HDU1853 Cyclic Tour(最小费用最大流)
题目大概说给一张有向图,每条边都有权值,要选若干条边使其形成若干个环且图上各个点都属于且只属于其中一个环,问选的边的最少权值和是多少. 各点出度=入度=1的图是若干个环,考虑用最小费用最大流: 每个点 ...
- HDU-1853 Cyclic Tour
最小权值环覆盖问题:用几个环把所有点覆盖,求所选取的边最小的权值之和. 拆点思想+求最小转求最大+KM算法 #include <cstdlib> #include <cstdio&g ...
- hdu1853 Cyclic Tour 完美匹配 验证模版
题意: 给出n个城市和m条路,每个城市只能经过一次,想要旅游所有的城市,求需要的最小花费(路径的长度). 分析: 做题之前,首先要知道什么是完美匹配.不然题目做了却不知道为什么可以用这个方法来做.完美 ...
- Cyclic Tour HDUOJ 费用流
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...
- hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- HDU 1853 Cyclic Tour[有向环最小权值覆盖]
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- 最大流增广路(KM算法) HDOJ 1853 Cyclic Tour
题目传送门 /* KM: 相比HDOJ_1533,多了重边的处理,还有完美匹配的判定方法 */ #include <cstdio> #include <cmath> #incl ...
随机推荐
- Liunx Mkdir
linux mkdir命令: 创建目录 介绍:该命令创建指定的目录名,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录1语法: mkdir [-m] [-p] 目录 ...
- iis日志分析软件及大文本切割软件下载
在网上找了好几个日志分析软件,觉得这个是最简单.实用的,至少对我来说. 但这个软件有个缺点,就是日志比较大时,分析详细的会溢出,需要用到文本切割工具. 软件下载: iis日志分析软件 大文本切割软件 ...
- PAT 1087 有多少不同的值(20)(STL—set)
1087 有多少不同的值(20 分) 当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然数 ...
- 1.1 Java 的概述
[什么是java]:Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是有SunMicrosystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaSE,Jav ...
- Servlet API
Servlet API的查询网址:通过Tomcat的官网链接找到 可见,Servlet API有4个packages javax.servlet // 包含定义Servlet和Servlet容器之间契 ...
- [AI]神经网络章2 神经网络中反向传播与梯度下降的基本概念
反向传播和梯度下降这两个词,第一眼看上去似懂非懂,不明觉厉.这两个概念是整个神经网络中的重要组成部分,是和误差函数/损失函数的概念分不开的. 神经网络训练的最基本的思想就是:先“蒙”一个结果,我们叫预 ...
- jquery单行文字上下循环滚动
html代码: <div class="box"> <div class="t_news"> <b>已关联奖励账号.昵称:& ...
- Java,JavaScript,jQuery,jSP,js
js是javascript文件的文件后缀,就像 a.txt 这个.txt是后缀一样 jsp是jsp网页文件的后缀,而jsp是java web 的表现层的一种技术 jquery 是一个函数库,基于jav ...
- 【Redis】Redis cluster集群搭建
Redis集群基本介绍 Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施installation. Redis 集群不支持那些需要同时处理多个键的 Redis 命令, 因为执行 ...
- 数字统计(NOIP2010)
题目链接:数字统计 这题很水. 思路就是:枚举每一个区间内的数,然后对于每一个数,每个位去判断是否为2,就行了. 下面上代码: #include<bits/stdc++.h> using ...