• <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>

            SRM 146 DIV 2 1000

            Problem Statement

                

            A well-known riddle goes like this: Four people are crossing an old bridge. The bridge cannot hold more than two people at once. It is dark, so they can't walk without a flashlight, and they only have one flashlight! Furthermore, the time needed to cross the bridge varies among the people in the group. For instance, let's say that the people take 1, 2, 5 and 10 minutes to cross the bridge. When people walk together, they always walk at the speed of the slowest person. It is impossible to toss the flashlight across the bridge, so one person always has to go back with the flashlight to the others. What is the minimum amount of time needed to get all the people across the bridge?

            In this instance, the answer is 17. Person number 1 and 2 cross the bridge together, spending 2 minutes. Then person 1 goes back with the flashlight, spending an additional one minute. Then person 3 and 4 cross the bridge together, spending 10 minutes. Person 2 goes back with the flashlight (2 min), and person 1 and 2 cross the bridge together (2 min). This yields a total of 2+1+10+2+2 = 17 minutes spent.

            You want to create a computer program to help you solve new instances of this problem. Given an vector <int> times , where the elements represent the time each person spends on a crossing, your program should return the minimum possible amount of time spent crossing the bridge.

            Definition

                
            Class: BridgeCrossing
            Method: minTime
            Parameters: vector <int>
            Returns: int
            Method signature: int minTime(vector <int> times)
            (be sure your method is public)
                很有意思的一個題目,大概是說有n個人(1<=n<=6)要過一個獨木橋,獨木橋每次最多只能過2個人。過橋必須要燈籠,而且只有一盞燈籠。如果2個人一起過橋,所花時間由那個需要最長時間的人決定。問怎么過橋所需要的時間最短。
                由于題目的數據量不是很大,我直接用dfs搜了下所有的情況然后取最小的時間。貌似還有數學方法能很輕松的解決,不過還沒想到。
            #include <iostream>
            #include 
            <vector>
            using namespace std;

            class BridgeCrossing{
            public:
                
            int len,time,cross[6],best;
                
            void dfs(int n,bool flash,vector<int> times);
                
            int minTime(vector<int> times);
            }
            ;
            void BridgeCrossing::dfs(int n, bool flash, vector<int> times){
                
            if(n==0){
                    best
            =(best>time)?time:best;
                    
            return;
                }

                
            if(flash){
                    
            for(int i=0;i<len;i++)
                        
            for(int j=i+1;j<len;j++)
                            
            if(cross[i] && cross[j]){
                                cross[i]
            =cross[j]=false;
                                time
            +=max(times[i],times[j]);
                                dfs(n
            -2,!flash,times);
                                time
            -=max(times[i],times[j]);
                                cross[i]
            =cross[j]=true;
                            }

                }

                
            else{
                    
            for(int i=0;i<len;i++)
                        
            if(!cross[i]){
                            cross[i]
            =true;
                            time
            +=times[i];
                            dfs(n
            +1,!flash,times);
                            time
            -=times[i];
                            cross[i]
            =false;
                        }

                }

            }

            int BridgeCrossing::minTime(vector<int> times){
                best
            =INT_MAX;
                len
            =times.size();
                time
            =0;
                
            for(int i=0;i<6;i++) cross[i]=true;
                
            if(len==1) best=times[0];
                
            else dfs(len,true,times);
                
            return best;
            }

            posted on 2009-05-27 00:59 極限定律 閱讀(655) 評論(1)  編輯 收藏 引用 所屬分類: TopCoder

            評論

            # re: SRM 146 DIV 2 1000 2010-03-05 11:05 3214668848

            在劉汝佳的《算法藝術與信息學競賽》上貪心算法練習題有這么一個題,應該是貪心可以吧  回復  更多評論   

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

            導航

            統計

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            精品久久人人做人人爽综合| 欧美激情精品久久久久| 91秦先生久久久久久久| 免费一级欧美大片久久网| 中文字幕无码免费久久| 香港aa三级久久三级| 无码精品久久一区二区三区| 久久天堂电影网| 久久福利资源国产精品999| 国内精品久久久久影院一蜜桃| 久久久久国产成人精品亚洲午夜| 99久久国语露脸精品国产| 人妻少妇久久中文字幕| 亚洲国产一成久久精品国产成人综合 | 91精品国产91久久| 99久久国产亚洲综合精品| 国产精品丝袜久久久久久不卡| 精品伊人久久大线蕉色首页| 中文精品99久久国产| 久久综合中文字幕| 久久精品国产亚洲AV电影| 人妻精品久久久久中文字幕一冢本| 狠狠久久综合伊人不卡| 国产韩国精品一区二区三区久久| 99久久国产亚洲综合精品| 亚洲精品99久久久久中文字幕| 久久99精品九九九久久婷婷| 国产精品久久久久久福利漫画| 97久久国产综合精品女不卡| 亚洲国产成人精品久久久国产成人一区二区三区综| 久久人人爽人人爽人人AV东京热| 久久国产午夜精品一区二区三区| 久久婷婷综合中文字幕| 精品乱码久久久久久久| 国内精品伊人久久久久AV影院| 欧美黑人激情性久久| 精品久久久无码人妻中文字幕豆芽 | 色婷婷综合久久久久中文字幕| 国产综合免费精品久久久| 国产精品成人99久久久久 | 久久国产精品77777|