uva 11578 - Situp Benches(dp+输出路径)
来源:互联网 发布:c语言用什么编译器好 编辑:程序博客网 时间:2024/05/16 17:39
题目连接:uva 11578 - Situp Benches
题目大意:健身房有两个仪器,初始角度为10度,每次有人使用需要交15元,每调10度需要花费10元,现在有n个人,给出每个人使用仪器的顺序和角度,保证不会同时有大于2个人序号一样,求最小花费,并且输出每个人分别使用哪一个仪器,并且所有人使用结束后,要将仪器调回10度。
解题思路:dp[i][x][y]表示第i个人,一个仪器为x,另一个仪器为y的情况,path[i][x][y]记录的是它的前一个状态,[0]为前一个仪器的夹角,[1]为另一个仪器的夹角,[2]为当前序号下前一个人用的是哪个仪器。
#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>using namespace std;const int N = 20005;const int INF = 0x3f3f3f3f;struct state {int id;int ra;}s[N][2];int n, m, c[N];int dp[N][10][10], path[N][10][10][3], t[N];inline void add (int x, int i, int re) {s[x][c[x]].id = i;s[x][c[x]].ra = re/10;c[x]++;}void init () {n = 0;scanf("%d", &m);memset(c, 0, sizeof(c));int x, re;for (int i = 0; i < m; i++) {scanf("%d%d", &x, &re);x++;n = max(n, x);add(x, i, re);}n++;add(n, m+10, 10);add(n, m+10, 10);}inline void set (int d, int p, int q, int x, int y, int sign) {path[d][p][q][0] = x;path[d][p][q][1] = y;path[d][p][q][2] = sign;}inline void cat (int d, int p) {if (c[d] == 0) return ;t[s[d][0].id] = p;if (c[d] == 2)t[s[d][1].id] = 3 - p;}int solve () {memset(dp, INF, sizeof(dp));dp[0][1][1] = 0;for (int i = 1; i <= n; i++) {for (int x = 1; x <= 5; x++) {for (int y = 1; y <= 5; y++) {if (dp[i-1][x][y] == INF)continue;if (c[i] == 0) {if (dp[i][x][y] > dp[i-1][x][y]) {dp[i][x][y] = dp[i-1][x][y];set(i, x, y, x, y, 0);}} else if (c[i] == 1) {int p = s[i][0].ra;if (dp[i][p][y] > dp[i-1][x][y] + abs(x-p)*10) {dp[i][p][y] = dp[i-1][x][y] + abs(x-p)*10;set(i, p, y, x, y, 1);}if (dp[i][x][p] > dp[i-1][x][y] + abs(y-p)*10) {dp[i][x][p] = dp[i-1][x][y] + abs(y-p)*10;set(i, x, p, x, y, 2);}} else {int p = s[i][0].ra;int q = s[i][1].ra;if (dp[i][p][q] > dp[i-1][x][y] + (abs(x-p) + abs(y-q))*10) {dp[i][p][q] = dp[i-1][x][y] + (abs(x-p) + abs(y-q))*10;set(i, p, q, x, y, 1);}if (dp[i][q][p] > dp[i-1][x][y] + (abs(x-q) + abs(y-p))*10) {dp[i][q][p] = dp[i-1][x][y] + (abs(x-q) + abs(y-p))*10;set(i, q, p, x, y, 2);}}}}}int p = 1, q = 1;for (int i = n; i >= 0; i--) {cat(i, path[i][p][q][2]);int x = path[i][p][q][0];int y = path[i][p][q][1];p = x; q = y;}return dp[n][1][1] + 15 * m;}int main () {int cas;scanf("%d", &cas);while (cas--) {init ();printf("%d\n", solve());for (int i = 0; i < m; i++)printf("%d\n", t[i]);}return 0;}
2 0
- uva 11578 - Situp Benches(dp+输出路径)
- UVA 11578 - Situp Benches(dp)
- uva 11578 Situp Benches (DP)求完dp之后求路径
- 【DP】UVA 103 Stacking Boxes 输出路径
- Benches
- poj1015(dp输出路径)
- CodeForces864E【DP+路径输出】
- uva 116 Unidirectional TSP 单向TSP 问题,经典dP(路径输出注意规划方向)
- uva 10564 DP+打印路径
- UVA-624 记录路径dp
- UVa 624 CD (01背包路径输出)
- uva 11404 Palindromic Subsequence lcs 路径输出
- UVA 264 CD (01背包+输出路径)
- UVA 531 - Compromise(dp + LCS打印路径)
- UVA 531 DP LCS 打印路径
- 【DP】UVA 624 CD 记录路径
- Uva 10599 - Robots(II) (dp + 记录路径)
- uva 116 Unidirectional TSP dp + 打印路径
- maven项目添加jar包.
- the msi failed
- IIS防盗链:ISAPI Rewrite图片防盗链规则写法
- undone有关链表
- db2日期和时间常用汇总
- uva 11578 - Situp Benches(dp+输出路径)
- SVN标准目录结构 的一些网文
- POJ 1050 To the Max(枚举+动规)
- iOS设备是否越狱的判断代码
- Map集合类比较
- linux shell编程awk、sed调用方法
- mesh repair
- AVR_BootLoader设计及实现
- Ubuntu操作命令备忘录(不定时更新)