Evacuation Plan - POJ 3963 dp滚动数组

本文探讨了在威胁增加的情况下,如何为建筑团队制定有效的撤离计划,以最小化撤离所需燃料的总成本。通过分析建筑团队的位置与避难所分布,采用动态规划方法优化分配方案。

Evacuation Plan
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 663 Accepted: 155 Special Judge

Description

Flatland government is building a new highway that will be used to transport weapons from its main weapon plant to the frontline in order to support the undergoing military operation against its neighbor country Edgeland. Highway is a straight line and there are n construction teams working at some points on it. 
During last days the threat of a nuclear attack from Edgeland has significantly increased. Therefore the construction office has decided to develop an evacuation plan for the construction teams in case of a nuclear attack. There are m shelters located near the constructed highway. This evacuation plan must assign each team to a shelter that it should use in case of an attack. 
Each shelter entrance must be securely locked from the inside to prevent any damage to the shelter itself. So, for each shelter there must be some team that goes to this shelter in case of an attack. The office must also supply fuel to each team, so that it can drive to its assigned shelter in case of an attack. The amount of fuel that is needed is proportional to the distance from the team’s location to the assigned shelter. To minimize evacuation costs, the office would like to create a plan that minimizes the total fuel needed.
Your task is to help them develop such a plan.

Input

The first line of the input file contains n — the number of construction teams (1 <= n <= 4000). The second line contains n integer numbers — the locations of the teams. Each team’s location is a positive integer not exceeding 109, all team locations are different. 
The third line of the input file contains m — the number of shelters (1 <= m <= n). The fourth linecontains m integer numbers — the locations of the shelters. Each shelter’s location is a positive integer not exceeding 109, all shelter locations are different. 
The amount of fuel that needs to be supplied to a team at location x that goes to a shelter at location y is equal to |x-y|.

Output

The first line of the output file must contain z — the total amount of fuel needed. The second line must contain n integer numbers: for each team output the number of the shelter that it should be assigned to. 
Shelters are numbered from 1 to m in the order they are listed in the input file.

Sample Input

3
1 2 3
2
2 10

Sample Output

8
1 1 2

题意:有n个队伍,m个避难所,每个避难所都至少需要一个队伍,问怎么计划他们的路径,使得所有队伍需要行走的路径和最小。

思路:排序后,dp[i][j]表示前i个队伍用到前j个避难所的最短路径和,中间用bool的数组记录dp的情况,否则会MLE。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;
bool f[4010][4010];
ll dp[2][4010],INF=1e18;
int n,m,ans[4010];
struct node
{
    int x,index;
};
node pos1[4010],pos2[4010];
bool cmp(node a,node b)
{
    return a.x<b.x;
}
void dfs(int a,int b)
{
    if(a==0)
      return;
    ans[pos1[a].index]=pos2[b].index;
    if(f[a][b]==0)
      dfs(a-1,b-1);
    else
      dfs(a-1,b);
}
int main()
{
    int i,j,k,a,b;
    ll ret;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)
        {
            scanf("%I64d",&pos1[i].x);
            pos1[i].index=i;
        }
        scanf("%d",&m);
        for(j=1;j<=m;j++)
        {
            scanf("%I64d",&pos2[j].x);
            pos2[j].index=j;
        }
        sort(pos1+1,pos1+1+n,cmp);
        sort(pos2+1,pos2+1+m,cmp);
        for(i=0;i<=1;i++)
           for(j=0;j<=m;j++)
           dp[i][j]=INF;
        dp[0][0]=0;
        for(i=1;i<=n;i++)
        {
            if(i&1)
              a=0,b=1;
            else
              a=1,b=0;
            dp[b][0]=INF;
            for(j=1;j<=m;j++)
            {
               dp[b][j]=INF;
               ret=abs(pos1[i].x-pos2[j].x);
               if(dp[a][j-1]<=dp[a][j])
               {
                   dp[b][j]=dp[a][j-1]+ret;
                   f[i][j]=0;
               }
               else
               {
                   dp[b][j]=dp[a][j]+ret;
                   f[i][j]=1;
               }
            }
        }
        //debug();
        printf("%I64d\n",dp[b][m]);
        dfs(n,m);
        printf("%d",ans[1]);
        for(i=2;i<=n;i++)
           printf(" %d",ans[i]);
        printf("\n");
    }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值