题目来源:http://community.topcoder.com//stat?c=problem_statement&pm=12587&rd=15501

这道题目开始以为是要在无向图中判断环,而且要找出环的大小,后来看了解析之后才发现原来使用一个Floyd算法就搞定了,因为题目中加了很多限制,并不真的需要在一个任意的无向图中求 指定大小的环的数量。生成所有的排列组合可以使用C++ STL提供的std::next_permutation 算法,非C++使用backtrack,具体实现可以参考解析

代码如下:

#include <algorithm>

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map> #include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring> using namespace std; /*************** Program Begin **********************/ int disA[9][9], disB[9][9];
int P[9];
const int INF = 1000;
class TreeUnionDiv2 {
public:
int maximumCycles(vector <string> tree1, vector <string> tree2, int K) {
int res = 0;
int vex = tree1.size();
for (int i = 0; i < 9; i++) {
P[i] = i;
}
for (int i = 0; i < vex; i++) {
for (int j = 0; j < vex; j++) {
if ('X' == tree1[i][j]) {
disA[i][j] = 1;
} else {
disA[i][j] = INF;
}
if ('X' == tree2[i][j]) {
disB[i][j] = 1;
} else {
disB[i][j] = INF;
}
}
} for (int k = 0; k < vex; k++) {
for (int i = 0; i < vex; i++) {
for (int j = 0; j < vex; j++){
if ( disA[i][j] > disA[i][k] + disA[k][j] ) {
disA[i][j] = disA[i][k] + disA[k][j];
}
if ( disB[i][j] > disB[i][k] + disB[k][j] ) {
disB[i][j] = disB[i][k] + disB[k][j];
}
}
}
} do {
int c = 0;
for (int i = 0; i < vex; i++) {
for (int j = i+1; j < vex; j++) {
if (disA[i][j] + disB[ P[i] ][ P[j] ] + 2 == K) {
++c;
}
}
}
res = max(res, c);
} while (next_permutation(P, P + vex)); return res;
}
}; /************** Program End ************************/

下面为使用 backtrack 实现的全部排列组合:

// This recursive function's only duty is to generate all the possible
// permutations P[].
void backtrack(int i)
{
if (i == N-1) {
//found a permutation, remember the best number of cycles:
best = std::max(best, countCycles() );
} else {
for (int j=i; j<N; j++) {
// Place P[j] in position i, move P[i] to P[j]:
std::swap( P[i], P[j] );
// Continue the backtracking search:
backtrack(i+1);
// Restore the positions of P[i] and P[j]:
std::swap( P[j], P[i] );
}
}
}

SRM 581 D2 L3:TreeUnionDiv2,Floyd算法的更多相关文章

  1. SRM 588 D2 L3:GameInDarknessDiv2,DFS

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12710 采用DFS搜索,第一次写的时候忘了加访问标志,结果状态 ...

  2. SRM 581 D2 L2:SurveillanceSystem,重叠度

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12588 在判断 ‘+’ 的时候使用了 重叠度 的概念,跟一般的 ...

  3. Codeforces Round #581 (Div. 2) C. Anna, Svyatoslav and Maps (Floyd 算法,最短路)

    C. Anna, Svyatoslav and Maps time limit per test2 seconds memory limit per test256 megabytes inputst ...

  4. 最短路径之Floyd算法

    Floyd算法又称弗洛伊德算法,也叫做Floyd's algorithm,Roy–Warshall algorithm,Roy–Floyd algorithm, WFI algorithm. Floy ...

  5. 最短路径—Dijkstra算法和Floyd算法

    原文链接:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html 最后边附有我根据文中Dijkstra算法的描述使用jav ...

  6. 最短路径问题——floyd算法

    floyd算法和之前讲的bellman算法.dijkstra算法最大的不同在于它所处理的终于不再是单源问题了,floyd可以解决任何点到点之间的最短路径问题,个人觉得floyd是最简单最好用的一种算法 ...

  7. floyd算法小结

    floyd算法是被大家熟知的最短路算法之一,利用动态规划的思想,f[i][j]记录i到j之间的最短距离,时间复杂度为O(n^3),虽然时间复杂度较高,但是由于可以处理其他相似的问题,有着广泛的应用,这 ...

  8. Uvaoj 10048 - Audiophobia(Floyd算法变形)

    1 /* 题目大意: 从一个点到达另一个点有多条路径,求这多条路经中最大噪音值的最小值! . 思路:最多有100个点,然后又是多次查询,想都不用想,Floyd算法走起! */ #include< ...

  9. Floyd算法(三)之 Java详解

    前面分别通过C和C++实现了弗洛伊德算法,本文介绍弗洛伊德算法的Java实现. 目录 1. 弗洛伊德算法介绍 2. 弗洛伊德算法图解 3. 弗洛伊德算法的代码说明 4. 弗洛伊德算法的源码 转载请注明 ...

随机推荐

  1. 定制化Azure站点Java运行环境(5)

    Java 8下PermGen及参数设置 在上一章节中,我们定制化使用了Java 8环境,使用我们的测试页面打印出了JVM基本参数,但如果我们自己观察,会发现在MXBeans中,没有出现PermGen的 ...

  2. Nginx学习笔记二基本配置

    1.Nginx的配置文件默认在Nginx程序安装目录的conf二级目录下,主配置文件为nginx.conf.假设您的Nginx安装 在/usr/local/webserver/nginx/目录下,那么 ...

  3. 1.elk 入门示例

    zjtest7-frontend:/usr/local/logstash-2.3.4/bin# ./logstash -e 'input{stdin{}} output{stdout{codec=&g ...

  4. Baidu Sitemap Generator插件使用图解教程

    这两天因为百度对本博客文章收录更新很慢,一直在网络查找真正的原因和解决方法.最终发现了柳城开发的Baidu Sitemap Generator WordPress插件,最终效果如果还需要验证一段时间. ...

  5. adobe reader安装完成之前被中断,错误代码150210解决方法

    adobe reader安装完成之前被中断,错误代码150210解决方法出现这种情况是因为之前安装过adobe reader但是没有卸载删除干净进而导致重新安装时无法安装.为什么卸载不了大多数是因为3 ...

  6. IVM import vector machine

    本文为<Kernel Logistic Regression and the Import Vector Machine>的阅读笔记是技法课的课外阅读 Abstract:基于KLR ker ...

  7. [C++ 2011 STL (VS2012 Update4) 源代码阅读系列(2)]熟悉一些宏定义和模版偏特化或叫模版专门化

    [C++ 2011 STL (VS2012 Update4) 源代码阅读系列(2)]熟悉一些宏定义和模版偏特化或叫模版专门化 // point_test.cpp : 知识点练习和测试,用于单步调试,跟 ...

  8. Oracle表空间常用操作

    --创建表空间 create tablespace test datafile 'E:\test2_data.dbf' SIZE 20M autoextend on next 5M maxsize 5 ...

  9. OC中NSString 的常用方法

    NSString *str1 = @"BeiJing"; NSString *str2 = @"beijing"; //全部转为大写 NSLog(@" ...

  10. 【部分枚举】【3-21个人赛】ProblemH

    Problem H Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Sub ...