Evacuation Plan - POJ 3963 dp滚动数组

来源:互联网 发布:简述数据库的隔离级别 编辑:程序博客网 时间:2024/05/04 02:45

Evacuation Plan
Time Limit: 10000MS Memory Limit: 65536KTotal 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

31 2 322 10

Sample Output

81 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");    }}




0 0
原创粉丝点击