HDOJ 1224 记忆搜索
2012-08-12 22:19
246 查看
前两天才做了一道记忆搜索题,所以这道题很快就有思路了。
如果一个路径,它经过的城市的interesting point之和最大,但是这条路径的终点不是起点,则这条路径不能取。
如果一个路径,它经过的城市的interesting point之和最大,但是这条路径的终点不是起点,则这条路径不能取。
#include <iostream> using namespace std; const int MAX_CITY = 105; int map[MAX_CITY + 1][MAX_CITY + 1];/* 最后一个点是起点 */ int ans[MAX_CITY + 1],point[MAX_CITY + 1],nextCity[MAX_CITY + 1]; int DFS(int n,int cityNum)/* n是当前所在城市 */ { int maxPoint = 0;/* 从n的下一点i出发,所能积累的最大point值 */ int isReturnable = 0;/* 是否能返回出发点 */ for (int i = (n + 1);i <= (cityNum + 1);i ++) { if (map [i] == 1) { int temp = 0; if (ans[i] == 0)/* 第i个城市的值没有计算过,先计算 */ temp = DFS(i,cityNum); else temp = ans[i]; if (temp > maxPoint) { maxPoint = temp; nextCity = i; } if (temp != 0 || (i == (cityNum + 1))) isReturnable = 1; } } if (isReturnable == 1) ans = point + maxPoint; return ans ; } int main () { int caseNum = 1; scanf("%d",&caseNum); for (int caseCount = 1;caseCount <= caseNum;caseCount ++) { memset(map,0,sizeof(map)); memset(ans,0,sizeof(ans)); memset(nextCity,0,sizeof(nextCity)); int cityNum,flightNum; scanf("%d",&cityNum); for (int i = 1;i <= cityNum;i ++) scanf("%d",&point[i]); point[0] = point[cityNum + 1] = 0; scanf("%d",&flightNum); for (int i = 1;i <= flightNum;i ++) { int a,b; scanf("%d%d",&a,&b); map[a][b] = 1; } printf("CASE %d#\n",caseCount); printf("points : %d\n",DFS(1,cityNum)); printf("circuit : "); int curCity = 1; while (curCity != 0) { printf("%d->",curCity); curCity = nextCity[curCity]; } printf("1\n"); if (caseCount < caseNum) printf("\n"); } return 0; }
相关文章推荐
- HDOJ 1428 漫步校园 (spfa+记忆搜索)
- HDOJ 1078 记忆搜索
- HDOJ 1244 记忆搜索
- HDOJ 2899 二分搜索
- HDOJ 题目2208唉,可爱的小朋友(搜索,dfs)
- new hdoj 1515 (pe)(但是我可以做出来了)(搜索,注意剪枝)
- HDOJ1078 记忆化搜索入门题 有详细的记忆化搜索模板程序
- HDOJ 1010 Tempter of the Bone(搜索,奇偶剪枝)
- HDU/HDOJ 1800 Flying to the Mars 搜索
- HDU/HDOJ 2102 A计划 广度优先搜索BFS
- POJ 1724 记忆话搜索dp
- poj_1088 记忆搜索
- Chapter06-FatMouse and Cheese(ZOJ 1107)(搜索记忆DP)
- POJ 题目1579 Function Run Fun(记忆搜索)
- HDOJ 4277 USACO ORZ 搜索+剪枝
- 杭电1078(记忆搜索)
- HDOJ 4747 - The Moving Points 三分搜索...
- hdoj 1428 漫步校园 (搜索)
- HDU 1428 漫步校园 SPFA + DFS记忆搜索
- coj1224(宽度优先搜索)