uva10564
路径条数很好找。记录最小路径,就记录到各点的最小字符串,存储起来。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,INF=0x7FFFFFFF,mod=;
lon n,k,arr[SZ][SZ],dp[SZ][SZ][];
string str[SZ][SZ][]; void init()
{
memset(dp,,sizeof(dp));
for(int i=;i<=*n-;++i)
{
for(lon j=;j<=abs(n-i)+;++j)
{
cin>>arr[i][j];
}
}
for(lon j=;j<SZ;++j)
{
if(j&)dp[][j][]=;
}
} string toStr(lon x)
{
if(x==)return "";
string res="";
for(;x;)
{
res+=''+x%;
x/=;
}
reverse(res.begin(),res.end());
return res;
} bool cmp(const string &x,const string &y)
{
int pos1=x.find(" ");
int pos2=y.find(" ");
if(pos1!=pos2)return pos1<pos2;
else return x<y;
} void work()
{
for(lon i=;i<=*n-;++i)
{
for(lon j=;j<=abs(n-i)+;++j)
{
lon cur=arr[i][j];
for(lon p=arr[i][j];p<=k;++p)
{
if(i<=n)
{
dp[i][j][p]+=dp[i-][j][p-cur];
dp[i][j][p]+=dp[i-][j+][p-cur];
if(i==)
{
str[i][j][p]=toStr(j-)+" ";
}
else if(dp[i-][j][p-cur]&&dp[i-][j+][p-cur])
{
if(cmp(str[i-][j][p-cur],str[i-][j+][p-cur]))
{
str[i][j][p]=str[i-][j][p-cur]+"R";
}
else
{
str[i][j][p]=str[i-][j+][p-cur]+"L";
}
}
else if(dp[i-][j][p-cur])
{
str[i][j][p]=str[i-][j][p-cur]+"R";
}
else if(dp[i-][j+][p-cur])
{
str[i][j][p]=str[i-][j+][p-cur]+"L";
}
}
else
{
dp[i][j][p]+=dp[i-][j-][p-cur];
dp[i][j][p]+=dp[i-][j][p-cur];
if(i==)
{
str[i][j][p]=toStr(j-)+" ";
}
else if(dp[i-][j-][p-cur]&&dp[i-][j][p-cur])
{
if(cmp(str[i-][j-][p-cur],str[i-][j][p-cur]))
{
str[i][j][p]=str[i-][j-][p-cur]+"R";
}
else
{
str[i][j][p]=str[i-][j][p-cur]+"L";
}
}
else if(dp[i-][j-][p-cur])
{
str[i][j][p]=str[i-][j-][p-cur]+"R";
}
else if(dp[i-][j][p-cur])
{
str[i][j][p]=str[i-][j][p-cur]+"L";
}
}
}
}
}
lon res=;
char tmp=(char)(''+);
string minstr="~~~~~~ ";
for(lon j=;j<=n;++j)
{
res+=dp[*n-][j][k];
if(dp[*n-][j][k])
{
//cout<<" "<<str[2*n-1][j][k]<<" "<<(str[2*n-1][j][k]=="")<<endl;
minstr=min(minstr,str[*n-][j][k],cmp);
}
}
cout<<res<<endl;
if(res)cout<<minstr<<endl;
else cout<<endl;
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon casenum;
//cin>>casenum;
//cout<<casenum<<endl;
//for(lon time=1;time<=casenum;++time)
for(lon time=;cin>>n>>k,n;++time)
{
init();
work();
}
return ;
}
uva10564的更多相关文章
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
- UVA - 10564 Paths through the Hourglass
传送门:https://vjudge.net/problem/UVA-10564 题目大意:给你一张形如沙漏一般的图,每一个格子有一个权值,问你有多少种方案可以从第一行走到最后一行,并且输出起点最靠前 ...
随机推荐
- RedHat7安装Docker
RedHat 启动 docker报错: 错误:Error starting daemon: SELinux is not supported with the overlay2 graph drive ...
- 如果此表在它的 ChildRelation 集合中不是父表,则不能将关系添加到该集合中。
今天遇到这个问题头都大了,百度上也没找到解决方案,就自己在哪里沉思................ 终于皇天不负有心人,被我解决了! 这是调用ChildRelations.Add(“名字”,“父级”, ...
- Python3 tkinter基础 Button command 单击按钮 在console中打印文本
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 【入门】Gradle的基本使用、在IDEA中的配置、常用命令
一.介绍 java的源码构建工具,大致经历了 ant -> maven -> gradle 这个过程,每一次进步,都是在解决之前的工具所带来的问题,简单来说: 1. ant 功能虽然也很强 ...
- samtools can not find libbz2.so.1.0
Error: samtoolssamtools: error while loading shared libraries: libbz2.so.1.0: cannot open shared obj ...
- [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))
题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https:// ...
- 【模板】关于c++的代码模板
收集了点代码模板,现在整理在这里. 目录: 大数阶乘 高精度(大数乘小数) 高精度(大数乘大数) 高精度(加法) 高精度(减法) 进制转化 最大公约数 最小公倍数 组合序列 未完待续
- HashMap 和 HashTable 的区别
1. 存储结构 HashMap HashTable 数组 + 链表/红黑树 数组 + 链表 HashMap的存储规则: 优先使用数组存储, 如果出现Hash冲突, 将在数组的该位置拉伸出链表进行存储( ...
- Images之管理image
Manage images The easiest way to make your images available for use by others inside or outside your ...
- gawk命令详解
GNU awk: sort.cut.uniq.wc等参考: https://blog.csdn.net/lk07828/article/details/46324807 https://blog.cs ...