您的位置:首页 > 其它

POJ-1695-Magazine Delivery-dp

2015-08-23 15:20 1086 查看


POJ-1695-Magazine Delivery-dp


Magazine Delivery

Time Limit: 1000MS Memory Limit: 10000K


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: 

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地方出发开始送杂志依次到编号2, 3…, n地方,已知汽车从编号i地方到编号j地方的时间花费(i < j),求最小总时间花费。
有额外几点需要注意,虽然题目没有提到,但确实是出题人“默认”的规则:
不可以到了一个地方却不送报纸
已送过杂志的地方不可以再回去
比如针对第二条,有数据:
1 5
1 1 1 1
10000 10000 10000
10000 10000
10000

AC程序的解为10003,如果“已送过杂志的地方可以再回去”,解就应该是5(三辆车分别依次直接到达编号2, 3, 4地方,然后一辆车回到起点再到编号5地方,共花费3+1+1 = 5) 

有了”默认条件“的话子问题就可以被依次构建,并且子问题的状态就不会被父问题的操作而改变,可以使用动态规划解决。 

设离起点由远到近的汽车编号依次为汽车1,汽车2,汽车3,dp[i][j][k]表示汽车1在编号i地,汽车2在编号j地,汽车3在编号k地的状态下的最小花费时间。则在已知子问题dp[i][j][k]时可构建父问题dp[i+1][j][k],状态转移如下:
假设已知dp[i][*][*]推导dp[i+1][*][*]: 

状态1:编号i+1地是由汽车1送到, dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k] + cost[i][i+1]); 

状态2:编号i+1地是由汽车2送到, dp[i+1][i][k] = min(dp[i+1][i][k], dp[i][j][k] + cost[j][i+1]); 

状态3:编号i+1地是由汽车3送到, dp[i+1][i][j] = min(dp[i+1][i][j], dp[i][j][k] + cost[k][i+1]).
附上代码:
/*
* poj. 1695
* date	2015.8.23
* cost 0ms 380kb
*/
#include <iostream>
#include <memory.h>
using namespace std;
const int INFPIECE = 0x3F;
const int INF = 0x3F3F3F3F;
const int MAXN = 32;
int cost[MAXN][MAXN];
int dp[MAXN][MAXN][MAXN];

int main() {
int t, n;
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i < n; ++i)
for (int j = i + 1; j <= n; ++j)
cin >> cost[i][j];
memset(dp, INFPIECE, sizeof(dp));		// 按字节将dp元素初始化为INF
dp[1][1][1] = 0;

for (int i = 1;
62be
i < n; ++i) {
for (int j = 1; j <= i; ++j) {
for (int k = 1; k <= j; ++k) {
if (dp[i][j][k] == INF)
continue;
dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k]+cost[i][i+1]);
dp[i+1][i][k] = min(dp[i+1][i][k], dp[i][j][k]+cost[j][i+1]);
dp[i+1][i][j] = min(dp[i+1][i][j], dp[i][j][k]+cost[k][i+1]);
}
}
}
int mincost = dp
[1][1];
for (int j = 1; j < n; ++j)
for (int k = 1; k <= j; ++k)
mincost = min(mincost, dp
[j][k]);

cout << mincost << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj dp