UVA 1474 Evacuation Plan
来源:互联网 发布:淘宝淘客怎么无法打开 编辑:程序博客网 时间:2024/05/16 01:47
题意: 在一条直线上有n个队员,m个避难所,他们都有各自的坐标,现在要让所有的队员进入避难所,且要求每个避难所至少有一人,求队员移动的最小距离和。要求输出每个队员进入的避难所编号。数据范围4000。
解法: 设dp[i][j] 表示前j个队员进入前i个避难所得最小移动距离和,那么有如下转移:
dp[i][j] = min(dp[i-1][j-1], dp[i][j-1]) + 队员j和避难所i的距离。
对于要求打印路径,这题的一大天坑啊。内存卡得相当紧,路径只能开bool型,用int都得吃MLE。UVA上貌似不能交,我在POJ判的。下面的代码在HDOJ上不能AC,表示找不到原因。。
/* **********************************************Author : NeroCreated Time: 2013-9-1 7:38:36Problem id : LA 4987Problem Name: Evacuation Plan*********************************************** */#include <stdio.h>#include <string.h>#include <assert.h>#include <algorithm>using namespace std;#define REP(i,a,b) for(int i=(a); i<(int)(b); i++)#define clr(a,b) memset(a,b,sizeof(a))typedef long long lld;const lld INF = (lld) 1e15;lld dp[2][4001]; // 前i个避难所包含前j个队的最小距离struct She { int pos, id; bool operator < (const She & tt) const { return pos < tt.pos; }}b[4001], a[4001];int n,m;int ans[4001];bool path[4001][4001];void dfs(int x, int y) { if(x == 0 || y == 0) return ; ans[a[y].id] = b[x].id; dfs(x-path[x][y],y-1);}int main() { while(~scanf("%d", &n)) { for(int i = 1; i <= n; i ++) scanf("%d", &a[i].pos), a[i].id = i; scanf("%d", &m); for(int i = 1; i <= m; i ++) scanf("%d", &b[i].pos), b[i].id = i; sort(a+1,a+n+1); sort(b+1,b+m+1); for(int i = 0; i <= n; i ++) dp[0][i] = INF; dp[0][0] = 0; for(int i = 1; i <= m; i ++) { int now = i & 1; int pre = ! now; dp[now][i-1] = INF; for(int j = i; j <= n; j ++) { dp[now][j] = abs(a[j].pos - b[i].pos); if(dp[now][j-1] <= dp[pre][j-1]) { dp[now][j] += dp[now][j-1]; path[i][j] = 0; } else { dp[now][j] += dp[pre][j-1]; path[i][j] = 1; } } } printf("%lld\n", dp[m&1][n]); // for(int i = 1; i <= m; i ++) { // for(int j = 1; j <= n; j ++) printf("%20lld", dp[i][j]); // printf("\n"); // } dfs(m,n); for(int i = 1; i <= n; i ++) printf("%d%c", ans[i], i == n ? '\n' : ' '); } return 0;}
- UVA 1474 Evacuation Plan
- UVA 1474 Evacuation Plan
- uva 1474 - Evacuation Plan(dp)
- UVA 1474 Evacuation Plan(dp)
- Evacuation Plan
- UVA - 1474(Evacuation Plan 动态规划中用其他状态优化自己,背包式优化)
- hdu 3757 Evacuation Plan
- UVALive 4987 Evacuation Plan
- HDU 3757 Evacuation Plan
- UVALive - 4987 Evacuation Plan
- POJ-2175-Evacuation Plan
- ZOJ 1553 Evacuation Plan
- POJ 2175 Evacuation Plan
- HDU-3757-Evacuation Plan
- POJ 3963 Evacuation Plan dp
- sgu272:Evacuation plan(大水题)
- POJ 2175 Evacuation Plan 笔记
- *POJ 2175 Evacuation Plan | 费用流
- apache ProxyPass指令详解
- Linux下安装Oracle11g服务器
- 打印所有的组合(组合问题C++实现)
- SQL SERVER BCP命令
- sql行列互换
- UVA 1474 Evacuation Plan
- Solving the "Too many open files" exception in linux
- ListActivity的使用
- C#类继承中构造函数用法小结
- C++ 虚函数表解析
- 地址翻译:从虚拟地址到物理地址
- 模态窗口与非模态窗口销毁方法
- Jquery之较动态的导航
- 两个链表的第一个公共结点