ACM: 动态规划 poj1695

来源:互联网 发布:网络安全工程师年薪 编辑:程序博客网 时间:2024/05/22 02:11
                                                              Magazine Delivery
Description
The TTT Taxi Servicein Tehran is required to deliver some magazines to N locations inTehran. The locations are labeled L1 to LN. TTT assigns 3 cars forthis service. At time 0, all the 3 cars and magazines are locatedat L1. There are plenty of magazines available in L1 and the carscan take as many as they want. Copies of the magazine should bedelivered to all locations, observing the following rules:
  1. For all i = 2 .. N, magazines should be delivered at Li onlyafter magazines are delivered at Li-1 .
  2. At any time, only one of the three cars is driving, and theother two are resting in other locations.

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

Input

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

Output

The output containsM lines, each corresponding the solution to one of the input data.In each line, the minimum time it takes to deliver the magazines toall 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;
}
0 0
原创粉丝点击