标签:style blog class code tar get
题目连接: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; }
uva 11578 - Situp Benches(dp+输出路径),布布扣,bubuko.com
uva 11578 - Situp Benches(dp+输出路径)
标签:style blog class code tar get
原文地址:http://blog.csdn.net/keshuai19940722/article/details/25095499