POJ 3377 Ferry Lanes
虽然它出现在dp专场里···但是我第一反应是一道最短路题···不过幸好它出现在dp专场里···因为我不怎么会dijstra什么的···
题意:一条河上有N+1对码头,每个相邻码头之间需要一定时间到达,每对码头之间也需要一定时间到达,如下图:
给出起点码头和终点码头,问最短到达时间。
解法:假设起点码头一定在左边,终点码头一定在右边,那么为了时间最短一定是向右或向上(下)走,所以dp数组为每个从起点出发到每个码头时的最短耗时,dp方程很好确定,但是上下码头的顺序无法确定,而当路径是由对面码头过来的时候,对面码头的dp值一定不是从当前码头而来,dp方程dp[0][i] = min(dp[0][i - 1] + time[0][i - 1], dp[1][i - 1] + time[1][i - 1] + time[2][i]),dp[1][i]
= min(dp[1][i - 1] + time[1][i - 1], dp[0][i - 1] + time[0][i - 1] + time[2][i])。
但从起点(终点)到它对面的时间需要另外处理,例如如下情况:
显然从南方的码头出发向右绕一圈后所需时间较短,所以要分别对起点通过左面路径到达对面码头的最短时间和终点通过右面路径到达对面码头的最短时间进行处理,绕行走的一定是U形路,所以只要计算所有U形路的最小时间,不过我还是用的dp写的···不是很好理解···
对于终点的绕行时间在最后要特殊处理,因为绕行时间和中间路径的dp过程有矛盾的地方。
一开始TLE了···但是我听说了某喵的惨痛经历后把memset改掉之后1900msA掉了···郁闷
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
struct node
{
long long num;
int dir;
};
long long dp[2][1000005];
long long c[3][1000005];
int main()
{
int n;
while(~scanf("%d", &n) && n)
{
for(int i = 0; i < 2; i++)
for(int j = 0; j < 1000005; j++)
dp[i][j] = -1;
//memset(dp, -1, sizeof(dp));
node st, fi;
scanf("%d%lld%d%lld", &st.dir, &st.num, &fi.dir, &fi.num);
if(st.num > fi.num)
{
node tmp = st;
st = fi;
fi = tmp;
}
dp[st.dir][st.num] = 0;
dp[fi.dir][fi.num] = 0;
for(int i = 0; i < n; i++)
scanf("%lld", &c[0][i]);
for(int i = 0; i <= n; i++)
scanf("%lld", &c[2][i]);
for(int i = 0; i < n; i++)
scanf("%lld", &c[1][i]);
for(int i = st.num - 1; i >= 0; i--)
dp[st.dir][i] = c[st.dir][i] + dp[st.dir][i + 1];
dp[!st.dir][0] = c[2][0] + dp[st.dir][0];
for(int i = 1; i <= st.num; i++)
dp[!st.dir][i] = min(dp[st.dir][i] + c[2][i], dp[!st.dir][i - 1] + c[!st.dir][i - 1]);
for(int i = fi.num + 1; i <= n; i++)
dp[fi.dir][i] = c[fi.dir][i - 1] + dp[fi.dir][i - 1];
dp[!fi.dir][n] = c[2][n] + dp[fi.dir][n];
for(int i = n - 1; i >= fi.num; i--)
dp[!fi.dir][i] = min(dp[fi.dir][i] + c[2][i], dp[!fi.dir][i + 1] + c[!fi.dir][i]);
for(int i = st.num + 1; i < fi.num; i++)
{
dp[0][i] = min(dp[0][i - 1] + c[0][i - 1], dp[1][i - 1] + c[1][i - 1] + c[2][i]);
dp[1][i] = min(dp[1][i - 1] + c[1][i - 1], dp[0][i - 1] + c[0][i - 1] + c[2][i]);
}
if(fi.num > 0)
{
dp[!fi.dir][fi.num] += dp[!fi.dir][fi.num - 1] + c[!fi.dir][fi.num - 1];
dp[fi.dir][fi.num] = min(dp[!fi.dir][fi.num], dp[fi.dir][fi.num - 1] + c[fi.dir][fi.num - 1]);
}
else
dp[fi.dir][fi.num] = dp[!fi.dir][fi.num];
cout << dp[fi.dir][fi.num] << endl;
}
return 0;
}
不想写注释了···就算写了我的代码也不会变的好读的QAQ
_(:з」∠)_
POJ 3377 Ferry Lanes的更多相关文章
- POJ 2609 Ferry Loading(双塔DP)
Ferry Loading Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1807 Accepted: 509 Sp ...
- POJ 2609 Ferry Loading
双塔DP+输出路径. 由于内存限制,DP只能开滚动数组来记录. 我的写法比较渣,但是POJ能AC,但是ZOJ依旧MLE,更加奇怪的是Uva上无论怎么改都是WA,其他人POJ过的交到Uva也是WA. # ...
- poj 2336 Ferry Loading II ( 【贪心】 )
Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3704 Accepted: 1884 ...
- 【POJ3377】Ferry Lanes 最短路
我仅仅是贴一下手写堆优化的dij模板.尽管.它.TLE了--**** #include <cstdio> #include <cstring> #include <ios ...
- POJ 3895 Cycles of Lanes (dfs)
Description Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of t ...
- poj 动态规划题目列表及总结
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- poj动态规划列表
[1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...
- POJ 动态规划题目列表
]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...
- poj 动态规划的主题列表和总结
此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...
随机推荐
- ASIHTTPRequest的使用(转)
转载自:http://fushengfei.iteye.com/blog/1147112 博客分类: IOS 原文地址:http://wiki.magiche.net/pages/viewpage ...
- 【学习总结】OS X , IOS , IOS SDK , XCode之间的关系
几个基本的概念 : OS X : 属于桌面PC级别(IMac,MacPro等)对应安装的操作系统 IOS : 属于移动设备级别(Iphone,Ipad等)对应安装的操作系统 XCode: 是一个IDE ...
- 【POJ2104】kth num
You are working for Macrohard company in data structures department. After failing your previous tas ...
- c# Oracle 远程连接方式 plsql 连接oracle 11g 64位
1.本地连接字符串: string connect = "Data Source=orcl;user=XXX;password=XXX;Persist Security Info=Tru ...
- K-meams文本聚类算法C++实现
FROM:http://www.cnblogs.com/finallyliuyu/archive/2010/09/03/1817348.html 头文件: #ifndef _Preprocess_H ...
- 把Ubuntu打造成Mac Macbuntu
Macbuntu:把 Ubuntu 一键打造成完整的 Mac 风格 23八 2010 # 作者:riku/ 本文采用CC BY-NC-SA 2.5协议授权,转载请注明本文链接. Macbuntu 是一 ...
- Eat the Trees hdu 1693
Problem DescriptionMost of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...
- 1024: [SCOI2009]生日快乐 - BZOJ
Description windy的生日到了,为了庆祝生日,他的朋友们帮他买了一个边长分别为 X 和 Y 的矩形蛋糕.现在包括windy,一共有 N 个人来分这块大蛋糕,要求每个人必须获得相同面积的蛋 ...
- 15个带示例的jQuery滚动条插件
1.NiceScroll:可用于桌面.移动与触摸设备的jQuery滚动插件 NiceScroll是一个jQuery插件(since 1.5),它有着类似于ios/移动设备的样式.它支持Div.iFra ...
- XSS与CSRF两种跨站攻击比较
XSS:跨站脚本(Cross-site scripting) CSRF:跨站请求伪造(Cross-site request forgery) 在那个年代,大家一般用拼接字符串的方式来构造动态SQL 语 ...