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

            USACO chapter 3 section 2 Sweet Butter

            做了一題,開心,冒一下泡:
            題目大意:有一些草原(1,……,p個(gè)),告訴你n個(gè)奶牛的在哪個(gè)草原,F(xiàn)amer John 會(huì)在其中一個(gè)草原放一塊糖,
            讓所有的奶牛去舔(提高奶的質(zhì)量^-^),要求的就是把糖放在哪個(gè)草原使所有的奶牛到那的路程和最短。
            (當(dāng)然奶牛都是揀最短的路走)。
             
            第一種超時(shí)思路 :Floyd算法求出任意兩個(gè)草原間的距離,枚舉所有草原,取所有奶牛所在的草原到該草原和的最大值。
                           很不幸TLE了,O(800^3);
            第二種超時(shí)思路:用Dijkstra+priority_queue(優(yōu)先級(jí)隊(duì)列),枚舉所有草原算單源最短路徑,如果不優(yōu)化還是O(800^3)
            用priority_queue還是TLE了,Dijkstra找最近的的時(shí)候用優(yōu)先級(jí)隊(duì)列很快,O(1),調(diào)整O(log n),可能是用的不夠好。
            貌似有人用這種方法做出來。不知道用鄰接表行不行。
             
            第三種思路:就是SPFA算法(Shortest Path Fast Algorithm):
            用鄰接表,枚舉所有草原,用SPFA算單源最短路徑,SPFA算法要用到隊(duì)列,一個(gè)標(biāo)記數(shù)組用于判斷是否在隊(duì)列中,
            d[]數(shù)組,存放i到所有點(diǎn)的距離,初始值都為MAX,d[i]=0。
            枚舉的草原i先入隊(duì)。如果隊(duì)列非空就開始迭代,取隊(duì)首元素x并彈出它,對(duì)于所有與x相鄰的(鄰接表的好處體現(xiàn)了)u,
            如果d[u]>d[x]+len(x,u)(x到u的距離)d[u]=d[x]+len(x,u),這樣d[u]進(jìn)行更新了,d[u]可能會(huì)繼續(xù)更新別的點(diǎn),
            如果u不在隊(duì)列的話,就把u放在隊(duì)列中。一直迭代下去,直到隊(duì)列為空(就是說沒有在更新了);
             

            1# TLE
            USER: tian tianbing [tbbd4261]
            TASK: butter
            LANG: C++
            Compiling...
            Compile: OK
            Executing...
            Test 1: TEST OK [0.000 secs, 5544 KB]
            Test 2: TEST OK [0.000 secs, 5544 KB]
            Test 3: TEST OK [0.011 secs, 5544 KB]
            Test 4: TEST OK [0.022 secs, 5544 KB]
            Test 5: TEST OK [0.022 secs, 5544 KB]
            Test 6: TEST OK [0.054 secs, 5544 KB]
            Test 7: TEST OK [0.410 secs, 5544 KB]
            
              > Run 8: Execution error: Your program (`butter') used more than the
                        allotted runtime of 1 seconds (it ended or was stopped at 1.339
                        seconds) when presented with test case 8. It used 5544 KB of
                        memory.
                        

            想用Foloyd()求最短路徑,超時(shí)了。
            /*
            ID:tbbd4261
            PROG:butter
            LANG:C++
            */

            #include
            <fstream>
            using namespace std;
            const int MAX=805;
            const int INF=0x0fffffff;
            int adj[MAX][MAX]={0};
            int location[505];
            int n,p,c;
            ifstream fin(
            "butter.in");
            ofstream fout(
            "butter.out");
            void init()
            {
                 
            int i,j;
                 
            for(i=1; i<=p; i++)
                 
            for(j=1; j<=p; j++)
                     adj[i][j]
            =INF;
                 
            for(i=1; i<=p; i++)
                          adj[i][i]
            =0;
            }

            void Floyd()
            {
                 
            int i,j,k;
                 
            for(k=1; k<=p; k++)
                  
            for(i=1; i<=p; i++)
                   
            for(j=1; j<=p; j++)
                   {
                    
            if(i==j)continue;
                    
            if(adj[i][j]>adj[i][k]+adj[k][j])
                              adj[i][j]
            =adj[i][k]+adj[k][j];     
                   }
            }

            void print()
            {
                  
            int i,j;
                 
            for(i=1; i<=p; i++,fout<<endl)
                 
            for(j=1; j<=p; j++)
                          fout
            <<adj[i][j]<<' ';
            }

            int main()
            {
                
                fin
            >>n>>p>>c;
                init();
              
                
            for(int i=1; i<=n; i++ )
                     fin
            >>location[i];
                
                
            for(int i=1,len,s,t; i<=c; i++)
                {
                        fin
            >>s>>t>>len;
                        adj[t][s]
            =adj[s][t]=len;
                }
                Floyd();
                
            int minSum=INF,sum;
                
            for(int i=1; i<=p; i++)
                {
                        
            bool f=true;
                        sum
            =0;
                        
            for(int j=1; j<=n; j++)
                        {
                                
            if(adj[i][location[j]]!=INF)
                                {
                                   sum
            +=adj[i][location[j]];
                                }
                                
            else f=false
                        }
                        
            if(f==true&&sum<minSum)minSum=sum;
                                          
                }
                
                fout
            <<minSum<<endl;
                
                
            return 0;
            }
            2,TLE
            USER: tian tianbing [tbbd4261]
            TASK: butter
            LANG: C++
            Compiling...
            Compile: OK
            Executing...
            Test 1: TEST OK [0.000 secs, 5552 KB]
            Test 2: TEST OK [0.022 secs, 5552 KB]
            Test 3: TEST OK [0.011 secs, 5552 KB]
            Test 4: TEST OK [0.011 secs, 5552 KB]
            Test 5: TEST OK [0.032 secs, 5552 KB]
            Test 6: TEST OK [0.097 secs, 5552 KB]
            Test 7: TEST OK [0.637 secs, 5552 KB]
            
              > Run 8: Execution error: Your program (`butter') used more than the
                        allotted runtime of 1 seconds (it ended or was stopped at 1.674
                        seconds) when presented with test case 8. It used 5552 KB of
                        memory.
                        
             改用Dijkstra+priority_queue(STL)更慢了…………

             

            /*
            ID:tbbd4261
            PROG:butter
            LANG:C++
            */

            #include
            <fstream>
            #include
            <iostream>
            #include
            <queue>
            #include
            <functional>
            #include
            <cstring>
            using namespace std;
            const int MAX=805;
            const int INF=0x0fffffff;
            int n,p,c;
            int location[505]={0};
            int adj[MAX][MAX]={0};
            ifstream fin(
            "butter.in");
            ofstream fout(
            "butter.out");

            typedef pair
            <int ,int > type;

            int d[MAX];
            bool done[MAX];
            int dijkstra(int t)
            {
                memset(done,
            0,sizeof done);
                
            for(int i=1; i<=p; i++)
                        d[i]
            =INF;
                d[t]
            =0;
                priority_queue
            <int ,vector<type > ,greater<type> >q;
                q.push(make_pair(d[t],t));
                type temp;
                
            while(!q.empty())
                {
                        temp
            =q.top();  q.pop();
                        
            int x=temp.second;
                        
            if(done[x])continue;
                        done[x]
            =true;
                        
            //fout<<x<<' '<<d[x]<<endl;
                        for(int j=1; j<=p; j++)
                        {
                                
            if(d[j]>d[x]+adj[x][j])
                                      {
                                         d[j]
            =d[x]+adj[x][j];
                                         q.push(make_pair(d[j],j));
                                      }
                        }
                }
                
            int ans=0;
                
            for(int i=1; i<=n; i++)
                        
            if(d[location[i]]==INF)return -1;
                        
            else ans+=d[location[i]];  
                
            return  ans;
            }

            void init()
            {
                 
            int i,j;
                 
            for(i=1; i<=p; i++)
                 
            for(j=1; j<=p; j++)
                      adj[i][j]
            =(i==j?0:INF);    
            }

            void print()
            {
                 fout
            <<"print location: "<<endl;
                 
            for(int i=1; i<=n; i++)
                         fout
            <<location[i]<<' ';
                 fout
            <<endl;
                 fout
            <<"print adj: "<<endl;
                 
            for(int i=1; i<=p; i++,fout<<endl)
                 
            for(int j=1; j<=p; j++)
                         fout
            <<adj[i][j]<<' ';
                 fout
            <<endl;
            }
            int main()
            {
                fin
            >>n>>p>>c;
                init();
                
            for(int i=1; i<=n; i++)
                        fin
            >>location[i];
                        
                
            for(int i=1,s,t,len; i<=c; i++)
                {
                        fin
            >>s>>t>>len;
                        adj[s][t]
            =adj[t][s]=len;
                }
                
            //print();
                int min=INF;
                
            for(int i=1,tt; i<=p; i++)
                {
                         tt
            = dijkstra(i);
                         
            //system("pause");
                         if(tt!=-1&&tt<min)
                                   min
            =tt;
                }
                fout
            <<min<<endl;
                
            //system("pause");
                return 0;
            }
            USER: tian tianbing [tbbd4261]
            TASK: butter
            LANG: C++
            Compiling...
            Compile: OK
            Executing...
            Test 1: TEST OK [0.011 secs, 8084 KB]
            Test 2: TEST OK [0.000 secs, 8084 KB]
            Test 3: TEST OK [0.011 secs, 8084 KB]
            Test 4: TEST OK [0.022 secs, 8084 KB]
            Test 5: TEST OK [0.000 secs, 8084 KB]
            Test 6: TEST OK [0.011 secs, 8084 KB]
            Test 7: TEST OK [0.054 secs, 8084 KB]
            Test 8: TEST OK [0.108 secs, 8084 KB]
            Test 9: TEST OK [0.162 secs, 8084 KB]
            Test 10: TEST OK [0.173 secs, 8084 KB]
            All tests OK.
            

            Your program ('butter') produced all correct answers! This is your submission #5 for this problem. Congratulations!


            3,A了!
            千呼萬喚始出來,終于A了,而且速度挺快!!!
            傳說中的SPFA算法:
             
            /*
            ID:tbbd4261
            PROG:butter
            LANG:C++
            */

            #include
            <fstream>
            #include
            <cstring>
            #include
            <queue>
            using namespace std;
            const int MAX=805;
            const int INF=0x0fffffff;
            ifstream fin(
            "butter.in");
            ofstream fout(
            "butter.out");
            struct node{
                   
            int end,len;
            };
            int cnt[MAX]={0},location[505]={0},n,p,c;
            node adj[MAX][MAX];
            bool in[MAX]={0};
            int d[MAX];
            int SPFA(int i)
            {
                memset(
            in,0,sizeof in);
                
            for(int k=1; k<=p; k++)d[k]=INF;
                queue
            <int> Q;
                Q.push(i); 
                
            in[i]=true;
                d[i]
            =0;
                
            while(!Q.empty())
                {
                      
            int x=Q.front(); Q.pop();
                      
            in[x]=false;
                      
            for(int j=0; j<cnt[x]; j++)
                              
            if(d[x]+adj[x][j].len < d[ adj[x][j].end ])
                              {
                               d[adj[x][j].end]
            =d[x]+adj[x][j].len;   
                               
            if(!in[adj[x][j].end])
                                                    { 
                                                    Q.push(adj[x][j].end);
                                                    
            in[adj[x][j].end ]=true
                                                    }                      
                              }
                }
                
                
            int ans=0;
                
                
            for(int j=1; j<=n; j++)
                {
                        
            if(d[location[j]]==INF)return -1
                        
            else  ans+=d[location[j]]; 
                }
                
            return ans;
            }

            void print()
            {
                 
            int i,j;
                 
            for(i=1; i<=p ; i++)
                 
            for(j=0; j<cnt[i]; j++)
                 {
                          fout
            <<i<<' '<<adj[i][j].end<<' '<<adj[i][j].len<<endl;
                 }
            }

            int main()
            {
                memset(cnt,
            0,sizeof cnt);
                fin
            >>n>>p>>c;
                
            for(int i=1; i<=n; i++)
                        fin
            >>location[i];
                
                
            for(int i=1,s,t,value; i<=c; i++)
                {
                        fin
            >>s>>t>>value;
                        adj[s][cnt[s]].end
            =t; adj[s][cnt[s]].len=value; cnt[s]++;
                        adj[t][cnt[t]].end
            =s; adj[t][cnt[t]].len=value; cnt[t]++;
                }
                
                
            int tt,min=INF;
                
                
            for(int i=1; i<=p; i++)
                {
                        tt
            =SPFA(i); 
                        
            if(tt<min&&tt!=-1) min=tt;
                }
                fout
            <<min<<endl;
                
            //system("pause");
                return 0;
            }

            posted on 2010-08-08 09:20 田兵 閱讀(1779) 評(píng)論(7)  編輯 收藏 引用 所屬分類: 算法筆記USACO

            評(píng)論

            # re: USACO chapter 3 section 2 Sweet Butter[未登錄] 2010-08-10 16:34 Klion

            樓主你第二種方法應(yīng)該是用優(yōu)先隊(duì)列引起的,自己手寫堆試試,應(yīng)該可以達(dá)到和spfa差不多的效果  回復(fù)  更多評(píng)論   

            # re: USACO chapter 3 section 2 Sweet Butter 2010-08-10 21:33 田兵

            @Klion
            為什么用優(yōu)先級(jí)隊(duì)列就變慢了,第三種方法里還用了隊(duì)列還那么快?

            難道是因?yàn)榇罅吭匾苿?dòng)?

              回復(fù)  更多評(píng)論   

            # re: USACO chapter 3 section 2 Sweet Butter[未登錄] 2010-08-10 23:49 Klion

            @田兵
            這個(gè)應(yīng)該是stl的一個(gè)比較不爽的地方吧,stl確實(shí)很方便,不過有時(shí)確實(shí)用優(yōu)先隊(duì)列會(huì)超時(shí),但是自己手寫堆可過,這個(gè)應(yīng)該和stl的實(shí)現(xiàn)有關(guān),具體的我也不是很清楚(標(biāo)稱是手寫heap+dij最后那組數(shù)據(jù)也只有0.2S)。第三種還那么快應(yīng)該是因?yàn)閟pfa快吧。  回復(fù)  更多評(píng)論   

            # re: USACO chapter 3 section 2 Sweet Butter[未登錄] 2010-08-10 23:54 Klion

            發(fā)現(xiàn)你刷usaco好快啊,我最近也在刷usaco,可以交個(gè)朋友么?
            QQ:978132955
            Email:qcx978132955@yeah.net  回復(fù)  更多評(píng)論   

            # re: USACO chapter 3 section 2 Sweet Butter 2010-08-11 08:53 田兵

            @Klion
            哦 謝謝,stl的確是有點(diǎn)慢,對(duì)大量數(shù)據(jù)時(shí)很不爽,一般如果容器內(nèi)存不夠時(shí)它會(huì)重新分配一個(gè)容量是現(xiàn)在的兩倍。  回復(fù)  更多評(píng)論   

            # re: USACO chapter 3 section 2 Sweet Butter 2010-08-11 08:58 田兵

            @Klion

            談不上刷,我們學(xué)校暑假做這個(gè),很多都是參考NOCOW上的,當(dāng)然可以交朋友,有問題我可以請(qǐng)教你了,我QQ346523942。^-^  回復(fù)  更多評(píng)論   

            # re: USACO chapter 3 section 2 Sweet Butter 2011-01-22 21:19 st8676746

            我是用STL的優(yōu)先隊(duì)列+Dijkstra過的~而且貌似不比你的SPFA慢~所以你可以檢查一下你的heap版本的Dijkstra是否寫錯(cuò)了?
            注意用堆優(yōu)化的Dijkstra必須用鄰接表,否則復(fù)雜度無法降低(甚至更慢)。  回復(fù)  更多評(píng)論   

            <2011年1月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆分類(65)

            隨筆檔案(65)

            文章檔案(2)

            ACM

            搜索

            積分與排名

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            国产激情久久久久影院老熟女| 久久97精品久久久久久久不卡| 国产精品一区二区久久| 亚洲国产精品无码久久久不卡| 欧美午夜精品久久久久久浪潮| 色成年激情久久综合| 国内精品人妻无码久久久影院| 日韩人妻无码精品久久免费一| 无码久久精品国产亚洲Av影片| 久久久亚洲AV波多野结衣| 久久青青草视频| 精品熟女少妇AV免费久久| 亚洲αv久久久噜噜噜噜噜| 中文成人无码精品久久久不卡 | 久久久WWW成人| 久久99精品九九九久久婷婷| 93精91精品国产综合久久香蕉| 久久婷婷国产麻豆91天堂| 国产精品亚洲综合专区片高清久久久| 国产成人无码精品久久久免费| 9999国产精品欧美久久久久久 | 亚洲人成电影网站久久| 伊人久久大香线蕉综合热线| 国产精品久久久久蜜芽| 国产69精品久久久久9999APGF| 久久偷看各类wc女厕嘘嘘| 久久精品视频网| 久久婷婷五月综合97色直播| 久久人人爽人人爽人人片AV东京热 | 色综合久久夜色精品国产| 精品伊人久久大线蕉色首页| 亚洲熟妇无码另类久久久| 国产精品美女久久久久网| 国产精品永久久久久久久久久 | 亚洲日韩欧美一区久久久久我 | 99精品久久精品| 久久亚洲AV无码西西人体| 性高湖久久久久久久久| 97久久精品人人澡人人爽| 久久人妻AV中文字幕| 99久久精品无码一区二区毛片 |