双塔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的更多相关文章

  1. POJ 2609 Ferry Loading(双塔DP)

    Ferry Loading Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1807   Accepted: 509   Sp ...

  2. poj 2336 Ferry Loading II ( 【贪心】 )

    Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3704   Accepted: 1884 ...

  3. Ferry Loading III[HDU1146]

    Ferry Loading IIITime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. POJ-2336 Ferry Loading II(简单DP)

    Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3763 Accepted: 1919 Desc ...

  5. poj-2336 Ferry Loading II(dp)

    题目链接: Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3946   Accepted: ...

  6. TOJ 2419: Ferry Loading II

    2419: Ferry Loading II  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Subm ...

  7. Ferry Loading II_贪心

    Description Before bridges were common, ferries were used to transport cars across rivers. River fer ...

  8. [POJ2336]Ferry Loading II

    题目描述 Description Before bridges were common, ferries were used to transport cars across rivers. Rive ...

  9. POJ 3377 Ferry Lanes

    虽然它出现在dp专场里···但是我第一反应是一道最短路题···不过幸好它出现在dp专场里···因为我不怎么会dijstra什么的··· 题意:一条河上有N+1对码头,每个相邻码头之间需要一定时间到达, ...

随机推荐

  1. UIImage+Scale

    Scale a UIImage to any given rect keeping the aspect ratio Raw  UIImage+Scale.m   @implementation UI ...

  2. Swift 学习笔记 (二)

    原创:转载请注明出处 41.闭包表达式语法(Closure Expression Syntax) 闭包表达式语法有如下一般形式: { (parameters) -> returnType in ...

  3. Android简单逐帧动画Frame的实现(二)

    Android简单逐帧动画Frame的实现   Android简单逐帧动画Frame的实现 1.逐帧动画 即是通过播放预先排序好的图片来实现动态的画面,感觉像是放电影. 2.实现步骤: 1. 在工程里 ...

  4. socket通信的json数据传输与获取

    本文是基于scoket通信的tcp来进行数据的json格式传输与获取的. 首先,我们先要下载AsyncSockethttps://github.com/robbiehanson/CocoaAsyncS ...

  5. FZU 1894 志愿者选拔(优化循环)

    我并没有多想,他们是用的数组模拟队列,然而我就是优化循环次数过得 #include<iostream> #include<cstdio> #include<cstring ...

  6. IDL 计算TVDI

    介绍请看:http://blog.sina.com.cn/s/blog_764b1e9d0100wdrr.html 源码: IDL 源码PRO TVDI,NDVI,LST,NBINS,RES RES ...

  7. jq判断元素是否显示

    为了判断元素是否显示,jquery中用is()来实现, $(function(){ $(obj).bind('click',function(){ if(obj.is(:visible)){ //编写 ...

  8. php源码分析之php_info输出中css样式是怎么来的

    我们经常使用echo phpinfo();查看php的配置信息,但是大家知道里面的css样式是怎么来的吗? 我们查看源码(php源码/ext/standard/css.c) PHPAPI void p ...

  9. 利用XShell 上传和下载文件

    原文:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28977986&id=4292781 借助XShell,使用li ...

  10. Python3基础 函数 关键字参数 的示例

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...