[Zhengqing] [1799] Wrz's New Year Money

Problem Description

wrz的压岁钱
Time limit: 1 SecMemory limit: 128 MB

Description

The New Year is coming soon, and wrz from Electronic Information is going to his relatives’ homes to collect New Year’s money.
He has three relatives. He has a Guge-brand electric scooter whose battery can only last from his own home to any one relative’s home; then the scooter runs out of power and is left at that relative’s home to charge.
wrz from Electronic Information walks to the remaining two relatives, then comes back and rides the fully charged Guge-brand scooter home.
But wrz from Electronic Information is lazy and does not like walking too much. Can you help him calculate the minimum distance he has to walk?

Input

The first line is the number T, indicating there are T (1<=T<=1000) test cases.
Each test case occupies one line and contains three integers x, y, and z, representing the distances between any two of the three relatives’ homes. (1<=x, y, z<=10~9)
For example, if the three relatives are a, b, and c, then the three numbers are the distance from a to b, from b to c, and from c to a.

Output

Each instance output occupies one line: the format is Case #x: y
x is the x-th instance, and y is the shortest distance wrz from Electronic Information needs to walk.

Sample Input

1
2
3
4
3
1 2 3
4 5 6
7 8 9

Sample Output

1
2
3
Case #1: 6
Case #2:15
Case #3: 24

Problem Analysis

At first glance, the idea was to draw a triangle.
So it felt very simple.
But something still felt strange.
Then I suddenly realized
the catch is that these three values do not necessarily form a triangle.
When they do not form a triangle,
going back along the original route without visiting the third
uses less distance.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
#include<algorithm>
using namespace std;
int main() {
    int T;
    int k,i;
    long long a[3];
    long long sum;
    scanf("%d",&T);
    for(k=1;k<=T;k++) {
        for(i=0; i<3; i++)
            scanf("%lld",&a[i]);
        sort(a,a+3);
        sum=a[0]+a[1];
        if(a[2]<sum)
            sum+=a[2];
        else
            sum+=sum;
        printf("Case #%d: %lld\n",k,sum);
    }
    return 0;
}

Problem link: [ZZULI] [1799] wrz’s lucky money

Loading...