双塔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. ABI & API

    API defines the programning language and function entry point, arguments type, order. ABI defines th ...

  2. awstats + tomcat + windows

    下载: 1.apache-tomcat-7.0.67 2.ActivePerl-5.22.1.2201-MSWin32-x86-64int-299574.msi 3.awstats-7.4.zip 修 ...

  3. NSUserDefault -- synchronize 浅析

    NSUserDefault的使用比较简单:NSUserDefaults *mySettingData = [NSUserDefaults standardUserDefaults];  创建NSUse ...

  4. android笔记20170116

    封装http请求类,利用回调机制获取返回值 public interface HttpCallbackListener { void onFinish(String response); void o ...

  5. Android开发 R cannot be resolved to a variable问题的分析

    R文件是系统自动生成的,如果没出现的话,你的XML文件是不是有错误?是否之前修改过res文件夹下面.xml文件 R文件没有生成的情况有几种: 1.项目没有自动编译:这种时候只需要简单的编译一下工程就会 ...

  6. Django - 通用视图

    urls.py from . import views ... url(r'^$', views.IndexView.as_view, name="index"), url(r'^ ...

  7. Codeforces 691A Fashion in Berland

    水题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...

  8. CentOS Hadoop安装配置详细

    总体思路,准备主从服务器,配置主服务器可以无密码SSH登录从服务器,解压安装JDK,解压安装Hadoop,配置hdfs.mapreduce等主从关系. 1.环境,3台CentOS7,64位,Hadoo ...

  9. ubuntu中文论坛

    http://forum.ubuntu.org.cn/index.php?sid=e40344219c81dbc4b289135a71db4efd

  10. jQuery validation

    之前做客户端验证感觉自己javascript 不行,虽然能写出来一完整的验证,但从不自信,一直觉得客户端验证是比较繁琐的事情,但是又不能不做,只到最开始接触ajax ,遇到了一个jQuery vali ...