• <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)
                很有意思的一個(gè)題目,大概是說有n個(gè)人(1<=n<=6)要過一個(gè)獨(dú)木橋,獨(dú)木橋每次最多只能過2個(gè)人。過橋必須要燈籠,而且只有一盞燈籠。如果2個(gè)人一起過橋,所花時(shí)間由那個(gè)需要最長時(shí)間的人決定。問怎么過橋所需要的時(shí)間最短。
                由于題目的數(shù)據(jù)量不是很大,我直接用dfs搜了下所有的情況然后取最小的時(shí)間。貌似還有數(shù)學(xué)方法能很輕松的解決,不過還沒想到。
            #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 極限定律 閱讀(659) 評(píng)論(1)  編輯 收藏 引用 所屬分類: TopCoder

            評(píng)論

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

            在劉汝佳的《算法藝術(shù)與信息學(xué)競賽》上貪心算法練習(xí)題有這么一個(gè)題,應(yīng)該是貪心可以吧  回復(fù)  更多評(píng)論   


            只有注冊用戶登錄后才能發(fā)表評(píng)論。
            相關(guān)文章:
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久久久人妻一区精品| 久久精品www| 久久精品国产2020| 亚洲va国产va天堂va久久| 99久久99这里只有免费费精品| 国内精品人妻无码久久久影院导航| 久久午夜无码鲁丝片| 久久精品国产91久久麻豆自制| 久久99精品久久久久久9蜜桃 | 久久不见久久见免费视频7| 国产精品美女久久久m| 久久人人超碰精品CAOPOREN| 亚洲精品无码久久久久久| 久久99热国产这有精品| 亚洲国产精品嫩草影院久久| 久久精品国产亚洲77777| 久久综合伊人77777麻豆| 久久人人爽人人爽人人AV| 久久国产精品偷99| 久久精品国产99国产精偷 | 国产一级持黄大片99久久| 欧美性猛交xxxx免费看久久久| 人妻无码αv中文字幕久久| 亚洲国产成人久久综合区| 国产精品久久久天天影视香蕉| 久久精品人人做人人妻人人玩| 无码人妻少妇久久中文字幕| 成人精品一区二区久久| 国产精品久久久福利| 久久久亚洲欧洲日产国码aⅴ | 久久只有这里有精品4| 精品综合久久久久久97超人| 亚洲AV无码1区2区久久| 久久天天躁狠狠躁夜夜不卡| 亚洲精品综合久久| 日韩中文久久| 久久人人爽人人爽人人片av麻烦 | 无码国内精品久久人妻麻豆按摩| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 国产精品永久久久久久久久久| 久久久久久综合一区中文字幕|