nyist 740 “炫舞家“ST(DP)

网友投稿 709 2022-10-08

nyist 740 “炫舞家“ST(DP)

nyist 740 “炫舞家“ST(DP)

题目地址:#include #include #include #include #include #include #include #include #include #include const int inf = 0x3f3f3f3f;//1061109567typedef long long LL;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int cost[5][5];int dp[10010][5][5];int a[10010];void init(){ for(int i=1; i<=4; i++) { cost[0][i] = 2; cost[i][i] = 1; } cost[1][3] = cost[3][1] = 4; cost[2][4] = cost[4][2] = 4; cost[1][2] = cost[2][1] = 3; cost[2][3] = cost[3][2] = 3; cost[3][4] = cost[4][3] = 3; cost[1][4] = cost[4][1] = 3;}int main(){ int x; init(); while(scanf("%d",&x) && x) { int k = 1; a[k++] = x; while(scanf("%d",&x) && x) a[k++] = x; int m = k-1; memset(dp,inf,sizeof(dp)); dp[1][a[1]][0] = dp[1][0][a[1]] = 2; for(int k=2; k<=m; k++) { for(int j=0; j<=4; j++) { if(dp[k-1][a[k-1]][j] != 0) { dp[k][a[k]][j] = min(dp[k][a[k]][j],dp[k-1][a[k-1]][j] + cost[a[k-1]][a[k]]);//打印出来看,后面的值有可能覆盖前面的值,所以要每次取最小值 dp[k][a[k-1]][a[k]] =min(dp[k][a[k-1]][a[k]],dp[k-1][a[k-1]][j] + cost[j][a[k]]); } if(dp[k-1][j][a[k-1]] != 0) { dp[k][j][a[k]] = min(dp[k][j][a[k]],dp[k-1][j][a[k-1]] + cost[a[k-1]][a[k]]); dp[k][a[k]][a[k-1]] = min(dp[k][a[k]][a[k-1]],dp[k-1][j][a[k-1]] + cost[j][a[k]]); } } } int max1 = inf; for(int i=0; i<=4; i++) { max1 = min(max1,dp[m][a[m]][i]); max1 = min(max1,dp[m][i][a[m]]); } printf("%d\n",max1); } return 0;}

错误代码

#include #include #include #include #include #include #include #include #include #include #include const int inf = 0x3f3f3f3f;//1061109567typedef long long LL;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int cost[5][5];int dp[10010][4][4];int a[10010];void init(){ for(int i=1; i<=4; i++) { cost[0][i] = 2; cost[i][i] = 1; } cost[1][3] = cost[3][1] = 4; cost[2][4] = cost[4][2] = 4; cost[1][2] = cost[2][1] = 3; cost[2][3] = cost[3][2] = 3; cost[3][4] = cost[4][3] = 3; cost[1][4] = cost[4][1] = 3;}int main(){ int x; init(); while(scanf("%d",&x) && x) { int k = 1; a[0] = 0; a[k++] = x; while(scanf("%d",&x) && x) a[k++] = x; int m = k-1; memset(dp,0,sizeof(dp)); dp[1][a[1]][0] = dp[1][0][a[1]] = 2; for(int k=2; k<=m; k++) { for(int j=0; j<=4; j++) { if(!dp[k-1][a[k-1]][j]) continue; dp[k][a[k]][j] = dp[k-1][a[k-1]][j] + cost[a[k-1]][a[k]]; printf("%d %d %d %d\n",k,a[k],j, dp[k][a[k]][j]); dp[k][a[k-1]][a[k]] = dp[k-1][a[k-1]][j] + cost[j][a[k]]; printf("%d %d %d %d\n",k,a[k-1],a[k], dp[k][a[k-1]][a[k]]); if(!dp[k-1][j][a[k-1]]) continue; dp[k][j][a[k]] = dp[k-1][j][a[k-1]] + cost[a[k-1]][a[k]]; printf("%d %d %d %d\n",k,j,a[k],dp[k][j][a[k]] ); dp[k][a[k]][a[k-1]] = dp[k-1][j][a[k-1]] + cost[j][a[k]]; printf("%d %d %d %d\n",k,a[k],a[k-1],dp[k][a[k]][a[k-1]]); } } int max1 = inf; for(int i=0; i<=4; i++) { if(!dp[m][a[m]][i]) continue; printf("aaaa%d\n",dp[m][a[m]][i]); max1 = min(max1,dp[m][a[m]][i]); if(!dp[m][i][a[m]]) continue; printf("aaaa%d\n",dp[m][i][a[m]]); max1 = min(max1,dp[m][i][a[m]]); } printf("%d\n",max1); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序标签、属性的智能补全(同时支持原生小程序、mpvue 和 wepy 框架,并提供 snippets)(微信小程序绑定属性)
下一篇:HDU 3402 Ants run!(比较好的贪心)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~