CodeForces#325 B. Laurenty and Shop

本文介绍了一个关于游戏寻路的问题,玩家需要从特定位置出发,经过不同的道路和等待时间,到达商店并返回,要求找到最短的总等待时间。文章通过使用前缀和的方法解决了这一问题,并提供了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击就送屠龙宝刀

B. Laurenty and Shop

time limit per test 1 second
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.
Sample test(s)
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

题目大意:你要从(2,n)走到(1,1)点两次,要求路径不完全重复,只能通过中间的过道一次。求最小和。(英语帝勿喷这只是简要的内容)

解题思路:
看到要求最小第一反应是最短路,然而最短路你就废了。比赛时长只有两个小时,如果你跟我一样入了最短路的坑恭喜你起码半小时已经没了。因为过道每次只允许通过一次所以最短路显然不是正解。
现在开始说正解:前缀和。
下面贴代码
(什么??!没听懂???!把图画出来前缀和写出来你就明白了!!)

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
int a[maxn],b[maxn],c[maxn];
int sa[maxn]= {0},sb[maxn]= {0};
int get(int i)
{
    return sa[i-1]+sb[i]+c[i];
}
int main()
{
    int n;
    cin>>n;
    for(int i=1; i<=n-1; ++i)
    {
        cin>>a[i];
        sa[i]=sa[i-1]+a[i];
    }
    for(int i=1; i<=n-1; ++i) cin>>b[i];
    for(int i=n-1; i>=1; --i)
    {
        sb[i]=sb[i+1]+b[i];
    }
    for(int i=1; i<=n; ++i) cin>>c[i];
    int ans=99999999999;
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=n; ++j)
        {
            if(i==j) continue;
            ans=min(ans,get(i)+get(j));
        }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值