Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) A. Trip For Meal

来源:互联网 发布:淘宝ipad版 编辑:程序博客网 时间:2024/04/29 17:48

A. Trip For Meal

Problem Statement

    Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. Winnie has got three best friends: Rabbit, Owl and Eeyore, each of them lives in his own house. There are winding paths between each pair of houses. The length of a path between Rabbit’s and Owl’s houses is a meters, between Rabbit’s and Eeyore’s house is b meters, between Owl’s and Eeyore’s house is c meters.
    For enjoying his life and singing merry songs Winnie-the-Pooh should have a meal n times a day. Now he is in the Rabbit’s house and has a meal for the first time. Each time when in the friend’s house where Winnie is now the supply of honey is about to end, Winnie leaves that house. If Winnie has not had a meal the required amount of times, he comes out from the house and goes to someone else of his two friends. For this he chooses one of two adjacent paths, arrives to the house on the other end and visits his friend. You may assume that when Winnie is eating in one of his friend’s house, the supply of honey in other friend’s houses recover (most probably, they go to the supply store).
    Winnie-the-Pooh does not like physical activity. He wants to have a meal n times, traveling minimum possible distance. Help him to find this distance.

Input

    First line contains an integer n (1 ≤ n ≤ 100) — number of visits.
    Second line contains an integer a (1 ≤ a ≤ 100) — distance between Rabbit’s and Owl’s houses.
    Third line contains an integer b (1 ≤ b ≤ 100) — distance between Rabbit’s and Eeyore’s houses.
    Fourth line contains an integer c (1 ≤ c ≤ 100) — distance between Owl’s and Eeyore’s houses.

Output

    Output one number — minimum distance in meters Winnie must go through to have a meal n times.

Examples

Example 1
    Input
        3
        2
        3
        1
    Output
        3
Example 2
    Input
        1
        2
        3
        5
    Output
        0

Note

    In the first test case the optimal path for Winnie is the following: first have a meal in Rabbit’s house, then in Owl’s house, then in Eeyore’s house. Thus he will pass the distance 2 + 1 = 3.
    In the second test case Winnie has a meal in Rabbit’s house and that is for him. So he doesn’t have to walk anywhere at all.

题意

    有一个人想要访问3个人的家一共n次,现在他在1号房子,进行了第一次访问。在3幢房子中间每两幢房子都有一条路,第二行表示1号房子和2号房子之间的路的长度,第三行表示1号房子和3号房子之间的路的长度,第四行表示2号房子和三号房子之间的路,问你他要想再进行n-1次访问,且相邻两次不能访问同一户人家(就是不能不走路),他需要走的最短长度是多少。

思路

    首先我们可以知道,要是n=1的话那么输出0就行了。否则找到三条路里面最短的一条路,如果这一条路与1号房子相连,那么接下来n-1次都走这条路就是最短的了,否则走一条min(a,b)到达最短的路c,之后c次都走那条就行了。

Code

#pragma GCC optimize(3)#include<bits/stdc++.h>using namespace std;typedef long long ll;inline void readInt(int &x) {    x=0;int f=1;char ch=getchar();    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();    x*=f;}inline void readLong(ll &x) {    x=0;int f=1;char ch=getchar();    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();    x*=f;}/*================Header Template==============*/ll n,a,b,c;int main() {    readLong(n);    readLong(a);    readLong(b);    readLong(c);    if(n==1) {        puts("0");        return 0;    }    ll len=min(a,min(b,c)),ans;    if(len==a||len==b)        ans=len*(n-1);    else        ans=min(a,b)+(n-2)*len;    printf("%lld\n",ans);    return 0;}
阅读全文
0 0
原创粉丝点击