• <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>
            隨筆-72  評(píng)論-126  文章-0  trackbacks-0
            http://acm.hdu.edu.cn/showproblem.php?pid=2807
            昨天比賽這道題目要求矩陣的乘法然后進(jìn)行比較。。
            算了一下復(fù)雜度是O(n^5)絕對(duì)超時(shí)。。

            比賽的時(shí)候一直卡著,賽后才知道有一種好的算法--矩陣比較法

            就是二維的矩陣乘以一個(gè)一維的矩陣使之降為一維,然后進(jìn)行比較

            這樣的話就只用n^2的算法進(jìn)行矩陣相乘了,復(fù)雜度降成了(n^4)順利AC、。。。

            #include<stdio.h>
            #include
            <string>
            struct H{
                
            int pos,time;
            }
            q[100000];
            struct Mat{
                
            int matrix[80][80];
                
            int yiwei[80];
            }
            city[80];
            bool map[80][80];
            int m,n;
            bool hash[80];
            int bfs(int start,int end)
            {
                
            int i,head,tail;
                head 
            = tail = 0;
                q[
            0].pos = start;
                q[
            0].time = 0;
                memset(hash,
            false,sizeof(hash));
                
            while(head <= tail)
                
            {
                    
            if(q[head].pos == end)
                        
            return q[head].time;
                    
            for(i=0;i<n;i++)
                    
            {
                        
            if(hash[i])
                            
            continue;
                        
            if(map[q[head].pos][i])
                        
            {
                            tail 
            ++;
                            q[tail].pos 
            = i;
                            q[tail].time 
            = q[head].time + 1;
                            hash[i] 
            = true;
                        }

                    }

                    head 
            ++;
                }

                
            return -1;
            }

            bool judge(int x,int y)
            {
                
            int a[80],b[80];
                
            int i,j,k;
                
            for(i=0;i<n;i++)
                
            {
                    
            if(i == x || i == y)
                        
            continue;
                    memset(a,
            0,sizeof(a));
                    memset(b,
            0,sizeof(b));
                    
            for(j=0;j<m;j++{
                        
            for(k=0;k<m;k++{
                            a[j] 
            += city[i].matrix[j][k] * (k+1);
                        }

                    }


                    
            for(j=0;j<m;j++{
                        
            for(k=0;k<m;k++{
                            b[j] 
            += city[x].matrix[j][k] * a[k];
                        }

                    }


                    
            for(j=0;j<m;j++{
                        
            if(b[j] != city[y].yiwei[j])
                            
            break;
                    }


                    
            if(j == m)
                        
            return true;
                }

                
            return false;
            }

            int main()
            {
                
            int i,j,k;
                
            while(scanf("%d%d",&n,&m),n+m)
                
            {
                    
            for(i=0;i<n;i++)
                    
            {
                        
            for(j=0;j<m;j++)
                        
            {
                            city[i].yiwei[j] 
            = 0;
                            
            for(k=0;k<m;k++)
                            
            {
                                scanf(
            "%d",&city[i].matrix[j][k]);
                                city[i].yiwei[j] 
            += city[i].matrix[j][k] * (k+1);
                            }

                        }

                    }

                    
            for(i=0;i<n;i++)
                    
            {
                        
            for(j=0;j<n;j++)
                        
            {
                            
            if(i == j)
                                map[i][j] 
            = false;
                            
            else
                                map[i][j] 
            = judge(i,j);
                        }

                    }

            //         for(i=0;i<n;i++)
            //         {
            //             for(j=0;j<n;j++)
            //                 printf("%d ",map[i][j]);
            //             puts("");
            //         }
                    int x;
                    scanf(
            "%d",&x);
                    
            while(x--)
                    
            {
                        
            int start,end,buf;
                        scanf(
            "%d%d",&start,&end);
                        start 
            --;
                        end 
            --;
                        
            if(start >= n || end >= n)
                        
            {
                            puts(
            "Sorry");
                            
            continue;
                        }

                        buf 
            = bfs(start,end);
                        
            if(buf!=-1)
                            printf(
            "%d\n",buf);
                        
            else
                            puts(
            "Sorry");
                    }

                }

                
            return 0;
            }
            posted on 2009-04-20 15:47 shǎ崽 閱讀(1108) 評(píng)論(2)  編輯 收藏 引用

            評(píng)論:
            # re: 又學(xué)了一招,矩陣的比較 2009-05-02 21:18 | dragon123
            您這樣做是不對(duì)的吧

            1*2矩陣 (1000,2) 和(996,4)
            1000*1+2*2==996*1+4*2 但是它們不是相同的吧?
              回復(fù)  更多評(píng)論
              
            # re: 又學(xué)了一招,矩陣的比較 2009-05-06 18:42 | shǎ崽
            這個(gè)有一定概率性的
            不過(guò)出現(xiàn)這樣的幾率非常小

            也可以隨機(jī)出數(shù)據(jù)
            for(int i = 0; i < n; i ++)
            ku[i] = rand()%10000;

            然后用數(shù)組ku作為一維數(shù)組去進(jìn)行降維  回復(fù)  更多評(píng)論
              

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


            97久久久精品综合88久久| 久久综合伊人77777麻豆| 久久综合九色综合网站| 久久影视综合亚洲| 久久久久久久免费视频| 久久久这里只有精品加勒比| 国内精品久久久久影院老司| 亚洲伊人久久大香线蕉综合图片| 久久人与动人物a级毛片| 无码国内精品久久人妻| 国产精品久久久福利| 久久精品国产WWW456C0M| 久久国产AVJUST麻豆| 久久精品国产亚洲av日韩| 久久免费小视频| 18禁黄久久久AAA片| 久久婷婷五月综合色高清| 久久精品一区二区| 欧美成人免费观看久久| www.久久99| 亚洲欧美久久久久9999| 97久久婷婷五月综合色d啪蜜芽| 色诱久久久久综合网ywww| 国产成人久久777777| 久久丫精品国产亚洲av| 欧美与黑人午夜性猛交久久久| 久久精品国产亚洲AV香蕉| 亚洲国产精品综合久久网络 | 久久久久香蕉视频| 伊人久久综合无码成人网| 久久精品草草草| 人妻精品久久久久中文字幕69 | 精品99久久aaa一级毛片| 久久久无码精品亚洲日韩蜜臀浪潮| 国产高潮国产高潮久久久91 | 久久影视综合亚洲| 日本精品久久久久中文字幕8| 色狠狠久久AV五月综合| 波多野结衣久久精品| 天天影视色香欲综合久久| 久久99亚洲综合精品首页|