码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive 4987---Evacuation Plan(区间DP)

时间:2016-09-11 15:35:41      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

题目链接

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2988

 

problem 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 input file contains several test cases, each of them as described below. 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 line contains 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

For each test case, the output must follow the description below. 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  然后输入n个施工队的位置(一维坐标) 然后输入m 再输入m个防御点的位置(一维坐标),1<=m<=n<=4000  一维坐标小于1e9  现在让所有的施工队进入防御点,且每个防御点必须有施工队进入,求所有施工队走的最小距离和,并输出每个施工队去的防御点编号;

思路:区间DP,定义dp[i][j]  表示前i个施工队进入j个防御点的最小距离和,那么有状态转移方程:dp[i][j]=max{dp[i-1][j-1],dp[i-1][j]}+abs(a[i]-b[j]) 注意要先对输入的施工队和防御点进行从小到大的排序;

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int n,m;
long long dp[4006][4006];
bool vis[4006][4006];

struct Node
{
    long long x;
    int id;
    int t;
    bool operator < (const Node & tt) const
    { return x < tt.x; }
}a[4005],b[4005];

bool cmp(const Node s1,const Node s2)
{
    return s1.id<s2.id;
}

void print(int x,int y)
{
    if(y==1&&x==1){
        a[x].t=b[y].id;
        return ;
    }
    print(x-1,y-1+vis[x][y]);
    a[x].t=b[y].id;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i].x);
            a[i].id=i;
        }
        sort(a+1,a+n+1);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%lld",&b[i].x);
            b[i].id=i;
        }
        sort(b+1,b+m+1);

        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m&&j<=i;j++)
            {
                if(j==1)
                {
                    dp[i][j]=dp[i-1][j]+abs(a[i].x-b[j].x);
                    vis[i][j]=true;
                }
                else if(j==i)
                {
                    dp[i][j]=dp[i-1][j-1]+abs(a[i].x-b[j].x);
                    vis[i][j]=false;
                }
                else
                {
                    dp[i][j]=min(dp[i-1][j],dp[i-1][j-1])+abs(a[i].x-b[j].x);
                    vis[i][j]=(dp[i-1][j]>dp[i-1][j-1])?false:true;
                }
            }
        }
        cout<<dp[n][m]<<endl;
        print(n,m);
        sort(a+1,a+n+1,cmp);
        for(int i=1;i<=n;i++)
            printf("%d%c",a[i].t,(i==n)?\n: );
    }
    return 0;
}

 

UVALive 4987---Evacuation Plan(区间DP)

标签:

原文地址:http://www.cnblogs.com/chen9510/p/5861899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!