题目来源: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 Backup备份和恢复虚拟机(2)

    虚拟机注册之后,下一步就是将他们添加为受保护的项,在菜单底部,单击"保护"按照提示选择需要保护的虚拟机     单击"下一步",在这个页面中需要选择配置虚拟机配 ...

  2. Linux中Samba详细安装

    为了实现Windows主机与Linux服务器之间的资源共享,Linux操作系统提供了Samba服务,Samba服务为两种不同的操作系统架起了一座桥梁,使Linux系统和Windows系统之间能够实现互 ...

  3. Java 基础类型

    在Java中,基本的数据类型主要有8种: 1)int  4 字节(byte)  -2的31次方到2的31次方-1 2)short 2 字节(byte) -2的15次方到2的15次方-1 3)long ...

  4. 安装oracle客户端(navicat for oracle)

    本文使用Navicat for Oracle工具连接oracle 安装的过程我就不在此赘述,跟一般软件的安装过程类似.下面主要讲解Navicat的配置. 1.启动该工具,出现如下的开始界面,单击“连接 ...

  5. python中pip的使用和安装

    Ubuntu下安装pip的方法   安装pip的方法: Install pip and virtualenv for Ubuntu 10.10 Maverick and newer   $ sudo ...

  6. iOS 动态加入button

    按现有的button之后自己主动创造一个新的button,并为新button加入事件,因此,当您单击弹出提示框. 于viewcontroller.h添加 @property (weak, nonato ...

  7. oracle后台进程2

    oracle中的进程共分为三类:用户进程.服务进程.后台进程.其中后台进程伴随实例的启动而启动,他们主要是维护数据库的稳定,相当于一个企业中的管理者及内部服务人员.他们并不会直接给用户提供服务. 一: ...

  8. table中超长字符串省略号表示两种方法

    写在前面:   1.第一种从网上找到的解决方式添加table-layout:fixed   2.第二种添加div   3.字符串过长产生省略号的css语句为如下三种合用:overflow:hidden ...

  9. Ext 随笔

    /-------------------------//清空panel等后面空白属性//------------------------- baseCls:"x-plain" // ...

  10. How to Install/Deinstall Oracle Workspace Manager (文档 ID 263428.1)

    In this Document   Goal   Solution   References APPLIES TO: Workspace Manager - Version 9.0.1.0 to 1 ...