Uva--10003(区间动规,四边形不等式)

本文介绍了一种寻找切割木棍最小成本的算法解决方案。通过动态规划实现,包括记忆化搜索和区间动态规划两种方法,并利用四边形不等式进行优化,显著提高了计算效率。

2014-08-27 15:19:59

  Cutting Sticks 

You have to cut a wood stick into pieces. The most affordable company, The Analog Cutting Machinery, Inc. (ACM), charges money according to the length of the stick being cut. Their procedure of work requires that they only make one cut at a time.

It is easy to notice that different selections in the order of cutting can led to different prices. For example, consider a stick of length 10 meters that has to be cut at 2, 4 and 7 meters from one end. There are several choices. One can be cutting first at 2, then at 4, then at 7. This leads to a price of 10 + 8 + 6 = 24 because the first stick was of 10 meters, the resulting of 8 and the last one of 6. Another choice could be cutting at 4, then at 2, then at 7. This would lead to a price of 10 + 4 + 6 = 20, which is a better price.

Your boss trusts your computer abilities to find out the minimum cost for cutting a given stick. 

Input 

The input will consist of several input cases. The first line of each test case will contain a positive number lthat represents the length of the stick to be cut. You can assume l < 1000. The next line will contain the number n (n < 50) of cuts to be made.

The next line consists of n positive numbers ci ( 0 < ci < l) representing the places where the cuts have to be done, given in strictly increasing order.

An input case with l = 0 will represent the end of the input.

Output 

You have to print the cost of the optimal solution of the cutting problem, that is the minimum cost of cutting the given stick. Format the output as shown below.

Sample Input 

100
3
25 50 75
10
4
4 5 7 8
0

Sample Output 

The minimum cutting is 200.
The minimum cutting is 22.

思路:非常典型的一道DP题,也是小白书上的例题,可以用记忆化搜索,也可用区间动规(O(n^3)),最高效的算法是用四边形不等式优化的区间动规(O(n^2))。

  dp[i][j]表示切割小木棍 i ~ j 的最少费用。

记忆化搜索版(285ms):

 1 /*************************************************************************
 2     > File Name: e.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Tue 29 Jul 2014 09:57:28 AM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 const int INF = 1000000000;
16 
17 int L,n,c[55],dp[55][55];
18 
19 int Solve(int x,int y){
20     if(y - x == 1)
21         return 0;
22     if(dp[x][y]) return dp[x][y];
23     int tmin = INF;
24     for(int k = x + 1; k < y; ++k)
25         tmin = min(tmin,Solve(x,k) + Solve(k,y));
26     return (dp[x][y] = tmin + c[y] - c[x]);
27 }
28 
29 int main(){
30     while(scanf("%d",&L) == 1 && L){
31         scanf("%d",&n);
32         for(int i = 1; i <= n; ++i)
33             scanf("%d",&c[i]);
34             memset(dp,0,sizeof(dp));
35         // dp[i][j] = min{dp[i][k] + dp[k][j]) | i < k < j} + j - i;
36         // 对每个切点,都考虑其他n - 1个切点,总状态数: n^2
37         c[0] = 0;
38         c[n + 1] = L;
39         printf("The minimum cutting is %d.\n",Solve(0,n + 1));
40     }
41     return 0;
42 }
View Code

区间动规版(83ms):

 1 /*************************************************************************
 2     > File Name: 10003.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Wed 27 Aug 2014 03:26:35 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 const int INF = 1 << 30;
16 
17 int dp[55][55];
18 int L,n,c[55];
19 
20 int main(){
21     while(scanf("%d",&L) == 1 && L){
22         scanf("%d",&n);
23         for(int i = 1; i <= n; ++i)
24             scanf("%d",&c[i]);
25         c[0] = 0;
26         c[n + 1] = L;
27         for(int i = 0; i < n + 1; ++i){
28             dp[i][i + 1] = 0;
29         }
30         for(int len = 2; len <= n + 1; ++len){
31             for(int i = 0; i <= n + 1 - len; ++i){
32                 int j = i + len;
33                 int res = INF;
34                 for(int k = i + 1; k < j; ++k){
35                     res = min(res,dp[i][k] + dp[k][j]);
36                 }
37                 dp[i][j] = res + c[j] - c[i];
38             }
39         }
40         printf("The minimum cutting is %d.\n",dp[0][n + 1]);
41     }
42     return 0;
43 }
View Code 

四边形不等式优化的区间动规(53ms),借鉴了传说中的快速读入,再加上一些丧病的优化 QAQ,效率rank终于#7 好感动ing TAT。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream> 
 4 using namespace std;
 5 const int INF = 1 << 30;
 6 
 7 inline int Read(){
 8     int x = 0; char ch = getchar();
 9     while(ch < '0' || ch > '9') ch = getchar();
10     while(ch >= '0' && ch <= '9'){x = x*10 + ch - '0'; ch = getchar();}
11     return x;
12 }
13 
14 int dp[51][51];
15 int S[51][51];
16 int L,n,c[51],res,j,len,i,k;
17 
18 int main(){
19     for( i = 1; i <= 50; ++i)
20             S[i][i + 1] = i + 1;
21     S[0][1] = 1;
22     while(L = Read()){
23         n = Read();
24         for(i = 1; i <= n; ++i)
25             c[i]= Read();
26         c[n + 1]= L;
27         memset(dp[0],0,sizeof(dp[0]));
28         for(len = 2; len <= n + 1; ++len){
29             for(i = 0,j = i + len; i <= n + 1 - len; ++i){
30                 dp[i][j] = INF;
31                 for(k = S[i][j - 1]; k <= S[i + 1][j]; ++k){
32                     res = dp[i][k] + dp[k][j];
33                     if(res < dp[i][j]){
34                         dp[i][j] = res;
35                         S[i][j] = k;
36                     }
37                 }
38                 dp[i][j] += c[j] - c[i];
39                 j++;
40             }
41         }
42         printf("The minimum cutting is %d.\n",dp[0][n + 1]);
43     }
44     return 0;
45 }

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3940196.html

光伏储能虚拟同步发电机VSG并网仿真模型(Similink仿真实现)内容概要:本文档介绍了光伏储能虚拟同步发电机(VSG)并网仿真模型的Simulink实现方法,重点在于通过建立光伏储能系统与虚拟同步发电机相结合的仿真模型,模拟其在并网过程中的态响应与控制特性。该模型借鉴了同步发电机的惯性和阻尼特性,提升了新能源并网系统的频率和电压支撑能力,增强了系统的稳定性与可控性。文档还提及相关电力系统仿真技术的应用,包括逆变器控制、储能配置、并网稳定性分析等,并提供了完整的Simulink仿真文件及技术支持资源链接,便于科研人员复现与二次开发。; 适合人群:电气工程、自化、能源系统等相关专业的研究生、科研人员及从事新能源并网技术开发的工程师。; 使用场景及目标:①用于研究光伏储能系统在弱电网条件下的并网稳定性问题;②掌握虚拟同步发电机(VSG)控制策略的设计与仿真方法;③支持高水平论文(如EI/SCI)的模型复现与创新研究;④为微电网、智能电网中的分布式能源接入提供技术参考。; 阅读建议:建议结合提供的Simulink模型文件与文档说明逐步操作,重点关注VSG控制模块的参数设置与态响应分析,同时可延伸学习文中提及的MPPT、储能管理、谐波分析等相关技术,以提升综合仿真能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值