ACM: 动态规划 poj1695

该博客介绍了ACM竞赛中的一道动态规划问题poj1695,讨论了如何在限制条件下递归地解决杂志递送问题。每个地点的杂志递送必须在前一个地点之后,并且在任何时候只有1辆车在行驶,另外2辆在休息。
摘要由CSDN通过智能技术生成
                                                                                                                            Magazine Delivery
Description
The TTT Taxi Service in Tehran is required to deliver some magazines to N locations in Tehran. The locations are labeled L1 to LN. TTT assigns 3 cars for this service. At time 0, all the 3 cars and magazines are located at L1. There are plenty of magazines available in L1 and the cars can take as many as they want. Copies of the magazine should be delivered to all locations, observing the following rules:
  1. For all i = 2 .. N, magazines should be delivered at Li only after magazines are delivered at Li-1 .
  2. At any time, only one of the three cars is driving, and the other two are resting in other locations.

The time to go from Li to Lj (or reverse) by any car is a positive integer denoted by D[i , j].
The goal is to organize the delivery schedule for the cars such that the time by which magazines are delivered to all N locations is minimum.
Write a program to compute the minimum delivery time.

Input

The input file contains M instances of this problem (1 <= M <= 10). The first line of the input file is M. The descriptions of the input data follows one after the other. Each instance starts with N in a single line (N <= 30). Each line i of the following N-1 lines contains D[i , j], for all i=1..N-1, and j=i+1..N.

Output

The output contains M lines, each corresponding the solution to one of the input data. In each line, the minimum time it takes to deliver the magazines to all N locations is written.

Sample Input

1

5
10 20 3 4
5 10 20
8 18
19

Sample Output

22

题意: 三辆汽车要分发杂志, 遵循两个规则:
      (1).所有的Li,要在Li-1的处的杂志分发完成后才可以到达Li.
      (2).每次只可以有一辆车在行走.

解题思路:
        1. 一开始还想用最短路做.
        2. 看了网上其他人的题解, 动态规划. 3维的DP. 又学到东西了哈哈!!
        问题解析:
            (1).设dp[i][j][k]: 表示三辆车分别停在i,j,k处所花费的最少时间.(i<=k , j <= k);
            (2).状态转移方程:
              dp[i][j][k] = min(dp[i][j][k+1]+t[k][k+1], //c车从k 到 k+1
                                dp[j][k][k+1]+t[i][k+1], //a车从i 到 k+1
                                dp[i][k][k+1]+t[j][k+1],)//b车从j 到 k+1
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 31

int n;
int t[MAX][MAX];
int dp[MAX][MAX][MAX];

inline int min(int a,int b)
{
    return a < b ? a : b;
}

int main()
{
//    freopen("input.txt","r",stdin);
    int caseNum;
    scanf("%d",&caseNum);
    while(caseNum--)
    {
        scanf("%d",&n);
        for(int i = 1; i < n; ++i)
        {
            for(int j = i+1; j <= n; ++j)
            {
                scanf("%d",&t[i][j]);
            }
        }
        memset(dp,0,sizeof(dp));
        for(int k = n-1; k >= 1; --k)
        {
            for(int i = 1; i <= k; ++i)
            {
                for(int j = 1; j <= k; ++j)
                {
                    dp[i][j][k] = min( dp[i][j][k+1]+t[k][k+1] , min( dp[j][k][k+1]+t[i][k+1] , dp[i][k][k+1]+t[j][k+1] ) );
                }
            }
        }
        printf("%d\n",dp[1][1][1]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值