codeforces586B
Laurenty and Shop
A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 ≤ i ≤ 2, 1 ≤ j ≤ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 ≤ j ≤ n). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.
Input
The first line of the input contains integer n (2 ≤ n ≤ 50) — the number of houses in each row.
Each of the next two lines contains n - 1 space-separated integer — values aij (1 ≤ aij ≤ 100).
The last line contains n space-separated integers bj (1 ≤ bj ≤ 100).
Output
Print a single integer — the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.
Examples
4
1 2 3
3 2 1
3 2 2 3
12
3
1 2
3 3
2 1 3
11
2
1
1
1 1
4
Note
The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
- Laurenty crosses the avenue, the waiting time is 3;
- Laurenty uses the second crossing in the first row, the waiting time is 2;
- Laurenty uses the first crossing in the first row, the waiting time is 1;
- Laurenty uses the first crossing in the first row, the waiting time is 1;
- Laurenty crosses the avenue, the waiting time is 1;
- Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4.
sol:只有两行根本用不着写最短路,弄一个前缀和和一个后缀和扫两遍,找到最短路和次短路即可
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,inf=0x3f3f3f3f;
int n,Dis[][N],Time[N];
int Qzh[][N],Hzh[][N];
int main()
{
int i,j,ans=;
R(n);
for(i=;i<=;i++)
{
for(j=;j<=n;j++) R(Dis[i][j]);
}
for(i=;i<=;i++)
{
Qzh[i][]=; for(j=;j<=n;j++) Qzh[i][j]=Qzh[i][j-]+Dis[i][j];
Hzh[i][n]=; for(j=n-;j>=;j--) Hzh[i][j]=Hzh[i][j+]+Dis[i][j+];
}
for(i=;i<=n;i++) R(Time[i]);
int Zuix=-,Cix=-;
for(i=;i<=n;i++) if(Zuix==-||Hzh[][i]+Time[i]+Qzh[][i]<Hzh[][Zuix]+Time[Zuix]+Qzh[][Zuix])
{
Zuix=i;
}
ans+=Hzh[][Zuix]+Time[Zuix]+Qzh[][Zuix];
for(i=;i<=n;i++) if(Cix==-||Qzh[][i]+Time[i]+Hzh[][i]<Qzh[][Cix]+Time[Cix]+Hzh[][Cix])
{
if(i==Zuix) continue;
Cix=i;
}
ans+=Qzh[][Cix]+Time[Cix]+Hzh[][Cix];
Wl(ans);
return ;
}
/*
input
4
1 2 3
3 2 1
3 2 2 3
output
12 input
3
1 2
3 3
2 1 3
output
11 input
2
1
1
1 1
output
4
*/
codeforces586B的更多相关文章
随机推荐
- PAT A1136 A Delayed Palindrome (20 分)——回文,大整数
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- Android PageAdapter翻译
介绍:ViewPager和PagerAdapter结合使用 public abstract class PagerAdapter extends Object java.lang.Object ...
- P4111 [HEOI2015]小Z的房间
你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着.你想要打通一些相邻房间的墙,使得所有房间能够互相到达.在此过程中,你不能把 ...
- sprintf()函数用法
sprintf()用法见操作手册:http://www.php.net/sprintf 简单写下format的用法: 1. + - 符号,数字 2. 填充字符 默认是空格,可以是0.如果其他字符填充, ...
- odoo学习之弹框显示
按条件隐藏: <xpath expr="//group[1]" position="attributes"> <attribute name= ...
- BZOJ3714 PA2014 Kuglarz 最小生成树
题目传送门 题意:有$N$个盒子,每个盒子中有$0$或$1$个球.现在你可以花费$c_{i,j}$的代价获得$i$到$j$的盒子中球的总数的奇偶性,求最少需要多少代价才能知道哪些盒子中有球.$N \l ...
- angularjs呼叫Web API
今早有分享一篇<创建Web API并使用>http://www.cnblogs.com/insus/p/7771428.html 接下来,我再分享一篇,怎样在angularjs去呼叫Web ...
- Idea Live Template代码片段总结
目录 Idea Live Template总结 一.演示 二.详细介绍 2.1 类型 2.2设置(win默认快捷键win+alt+s) 2.3 快捷键 2.4 实战 Idea Live Templat ...
- Linux查看特定端口是否被占用并kill掉相关进程
今天在搭建Zookeeper集群的时候,需要频繁启动zookeeper,但是启动的时候,有时会提示下列错误信息: zookeeper需要的地址已经被占用了,其实是因为上一次的zookeeper没有关闭 ...
- 汇编 STOSB, STOSW, STOSD指令
知识点: 汇编指令 STOSB STOSW STOSD 一.汇编指令STOSB 004113AC 8DBD B4FEFFFF LEA EDI,DWORD PTR SS:[EBP-14C] 004113 ...