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:
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.
- For all i = 2 .. N, magazines should be delivered at Li only
after magazines are delivered at Li-1 .
- 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;
}
该博客介绍了ACM竞赛中的一道动态规划问题poj1695,讨论了如何在限制条件下递归地解决杂志递送问题。每个地点的杂志递送必须在前一个地点之后,并且在任何时候只有1辆车在行驶,另外2辆在休息。
9455

被折叠的 条评论
为什么被折叠?



