[UVA][dp] 10874 - Segments@Morris' Blog|PChome Online 人新台
2013-05-22 10:45:21| 人817| 回0 | 上一篇 | 下一篇

[UVA][dp] 10874 - Segments

0 收藏 0 0 站台

Problem E
Segments
Input: Standard Input

Output: Standard Output

 

You are tofind the length of the shortest path from the top to the bottom of a gridcovering specified points along the way.

 

More precisely,you are given an n by n grid, rows 1...n and columns 1…n (1 ≤ n ≤20000). On each row i, two points L(i) and R(i) are given where 1 ≤ L(i)≤ R(i) ≤ n. You are to find the shortest distance from position (1,1), to (n, n) that visits all of the given segments in order. In particular,for each row i, all the points

(i,L(i)); (i, L(i) + 1), (i, L(i) + 2), …, (i, R(i)),

must bevisited. Notice that one step is taken when dropping down between consecutiverows. Note that you can only move left, right and down (you cannot move up alevel). On finishing the segment on row n, you are to go to position (n, n), ifnot already there. The total distance covered is then reported. 

 

Input

Input file contains maximum 10 sets of inputs.The description of each set is given below:

 

The first line of each set consists of aninteger n, the number of rows/columns on the grid. On each of the next n lines,there are two integers L(i) followed by R(i) (where 1 ≤ L(i) ≤ R(i)≤ n).

 

Input is terminated by a set whose value of n is zero. Thisinput should not be processed.

 

Output

For each setof input output is one integer, which is the length of the (shortest) path from(1, 1) to (n, n) which covers all intervals L(i), R(i).

 

SampleInput                              Output for Sample Input

6
2 6
3 4
1 3
1 2
3 6
4 5
0

24


Problem setter: Ian Munro

Special Thanks: Troy Vasiga

 

/* Sample input description on next page */

 

 

 

 

 

 

 

 

 

Explanation of Sample Input/Output

Below is a pictoral representation of the input.

Notice that on the first row, we must traverse 5 unitsto the right and then drop down one level.

On the second row, we must traverse 3 units to theleft and drop down one level.

On the third row, we must traverse 2 units to the leftand drop down one level.

On the fourth row, we move 1 unit to the right andthen drop down one level.

On the fifth row, we move 4 units to the right anddrop down one level.

On the sixth (and final) row, we move 2 units left,then 2 units right.

In total, we have moved 6 + 4 + 3 + 2 + 5 + 4 = 24units.


目意思:
(1,1) 出,只能左右或者是往下移一格,目到 (n,n),而且每段都要被走(整都要覆在走的路中),求最少路。

解法:

,於每保留停留在[左端][右端]的最小值,
然後行移,
[左端]到[下左端]+段 到 [下一次的右端]

[左端]到[下右端]+段 到 [下一次的左端]


[右端]到[下左端]+段 到 [下一次的右端]

[右端]到[下右端]+段 到 [下一次的左端]

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int main() {
    int n;
    while(scanf("%d", &n) == 1 && n) {
        int L = 1, R = 1, Li, Ri;
        int dpl = 0, dpr = 0, tdpl, tdpr;
        int i;
        for(i = 0; i < n; i++) {
            scanf("%d %d", &Li, &Ri);
            tdpl = min(dpl+abs(L-Li), dpr+abs(R-Li))+1;
            tdpr = min(dpr+abs(R-Ri), dpl+abs(L-Ri))+1;
            dpr = tdpl + Ri-Li;
            dpl = tdpr + Ri-Li;
            L = Li, R = Ri;
        }
        printf("%d\n", min(dpl+n-Li, dpr+n-Ri)-1);
  &nbs; }
    return 0;
}

[]

台: Morris
人(817) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][二分答案] 12124 - Assemble
此分上一篇:[UVA][spfa] 11338 - Minefield
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86