POJ 2128:Highways

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

4
3 5 10

Sample Output

12
3 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;
}
时间: 2024-08-18 18:59:45

POJ 2128:Highways的相关文章

POJ 2485:Highways(最小生成树&amp;&amp;prim)

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21628   Accepted: 9970 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Fla

【POJ 2485】 Highways

[POJ 2485] Highways 最小生成树模板 Prim #include using namespace std; int mp[501][501]; int dis[501]; bool vis[501]; int n; int Prim() { int i,j,w,p,mm = 0; memset(dis,-1,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[1] = 0; for(i = 0; i < n; ++i) { w = INF;

POJ 1963:All in All

All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27707   Accepted: 11381 Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever

POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1659:Frogs&#39; Neighborhood(Havel-Hakimi定理)

Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6898   Accepted: 3006   Special Judge Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只青蛙的邻居数目x1, x2, ..

POJ 1422:Air Raid(最大独立集)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6547   Accepted: 3896 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

POJ 2965:The Pilots Brothers&#39; refrigerator

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18080   Accepted: 6855   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

[2016-01-27][POJ][1751][B - Highways]

[2016-01-27][POJ][1751][B - Highways] B - Highways Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1751 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a

POJ 1904:King&#39;s Quest【tarjan】

题目大意:给出一个二分图的完美匹配(王子和公主的烧死名单表),二分图x部和y部均只有n个点,问对于每一个x部的点,他能选择哪些点与之匹配 使得与之匹配后,剩余图的最大匹配仍然是n 思路:这题是大白书379页二分图的压轴题,在图论刷的题还不多时思考过这题,现在想来也不难想 这题引人瞩目的一点便是预先给出了一个二分图的初始匹配 对每个点枚举后增广显然不怎么可行,那么还是图论问题的经典思考方式,点和边各表示什么 题目的输入天然的给出了一个图,但对这题好像没什么用处,于是开始思考把给出的初始匹配的每条边