POJ 2609 Ferry Loading
双塔DP+输出路径。
由于内存限制,DP只能开滚动数组来记录。
我的写法比较渣,但是POJ能AC,但是ZOJ依旧MLE,更加奇怪的是Uva上无论怎么改都是WA,其他人POJ过的交到Uva也是WA。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<algorithm>
using namespace std; int dp[][+];
struct P
{
int x,y;
int f;
}pre[][+];
int sum;
int a[],tot; void read()
{
while()
{
scanf("%d",&a[tot+]);
if(a[tot+]==) break;
tot++;
} for(int i=;i<=tot;i++)
for(int j=;j<=*sum;j++)
pre[i][j].f=-; for(int j=;j<=*sum;j++) dp[][j]=dp[][j]=0x7FFFFFFF;
} void init()
{
sum=sum*;
tot=;
} void work()
{
int now=;
dp[][a[]+sum]=;
pre[][a[]+sum].f=;
dp[][-a[]+sum]=;
pre[][-a[]+sum].f=; for(int i=;i<=tot;i++)
{
now=now^;
for(int j=;j<=*sum;j++) dp[now][j]=0x7FFFFFFF;
for(int j=;j<=*sum;j++)
{
if(dp[now^][j]==0x7FFFFFFF) continue; int tmp=j-sum; if(tmp>=)
{
if(dp[now^][j]+tmp+a[i]<=sum&&dp[now^][j]<dp[now][tmp+a[i]+sum])
{
dp[now][tmp+a[i]+sum]=dp[now^][j];
pre[i][tmp+a[i]+sum].x=i-;
pre[i][tmp+a[i]+sum].y=j;
pre[i][tmp+a[i]+sum].f=;
} if(max(tmp,a[i])+dp[now^][j]<=sum&&dp[now^][j]+min(tmp,a[i])<dp[now][tmp-a[i]+sum])
{
dp[now][tmp-a[i]+sum]=dp[now^][j]+min(tmp,a[i]);
pre[i][tmp-a[i]+sum].x=i-;
pre[i][tmp-a[i]+sum].y=j;
pre[i][tmp-a[i]+sum].f=;
}
} else
{
tmp=-tmp;
if(dp[now^][j]+tmp+a[i]<=sum&&dp[now^][j]<dp[now][-(tmp+a[i])+sum])
{
dp[now][-(tmp+a[i])+sum]=dp[now^][j];
pre[i][-(tmp+a[i])+sum].x=i-;
pre[i][-(tmp+a[i])+sum].y=j;
pre[i][-(tmp+a[i])+sum].f=;
} if(max(tmp,a[i])+dp[now^][j]<=sum&&dp[now^][j]+min(tmp,a[i])<dp[now][a[i]-tmp+sum])
{
dp[now][a[i]-tmp+sum]=dp[now^][j]+min(tmp,a[i]);
pre[i][a[i]-tmp+sum].x=i-;
pre[i][a[i]-tmp+sum].y=j;
pre[i][a[i]-tmp+sum].f=;
} }
}
}
} void output()
{
bool flag=;
int now_i,now_j;
for(int i=tot;i>=;i--)
{
for(int j=*sum;j>=;j--)
{
if(pre[i][j].f!=-)
{
now_i=i;
now_j=j;
flag=;
break;
}
}
if(flag) break;
} if(flag==) {printf("0\n");return;}
printf("%d\n",now_i); stack<int>S; while()
{
if(pre[now_i][now_j].f==-) break;
S.push(pre[now_i][now_j].f); int newx,newy;
newx=pre[now_i][now_j].x;
newy=pre[now_i][now_j].y;
now_i=newx;
now_j=newy;
} int k=;
int left=;
int right=; while(!S.empty())
{
if(S.top()==)
{
printf("port\n");
left=left+a[k];k++;
}
else
{
printf("starboard\n");
right=right+a[k];k++;
}
S.pop();
}
} int main()
{
scanf("%d",&sum);
init();
read();
work();
output();
return ;
}
POJ 2609 Ferry Loading的更多相关文章
- POJ 2609 Ferry Loading(双塔DP)
Ferry Loading Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1807 Accepted: 509 Sp ...
- poj 2336 Ferry Loading II ( 【贪心】 )
Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3704 Accepted: 1884 ...
- Ferry Loading III[HDU1146]
Ferry Loading IIITime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- POJ-2336 Ferry Loading II(简单DP)
Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3763 Accepted: 1919 Desc ...
- poj-2336 Ferry Loading II(dp)
题目链接: Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3946 Accepted: ...
- TOJ 2419: Ferry Loading II
2419: Ferry Loading II Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Subm ...
- Ferry Loading II_贪心
Description Before bridges were common, ferries were used to transport cars across rivers. River fer ...
- [POJ2336]Ferry Loading II
题目描述 Description Before bridges were common, ferries were used to transport cars across rivers. Rive ...
- POJ 3377 Ferry Lanes
虽然它出现在dp专场里···但是我第一反应是一道最短路题···不过幸好它出现在dp专场里···因为我不怎么会dijstra什么的··· 题意:一条河上有N+1对码头,每个相邻码头之间需要一定时间到达, ...
随机推荐
- 门面模式(Facade)解析
门面模式使用一个门面类来包装一些复杂的类,对外提供一个简单的访问方法. 见如下代码: class CPU { public void startup() { System.out.println(&q ...
- web配置文件的<load-on-startup>0</load-on-startup>
在servlet的配置当中,<load-on-startup>5</load-on-startup>的含义是:标记容器是否在启动的时候就加载这个servlet.当值为0或者大于 ...
- 下拉刷新和上拉加载 Swift
转载自:http://iyiming.me/blog/2015/07/05/custom-refresh-and-loading/ 关于下拉刷新和上拉加载,项目中一直使用MJRefresh(原先还用过 ...
- AutoTile 自动拼接(一) 学习与实践
恩,大家好,这两天江苏冷空气袭击,下了今年 第一场第二场雪. 不过今天我要说的 ,和 上面的 屁关系都没有. 今天要说的是 2d无缝自动拼接.大家有没有玩过 RPG Maker VX Ace. 类似 ...
- HDU 4738 Caocao's Bridges(割边)
乍一看一个模板题,仔细一看还是模板题,但是三个坑.1,不是连通图,放0个.2 守卫为0,放1个. 3注意重边. #include<iostream> #include<cstdio& ...
- 4--OC --合成存取器方法
1. 从OC 2.0开始就已经可以自动生成设置函数方法和获取函数方法(统称为存取器方法). 什么是 @property 和 @synthesize ? @property 和 @synthesize ...
- AndroidGradle--瘦身apk(转发)
apk瘦身一般有两条线, 去除无用的代码,例如引用一个比较大的lib,只使用了其中很少的功能.其他无用的代码可以想办法去掉 去除无用的资源文件,可能是第三方lib中的,也有可能是开发中引入了无用的资源 ...
- 完整的 HTML 4 + HTML 5 实体参考手册
1 完整的 HTML 4 + HTML 5 实体参考手册 http://www.runoob.com/charsets/html-charsets.html 1 下表中的所有实体都能在所有的浏览器中正 ...
- Java的SSH框架
SSH 为 struts+spring+hibernate的一个集成框架,是目前较流行的一种Web应用程序开源框架. 1.业务流程 集成SSH框架的系统从职责上分为四层:表示层.业务逻辑层.数据 ...
- mysql show processlist详解
SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPER权限,您可以看到所有线程.否则,您只能看到您自己的线程( ...