• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            hdu3440(差分約束)

            題目網(wǎng)址: http://acm.hdu.edu.cn/showproblem.php?pid=3440 

            House Man

            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
            Total Submission(s): 446    Accepted Submission(s): 157


            Problem Description
            In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop to housetop. Today he plans to use N houses to hone his house hopping skills. He will start at the shortest house and make N-1 jumps, with each jump taking him to a taller house than the one he is jumping from. When finished, he will have been on every house exactly once, traversing them in increasing order of height, and ending up on the tallest house.
            The man can travel for at most a certain horizontal distance D in a single jump. To make this as much fun as possible, the crazy man want to maximize the distance between the positions of the shortest house and the tallest house.
            The crazy super man have an ability—move houses. So he is going to move the houses subject to the following constraints:
            1. All houses are to be moved along a one-dimensional path.
            2. Houses must be moved at integer locations along the path, with no two houses at the same location.
            3. Houses must be arranged so their moved ordering from left to right is the same as their ordering in the input. They must NOT be sorted by height, or reordered in any way. They must be kept in their stated order.
            4. The super man can only jump so far, so every house must be moved close enough to the next taller house. Specifically, they must be no further than D apart on the ground (the difference in their heights doesn't matter).
            Given N houses, in a specified order, each with a distinct integer height, help the super man figure out the maximum possible distance they can put between the shortest house and the tallest house, and be able to use the houses for training.
             

            Input
            In the first line there is an integer T, indicates the number of test cases.(T<=500)
            Each test case begins with a line containing two integers N (1 ≤ N ≤ 1000) and D (1 ≤ D ≤1000000). The next line contains N integer, giving the heights of the N houses, in the order that they should be moved. Within a test case, all heights will be unique.
             

            Output
            For each test case , output “Case %d: “first where d is the case number counted from one, then output a single integer representing the maximum distance between the shortest and tallest house, subject to the constraints above, or -1 if it is impossible to lay out the houses. Do not print any blank lines between answers.
             

            Sample Input
            3 4 4 20 30 10 40 5 6 20 34 54 10 15 4 2 10 20 16 13
             

            Sample Output
            Case 1: 3 Case 2: 3 Case 3: -1

            // Bellman_Ford 
            #include <stdio.h>
            #include 
            <memory>
            #include 
            <iostream>
            #include 
            <algorithm>
            #include 
            <cstring>
            #include 
            <vector>
            #include 
            <map>
            #include 
            <cmath>
            #include 
            <set>
            #include 
            <queue>
            #include 
            <time.h> 
            #include 
            <limits>
            using namespace std;
            #define typev int
            #define N 1005
            #define inf 0x7fffffff 
            #define E (N*5) 
            const double pi = acos(-1.0); 
            struct e{
                
            int st, ed; 
                typev len;  
                
            void set(int _st, int _ed, typev _len){
                    st 
            = _st; 
                    ed 
            = _ed; 
                    len 
            = _len; 
                }
            }es[E];
            struct node{
                
            int h; 
                
            int i; 
            }nodes[N];
            e
            * fir[N]; 
            int n, en, d;
            int st, ed; 
            int vis[N], t[N], que[N]; 
            typev dist[N]; 
            inline 
            bool cmp(node n1, node n2){
                
            return n1.h < n2.h; 
            }
            inline 
            bool relax(int st, int ed, int len){
                
            if(dist[st] < inf && dist[ed] > dist[st] + len){
                    dist[ed] 
            = dist[st] + len;
                    
            return true
                }
                
            return false
            }
            bool Bellman_Ford(int n, int st){  //返回true表示有負(fù)環(huán)
                int i, j; 
                
            bool flag; 
                
            for(i = 0; i < n; i++) dist[i] = inf; 
                dist[st] 
            = 0
                
            for(i = 0; i < n; i++){
                    flag 
            = false
                    
            for(j = 0; j < en; j++){
                        
            if(relax(es[j].st, es[j].ed, es[j].len)) flag = true
                    }
                    
            if(!flag) break
                }
                
            return flag;  
            }
            int cnt = 0
            int main(){
            #ifndef ONLINE_JUDGE
                freopen(
            "in.txt""r", stdin); 
                
            //freopen("out.txt", "w", stdout); 
            #endif 

                
            int t, i, ans, u, v;
                scanf(
            "%d"&t);
                
            while(t--){
                    scanf(
            "%d%d"&n, &d);
                    
            for(i = 0; i < n; i++){
                        scanf(
            "%d"&nodes[i].h);
                        nodes[i].i 
            = i; 
                    }
                    en 
            = st = ed = 0
                    sort(nodes, nodes
            +n, cmp); 
                    st 
            = nodes[0].i; 
                    ed 
            = nodes[n - 1].i; 
                    
            if(st > ed) swap(st, ed); 
                    
            for(i = 0; i < n; i++) fir[i] = NULL; 
                    
            for(i = 1; i < n; i++){
                        es[en
            ++].set(i, i-1-1); 
                        u 
            = nodes[i].i; 
                        v 
            = nodes[i - 1].i; 
                        
            if(u < v){
                            es[en
            ++].set(u, v, d); 
                        }
            else es[en++].set(v, u, d); 
                    } 
                    
            if(!Bellman_Ford(n, st)) ans = dist[ed];
                    
            else ans = -1
                    printf(
            "Case %d: %d\n"++cnt, ans);

                }
                
            return 0;
            }



            posted on 2011-01-23 01:13 tw 閱讀(497) 評(píng)論(0)  編輯 收藏 引用 所屬分類: HDU題解

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            文章分類

            文章檔案

            搜索

            最新評(píng)論

            久久妇女高潮几次MBA| 97久久超碰国产精品旧版| 亚洲国产婷婷香蕉久久久久久| 久久久久久亚洲精品不卡| 久久国产欧美日韩精品| 久久精品嫩草影院| 一本色道久久88综合日韩精品| 无码伊人66久久大杳蕉网站谷歌| 久久最新精品国产| 国产毛片欧美毛片久久久| 国产亚洲美女精品久久久| 亚洲国产精品无码久久久蜜芽| 亚洲国产精品久久| 人妻精品久久无码区| 日日狠狠久久偷偷色综合免费| 久久久久成人精品无码中文字幕 | 亚洲va国产va天堂va久久| 品成人欧美大片久久国产欧美...| 国内精品久久久久影院亚洲| 青青草国产成人久久91网| 亚洲级αV无码毛片久久精品| 精品久久人人妻人人做精品| 国产精品久久久久久| 三上悠亚久久精品| 久久人人添人人爽添人人片牛牛| 国内精品欧美久久精品| 久久青青草原精品影院| 国产午夜久久影院| 久久精品嫩草影院| 色成年激情久久综合| 国产精品久久久久无码av| 精品国产一区二区三区久久久狼| 亚洲午夜久久久久久噜噜噜| 狠狠色婷婷久久一区二区| 色狠狠久久综合网| 日韩人妻无码一区二区三区久久99 | 国产亚洲精品久久久久秋霞| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 无码任你躁久久久久久老妇App| 开心久久婷婷综合中文字幕| 久久亚洲电影|