SRM 596 D2 L2:ColorfulRoad,dp
来源:互联网 发布:苹果手机拨号软件 编辑:程序博客网 时间:2024/05/19 06:18
题目:http://community.topcoder.com/stat?c=problem_statement&pm=12837
类似于弗洛伊德最短路径算法思想。
代码:
#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>#include <ctime>using namespace std;#define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)/*************** Program Begin **********************/int dp[20][20];class ColorfulRoad {public:int getMin(string road) {int res = 0;int n = road.size();vector <int> v;for (int i = 0; i < n; i++) {if (road[i] == 'R') {v.push_back(0);} else if (road[i] == 'G') {v.push_back(1);} else {v.push_back(2);}}memset(dp, -1, sizeof(dp));for (int len = 1; len <= n - 1; len++) {for (int i = 0; i <= n - len - 1; i++) {int & ans = dp[i][i+len];if ( (v[i] + 1) % 3 == v[i+len] ) {ans = len * len;}for (int j = i + 1; j < i + len; j++) {if (dp[i][j] == -1 || dp[j][i+len] == -1) {continue;}if (ans == -1) {ans = dp[i][j] + dp[j][i+len];}ans = min(ans, dp[i][j] + dp[j][i+len]);}}}res = dp[0][n-1];return res;}};/************** Program End ************************/
1 0
- SRM 596 D2 L2:ColorfulRoad,dp
- SRM 607 L2 D2:PalindromicSubstringsDiv2,DP
- SRM 605 D2 L2:AlienAndGame
- SRM 606 D2 L2:EllysNumberGuessing
- SRM 608 D2 L2:MysticAndCandiesEasy
- SRM 597 D2 L2:LittleElephantAndString
- SRM 581 D2 L2:SurveillanceSystem,重叠度
- SRM 605 D2 L3: AlienAndSetDiv2,DP
- SRM 607 D2 L3:CombinationLockDiv2,DP
- SRM 595 D2 L3:LittleElephantAndXor, dp
- SRM 601 D2 L3:WinterAndReindeers,dp
- SRM 596 D2 L3:SparseFactorialDiv2,math
- SRM 605 D1 L2:AlienAndSetDiv1,DP,bitmask
- SRM 601 D1 L2:WinterAndSnowmen, dp
- SRM 576 D2 L2:ArcadeManao,DFS,善于根据实际问题使用最简便的方法
- SRM 588 D2 L2:GUMIAndSongsDiv2,冷静思考,好的算法简洁明了
- SRM 576 D2 L3:CharacterBoard2
- SRM 603 D2 L3:GraphWalkWithProbabilities
- 数据结构《10》----二叉树 Morris 中序遍历
- Red and Black
- Android指定特定的包接受广播
- Linux命令详解之su
- ASCII码排序
- SRM 596 D2 L2:ColorfulRoad,dp
- 继续畅通工程
- Linux命令详解之uname
- Linux命令详解之id
- 解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法
- NYOJ 26 孪生素数问题 打表法
- 黄章称年后复出 魅族或将引入新的投资人
- 编程范式--c语言
- 从工厂小工到世界500强,我经历了6年