• <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>
            隨筆 - 97, 文章 - 22, 評論 - 81, 引用 - 0
            數據加載中……

            Pku 1383 Labyrinth && Zju 3172 Extend 7-day Vacation (最長路)

            求任意圖的最長路。

            其實思路很簡單,兩次bfs即可。
            首先在圖中任意選擇一個點,對它進行bfs擴展,找到距該點最遠的點,然后再一次bfs尋找最遠點,此時找到的最遠距離就是最長路的大小。

            Pku 1383 Labyrinth 代碼:

            #include <iostream>
            #include 
            <queue>
            using namespace std;

            int dir[4][2= {{01}{0-1}{10}{-10}};

            struct point {
                
            int x;
                
            int y;
                
            int step;
            }
            temp, tt;

            int n, m;
            point q[
            1500000];
            int hash[1002][1002];
            char map[1002][1002];
            int rear, front;

            void bfs(point temp) {
                
            int i;

                memset(hash, 
            0sizeof(hash));
                rear 
            = front = 0;

                hash[ temp.x ][ temp.y ] 
            = 0;
                q[ rear 
            ++ ] = temp;
                
            while(front < rear) {
                    temp 
            = q[ front++ ];

                    
            for(i = 0; i < 4; i++{
                        tt.x 
            = temp.x + dir[i][0];
                        tt.y 
            = temp.y + dir[i][1];
                        tt.step 
            = temp.step + 1;
                        
            if(tt.x < 0 || tt.y < 0 || tt.x >= n || tt.y >= m)
                            
            continue;
                        
            if(map[tt.x][tt.y] == '.' && !hash[ tt.x ][ tt.y ]) {
                            hash[ tt.x ][ tt.y ] 
            = tt.step;
                            q[ rear 
            ++ ] = tt;
                        }

                    }

                }

            }


            int main() {

                
            int t;
                
            int i, j;
                scanf(
            "%d"&t);

                
            while(t--{
                    scanf(
            "%d %d"&m, &n);

                    
            for(i = 0; i < n; i++{
                        scanf(
            "%s", map[i]);
                    }


                    
            int flag = 0;
                    
            for(i = 0; i < n; i++{
                        
            for(j = 0; j < m; j++{
                            
            if(map[i][j] == '.'{
                                temp.x 
            = i;
                                temp.y 
            = j;
                                temp.step 
            = 0;
                                flag 
            = 1;
                                
            break;
                            }

                        }

                        
            if(flag)
                            
            break;
                    }


                    bfs(temp);
                    temp.step 
            = 0;

                    
            for(i = 0; i < n; i++{
                        
            for(j = 0; j < m; j++{
                            
            if(hash[i][j] > temp.step) {
                                temp.step 
            = hash[i][j];
                                temp.x 
            = i;
                                temp.y 
            = j;
                            }

                        }

                    }



                    temp.step 
            = 0;
                    bfs(temp);
                    temp.step 
            = 0;
                    
            for(i = 0; i < n; i++{
                        
            for(j = 0; j < m; j++{
                            
            if(hash[i][j] > temp.step) {
                                temp.step 
            = hash[i][j];
                                temp.x 
            = i;
                                temp.y 
            = j;
                            }

                        }

                    }

                    printf(
            "Maximum rope length is %d.\n", temp.step);


                }

            }


            Zju 3172 Extend 7-day Vacation 代碼:
            #include <iostream>
            #include 
            <vector>
            #include 
            <queue>
            using namespace std;

            struct point {
                
            int now;
                
            int step;
            }
            temp, tt;

            vector 
            < int > vec[1001];
            int hash[1001];
            int n, m, x, y;
            int i, j;
            bool exsit[1001];
            queue 
            < point > q;

            void bfs(point temp) {

                
            int i;
                
            while(!q.empty())
                    q.pop();
                q.push( temp );
                hash[ temp.now ] 
            = 0;
                memset(hash, 
            -1sizeof(hash));

                
            while(!q.empty()) {

                    temp 
            = q.front();
                    q.pop();
                    
            int size = vec[ temp.now ].size();

                    
            for(i = 0; i < size; i++{
                        
            if(hash[ vec[temp.now][i] ] == -1{
                            
                            tt.now 
            = vec[temp.now][i];
                            tt.step 
            = temp.step + 1;

                            hash[ vec[temp.now][i] ] 
            = tt.step;
                            q.push( tt );
                        }

                    }

                }

            }

            int main() {
                
            while(scanf("%d %d"&n, &m) != EOF) {

                    memset(exsit, 
            0sizeof(exsit));

                    
            for(i = 0; i < n; i++)
                        vec[i].clear();
                    
                    
            while(m --{
                        scanf(
            "%d %d"&x, &y);
                        vec[x].push_back( y );
                        vec[y].push_back( x );
                        exsit[x] 
            = exsit[y] = 1;
                    }


                    
            for(i = 0; i < n; i++{
                        
            if(exsit[i] == 1)
                            
            break;
                    }

                    temp.now 
            = i;
                    temp.step 
            = 0;
                    bfs(temp);
                    
            for(i = 0; i < n; i++{
                        
            if(hash[i] > temp.step) {
                            temp.step 
            = hash[i];
                            temp.now 
            = i;
                        }

                    }

                    temp.step 
            = 0;

                    bfs(temp);
                

                    
            for(i = 0; i < n; i++{
                        
            if(hash[i] > temp.step) {
                            temp.step 
            = hash[i];
                            temp.now 
            = i;
                        }

                    }

                    
            if(temp.step < 7)
                        printf(
            "Impossible\n");
                    
            else
                        printf(
            "%d\n", temp.step + 1);
                }

                
            return 0;
            }

            posted on 2009-03-10 23:48 英雄哪里出來 閱讀(468) 評論(0)  編輯 收藏 引用 所屬分類: ACM

            精品久久久久香蕉网| 久久天堂AV综合合色蜜桃网| 大美女久久久久久j久久| 91久久九九无码成人网站 | 777午夜精品久久av蜜臀| 日韩人妻无码精品久久久不卡| 精品综合久久久久久97超人 | 国产精品丝袜久久久久久不卡| 热综合一本伊人久久精品| 欧美一区二区三区久久综合| 青青青国产精品国产精品久久久久| 精品久久久久久99人妻| 无码国内精品久久人妻蜜桃| 91久久精品视频| 久久久久久无码Av成人影院| 久久久久久亚洲精品不卡| 久久亚洲精品人成综合网| 色综合久久88色综合天天 | 国产日产久久高清欧美一区| 久久天天婷婷五月俺也去| 精品久久久无码中文字幕| 久久久久99精品成人片试看| 亚洲欧美一区二区三区久久| 伊人久久免费视频| 99re久久精品国产首页2020| 99久久国产精品免费一区二区| 色婷婷久久久SWAG精品| 国产精品久久久久久久午夜片| 久久99精品久久久久久久不卡| 久久精品一本到99热免费| 亚洲精品乱码久久久久久不卡| 精品久久人人妻人人做精品| 丁香久久婷婷国产午夜视频| 国产精品久久久久久吹潮| 婷婷综合久久中文字幕蜜桃三电影 | 无码AV波多野结衣久久| 麻豆精品久久久久久久99蜜桃| 欧美一级久久久久久久大| 久久综合伊人77777麻豆| 久久久亚洲精品蜜桃臀| 日韩欧美亚洲综合久久影院Ds|