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的更多相关文章
随机推荐
- 动手动脑(lesson 8)
一. 上面程序在不注释第一个i/j会出错,这是因为程序会顺序运行,在运行到try之前就已经出错,因此不会跳到异常处理. 异常处理基础知识: 二. 三. 运行结果: 运行结果: 四. 运行结果: 总结: ...
- day89
跨域问题 同源策略(Same origin policy) 是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之上 ...
- LOJ540 游戏 构造
传送门 题意:给出$N$,试构造一个点数小于$500$的图,使得其中三元环的个数恰好为$N$.$N \leq 2 \times 10^6$ 首先构造一个尽可能大的完全图,然后在这个完全图旁边加点.尽可 ...
- (转)linux sudo 重定向,实现只有系统管理员才有权限操作的文件中写入信息
众所周知,使用 echo 并配合命令重定向是实现向文件中写入信息的快捷方式. 本文介绍如何将 echo 命令与 sudo 命令配合使用,实现向那些只有系统管理员才有权限操作的文件中写入信息. 比如 ...
- 解决webapi首次启动速度慢的问题 - z
原理与下面两篇文章提及的相同 https://blog.csdn.net/godcyx/article/details/38517135 http://www.huaface.com/p/12
- Ionic Contoller类与Service类分开需要注意的问题
看了别人的项目把Controller类和Service类都写在了app.js文件里面,这不符合我的风格,想把他们分开成单独的文件,确遇见以下错误提示: ionic.bundle.min.js:133 ...
- [Oracle][Metadata]如何查找与某一个功能相关的数据字典名
当Oracel的一个新功能出来的时候,我们可能不知道所有与此功能关联的数据字典名称,那么如何才能得到这些 meta data 的 meta data 呢? 可以通过 dicitonary 来查看: 例 ...
- [Oracle]发生 ora-06502 RMAN 在对 catalog DB 同期时出错的调查方法
Catalog DB resync error: 1, setting on the server that starts the RMAN client $ Export EVENT_10928 = ...
- [数据可视化之一]Pandas单变量画图
Pandas单变量画图 Bar Chat Line Chart Area Chart Histogram df.plot.bar() df.plot.line() df.plot.area() df. ...
- MongoDB的导入导出
一:MongoDB的导出功能 mongodb提供了导入和导出的功能,分别是MongoDB下载目录下的mongoexport.exe和mongoimport.exe文件 ,具体的导出命令格式如下: mo ...