标签:des style blog http io ar color os sp
Dragon BallTime Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2196 Accepted Submission(s): 767 Problem Description
Sean has got a Treasure map which shows when and where the dragon balls will appear. some dragon balls will appear in a line at the same time for each period.Since the time you got one of them,the other dragon ball will disappear so he can only and must get one Dragon ball in each period.Digging out one ball he will lose some energy.Sean will lose |x-y| energy when he move from x to y.Suppose Sean has enough time to get any drogan ball he want in each period.We want to know the minimum energy sean will lose to get all period’s dragon ball.
Input
In the first line a number T indicate the number of test cases.Then for each case the first line contain 3 numbers m,n,x(1<=m<=50,1<=n<=1000),indicate m period Dragon ball will appear,n dragon balls for every period, x is the initial location of sean.Then two m*n matrix. For the first matrix,the number in I row and J column indicate the location of J-th Dragon ball in I th period.For the second matrix the number in I row and J column indicate the energy sean will lose for J-th Dragon ball in I-th period.
Output
For each case print a number means the minimum energy sean will lose.
Sample Input
1
3 2 5
2 3
4 1
1 3
1 1
1 3
4 2
Sample Output
8
Author
FZU
Source
|
这个题卡死我了啊啊啊啊啊啊啊啊啊
终于找到错了啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
啊啊啊啊啊啊啊啊
大意:有n个periodm个龙珠
每个period会出现一些龙珠
但是你每个period只能收集一个龙珠
告诉你每个龙珠在每个时期出现的位置和digging的cost
问最少花费多少cost
分析:
刚开始读错题意了 题目描述?额出问题了?我以为每个水晶球去过之后就不能去了呢
又是1000状态压缩都没法压
之后看到正确题意之后第一反应出来一个10^7的算法
交了一次TLE
之后我就想怎么优化
看别人题意的时候看到他说用单调队列优化
我想了大概十分钟才终于想通了
对于一个位置pos
dp[i][j] = min(dp[i - 1][k] + abs(pos - k) + cost[i][j])
将其展开
对于pos之前的额k
dp[i][j] = min(dp[i - 1][k] + pos - k + cost[i][j])
对于之后的k
dp[i][j] = min(dp[i - 1][k] + k - pos+ cost[i][j])
我的思路是把每个period的位置都排一次序
对于dp[i][j]只要正着扫一遍k用一个单调队列
然后在倒着扫一遍
然后就wa啊
后来才找到wa点
我们每次往单调队列里push的不能跟当前状态有任何关系==
以为他会影响到后续状态==
只要把dp方程改一下
pos之前的k dp[i][j] = min(dp[i - 1][k] - pos[i - 1][k]) + pos[i][j] + cost[i][j]
pos之后的k dp[i][j] = min(dp[i - 1][j] + pos[i - 1][k]) + cost[i][j] - pos[i][j]
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <algorithm> 6 using namespace std; 7 8 const int maxn = 55; 9 const int maxm = 1005; 10 const int INF = 1000000000; 11 12 struct Node { 13 int x, y, pos; 14 }node[maxn][maxm]; 15 bool cmp(Node n1, Node n2) { 16 return n1.pos < n2.pos; 17 } 18 int cost[maxn][maxm]; 19 int dp[maxn][maxm]; 20 priority_queue<int, vector<int>, greater<int> > q1, q2; 21 22 int main() { 23 int t, n, m, x; 24 scanf("%d",&t); 25 while(t--) { 26 scanf("%d %d %d",&n, &m, &x); 27 for(int i = 1; i <= n; i++) { 28 for(int j = 1; j <= m; j++) { 29 scanf("%d",&node[i][j].pos); 30 node[i][j].x = i; node[i][j].y = j; 31 } 32 } 33 for(int i = 1; i <= n; i++) { 34 for(int j = 1; j <= m; j++) { 35 scanf("%d",&cost[i][j]); 36 } 37 } 38 for(int i = 1; i <= n; i++) { 39 sort(node[i] + 1, node[i] + 1 + m, cmp); 40 } 41 memset(dp, 0x3f, sizeof(dp)); 42 for(int j = 1; j <= m; j++) { 43 dp[1][j] = abs(x - node[1][j].pos) + cost[node[1][j].x][node[1][j].y]; 44 } 45 for(int i = 2; i <= n; i++) { 46 int k = 1; 47 while(!q1.empty()) q1.pop(); 48 while(!q2.empty()) q2.pop(); 49 for(int j = 1; j <= m; j++) { 50 for(k; k <= m; k++) { 51 if(node[i - 1][k].pos > node[i][j].pos) { 52 break; 53 } 54 q1.push(dp[i - 1][k] - node[i - 1][k].pos); 55 } 56 if(!q1.empty()) { 57 dp[i][j] = min(dp[i][j], q1.top() + node[i][j].pos + cost[node[i][j].x][node[i][j].y]); 58 } 59 } 60 k = m; 61 for(int j = m; j >= 1; j--) { 62 for(k; k >= 1; k--) { 63 if(node[i - 1][k].pos < node[i][j].pos) break; 64 q2.push(dp[i - 1][k] + node[i - 1][k].pos); 65 } 66 if(!q2.empty()) { 67 dp[i][j] = min(dp[i][j], q2.top() + (- node[i][j].pos + cost[node[i][j].x][node[i][j].y])); 68 } 69 } 70 } 71 int ans = INF; 72 73 for(int j = 1; j <= m; j++) { 74 ans = min(ans, dp[n][j]); 75 } 76 printf("%d\n", ans); 77 } 78 return 0; 79 }
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/zhanzhao/p/4113540.html