POJ 2128 Highways(我的水题之路——最小两条公路之和)

Highways
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1712   Accepted: 642   Special Judge

Description

In a distant country Lineland there are N cities and they are all located along the highway. The highway is a straight line; it starts from the first city and runs through the second, third city and so on, ending in the N-th city. The i-th city is located at the distance of X i miles from the first one. 
The highway is wide and smooth, so it is a pleasure for all people to drive along it. But there is one problem --- all roads in Lineland, including the highway, are one-way. So people are only allowed to drive along the highway from the city with smaller number to the city with greater number and they have to use country roads to get back, and that is not such a great pleasure indeed. 
After the new president Mr. Pathwayson was elected in Lineland, he has decided that he would like to make it easier for people to get from one town to another. But he does not dare to change the traditions, and make the highway two-way. Therefore he has decided to build new highways to connect the cities, so that it would be possible to get from any city to any other one by highways. Traditionally, the new highways must be one-way. 
Of course, Mr. Pathwayson is a great president, and he wants people to remember him in years. After a thought he has decided that building just one highway would not be enough for that. Therefore he has decided that he must build two new highways. Each highway would connect two different cities. Since people are anxious about their health, and cars running along the highway produce dangerous wastes, each new highway must not pass through any cities, except the cities it connects. Also building two new highways in one city would disturb people too much, so all the cities that would be the ends of the new highways must be different. 
You are the assistant of the minister of transportation of Lineland, so you are asked to choose the cities to be connected by the new highways. Since the cost of building a highway is proportional to its length, the total length of the highways must be minimal possible. Write a program to solve this problem. You may assume that the distance between two cities along the new highway is equal to the distance between those cities along the main highway.

Input

The first line of the input contains N (2 <= N <= 50 000). 
Next line contains N - 1 integer numbers: X2 , X3 , . . . , XN (1 <= X2 < X3 < . . . < XN <= 10 9 ).

Output

If it is impossible to build the highways satisfying all requirements, print number 0 on the first line of the output. 
In the other case on the first line of the output file print the minimal possible total length of the highways to be built. On the second line print S1 , E1 , S2 and E2 --- the numbers of the cities to connect by the first and the second highway, respectively. Note that highways are one-way and must run from S1 to E1 and from S2 to E2 .

Sample Input

4
3 5 10

Sample Output

12
3 1 4 2

Source

Northeastern Europe 2003, Northern Subregion

一个省,有N个城市,这几个城市只有N-1条单向的公路贯穿所有城市,且他们仅能够从1城市到N城市的方向通行,现在要修两条逆向公路,使得所有的城市可以相互连通。
如下图(蓝色路径):

即,如下图即可完成所有城市相互连通的功能,为了使得长度最小,其实长度为路线总长度加上重叠路径。所以,只要保证重叠城市的路径长度最小即可。

注意点:
1)如果城市数量不到4个,则输出"0\n",其他情况均可以按照此理论求的(1WA)

代码(1AC 1WA):
#include <cstdio>
#include <cstdlib>
#include <cstring>

int N;
int length[51000];

int main(void){
    long long min, tmp, sum;
    int i, index;

    while (scanf("%d", &N) != EOF){
        memset(length, 0, sizeof(length));
        for (i = 1, sum = 0; i < N ; i++){
            scanf("%d", &length[i]);
        }
        sum = length[i - 1];
        if (N >= 4){
            min = 1000010000;
            for (index = 2, i = 2; i < N - 1; i++){
                tmp = length[i] - length[i - 1];
                if (tmp < min){
                    min = tmp;
                    index = i;
                }
            }
            printf("%lld\n", sum + min);
            printf("%d %d %d %d\n", index + 1, 1, N, index);
        }
        else{
            printf("0\n");
        }
    }
    return 0;
}



你可能感兴趣的:(Integer,input,Build,output,distance,Numbers)