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对码头,每个相邻码头之间需要一定时间到达, ...
随机推荐
- JavaScript(1)——变量、函数声明及作用域
这是我的第一篇博客文章,本人不才,文笔也不好,所以可能写的有点凌乱.有什么不对的地方还望见谅.不过每天进步一小步,总有一天会迈出那一大步.以下内容是我对变量.函数声明及函数表达式.作用域的理解. [变 ...
- html 7.29
4.请判断以下说法是否正确:HTML 会被 XHTML 取代. 您的回答:错误 正确答案:正确 9.请判断以下说法是否正确:DOCTYPE 没有关闭标签. 您的回答:错误 正确答案:正确 13.下列哪 ...
- JS定时器的使用--数码时钟
<title>无标题文档</title> <script> function toDou(n){ if(n<10){ return '0'+n; }else{ ...
- hadoop中联结不同来源数据
装载自http://www.cnblogs.com/dandingyy/archive/2013/03/01/2938462.html 有时可能需要对来自不同源的数据进行综合分析: 如下例子: 有Cu ...
- HDU 3594 Cactus (强连通分量 + 一个边只能在一个环里)
题意:判断题目中给出的图是否符合两个条件.1 这图只有一个强连通分量 2 一条边只能出现在一个环里. 思路:条件1的满足只需要tarjan算法正常求强连通分量即可,关键是第二个条件,我们把对边的判断转 ...
- plat模板修改记录
每个栏目显示文章数 homepage.hph 94行 $loop = new WP_Query('post_type=post&showposts=6&category_name=xi ...
- springMVC如何访问静态文件
在进行Spring MVC的配置时,通常我们会配置一个dispatcher servlet用于处理对应的URL.配置如下:<servlet><servlet-name>mvc- ...
- width为auto或者100%的区别
一.四个理论 1. 某div不显示设置宽度,那么width为auto. 2. 某div的width在默认情况设置的是盒子模型中content的值 3. 某div的width为100%表示的是此div盒 ...
- create a new table for the query results
http://stackoverflow.com/questions/2698401/how-to-store-mysql-query-results-in-another-table CREATE ...
- jquery 延迟执行实例介绍
代码如下: $(function(){ var $inputs = $('input[type=button]') .delay(500) .queue(function(){$(this).hide ...