poj 1695 Magazine Delivery 记忆化搜索

dp[a][b][c],表示三个人从小到大依次在a,b,c位置时,距离结束最少的时间。

每次选一个人走到c+1位置搜索就好了。

坑点在于不能floyd,估计题目没说清楚,意思就是如果没送Li,那么Li~n的点连去都不能去。

#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
int dp[31][31][31];
int mp[31][31];
int n;
int dfs(int a[])
{
    if(~dp[a[0]][a[1]][a[2]]) return dp[a[0]][a[1]][a[2]];
    if(a[2]==n) return 0;
    int tmp[3];
    int mins=INF;
    for(int i=0;i<3;i++)
    {
        tmp[0]=a[0];tmp[1]=a[1];tmp[2]=a[2];
        tmp[i]=a[2]+1;
        sort(tmp,tmp+3);
        mins=min(mins,dfs(tmp)+mp[a[i]][tmp[2]]);
    }
    return dp[a[0]][a[1]][a[2]]=mins;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
        memset(dp,-1,sizeof(dp));
        for(int i=1;i<=n;i++) mp[i][i]=1;
        for(int i=1;i<=n-1;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                scanf("%d",&mp[i][j]);
                mp[j][i]=mp[i][j];
            }
        }
//        for(int k=1;k<=n;k++)
//        {
//            for(int i=1;i<=n;i++)
//            {
//                for(int j=1;j<=n;j++)
//                {
//                    mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
//                }
//            }
//        }
        int a[3]={1,1,1};
        printf("%d\n",dfs(a));
    }
    return 0;
}

poj 1695 Magazine Delivery 记忆化搜索

时间: 2024-08-25 17:48:28

poj 1695 Magazine Delivery 记忆化搜索的相关文章

poj 1088 滑雪 【记忆化搜索】+【DFS】

策略:如题 题目链接:http://poj.org/problem?id=1088 代码: #include<stdio.h> #include<string.h> int map[105][105], dp[105][105], n, m; const int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; //四个方向 int limit(int x, int y) //判断是不是越界了 { if(x<1||x>n||y<1||

POJ 2738 Two Ends 记忆化搜索

题意比较简单,两个人拿偶数张卡片,要么拿当前的最左边,要么拿最右边,尽量拿大的,拿完后求第一个人拿的卡片数字总和减去第二个人卡片的数字总和,求最大差值. 刚开始写记忆化搜索,参考了一下别人的代码,但是还有一个地方不太明白,就是当第一个人拿右边的时候,为什么写成num[x]<=num[y-1]会wa(⊙o⊙)- 求指教额... #include <iostream> #include<stdio.h> #include<algorithm> #include<

POJ 1088 滑雪(记忆化搜索)

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 92384   Accepted: 34948 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17

POJ 3176-Cow Bowling(DP||记忆化搜索)

Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14210   Accepted: 9432 Description The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard

poj 1695 Magazine Delivery dp

题意: 有n个地方,每两个点之间需要的时间已知,现在有3辆车在地点1,要把杂志送到2,3...n,同一时刻只能有一辆车在走,且送到i之前必须送到i-1,求完成任务需要的最少时间. 分析: dp[a][[b][c]表示三辆车由近到远在a,b,c三个地方所要的最少时间.当n==6时,2,2,1,3,1,3.3,3,1,2,1,2.2,2,1,1,1,3这些访问序列都能被dp[2][5][6]表示,动态规划能避免访问状态空间中很多多余状态. 代码: //poj 1695 //sep9 #include

[ACM] POJ 1088 滑雪 (记忆化搜索复习)

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 77763   Accepted: 28905 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17

poj 1088 滑雪【记忆化搜索】

题目链接:http://poj.org/problem?id=1088 基础题,不讲了,饿了 吃早饭去... 代码: #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #include <map> #include <string>

poj 2111 Millenium Leapcow(记忆化搜索)

Description The cows have revised their game of leapcow. They now play in the middle of a huge pasture upon which they have marked a grid that bears a remarkable resemblance to a chessboard of N rows and N columns (3 <= N <= 365). Here's how they se

POJ 2704 Pascal&#39;s Travels (基础记忆化搜索)

Pascal's Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5328   Accepted: 2396 Description An n x n game board is populated with integers, one nonnegative integer per square. The goal is to travel along any legitimate path from t