POJ 2128:Highways,poj2128highways

來源:互聯網
上載者:User

POJ 2128:Highways,poj2128highways

Highways
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2730   Accepted: 1008   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 <= 109 ).

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

43 5 10

Sample Output

123 1 4 2

題意是有N個城市在一條高速公路上,這條高速公路是一條直線。但是這條高速公路是單向的,人們去的時候可以走高速公路,回來的時候又只能走country road了。

所以新上任的市長打算在回去的方向上建立兩條路(其實一條整個回去的路就足夠了,但是市長任性,就兩條),滿足路上的城市都能來回都走上高速公路,問修這兩條路的最短距離是多少。

要在回去的方向上修兩條能夠在所有城市都能互相來回,說明這兩條路必然重複了一段距離,那既然必然要重複一段距離的話,又要修這兩條路的距離最短,自然要覆蓋那個兩個城市之間最短距離的那個了。

這個題 題意比題都難理解。另外雖然是水題,只需要找這些線段中的最短線段,但我自己還是WA了幾次,原因是要考慮到第一條和最後一條線段是不能被選擇為重複的,原因題目中說了: Each highway would connect two different cities.

代碼:

#include <iostream>#include <algorithm>#include <cmath>#include <vector>#include <string>#include <cstring>#pragma warning(disable:4996)using namespace std;int dis[50002];int main(){int n,i,min,min_x;cin>>n;dis[0]=0;min=1000000000;cin>>dis[1];for(i=2;i<n-1;i++)//得考慮到不是第一條線段和最後一條線段的情況{cin>>dis[i];if(dis[i]-dis[i-1]<min){min=dis[i]-dis[i-1];min_x=i;}}cin>>dis[n-1];if(n>=4){cout<<min+dis[n-1]<<endl;cout<<min_x+1<<" "<<1<<" "<<n<<" "<<min_x<<endl;}else{cout<<0<<endl;}return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.