最短路徑演算法學習總結

來源:互聯網
上載者:User

標籤:style   blog   color   os   資料   io   

Dijkstra最短路徑演算法:

dijkstra 演算法的優點在於可以求出從一點到所有其他點的最短距離;

input:

5 7
1 2 10
1 3 20
1 5 30
2 5 10
2 3 5
4 5 20
4 3 30

output:

0 10 15 40 20//這是求的在這顆樹中,1到所有點的最短距離

 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 const int N=1100; 5 const int INF=1000000;  6 int map[N][N]; 7 //int p[N]; 8 int vis[N]; 9 int low[N];10 int n,m;//代表點的個數和路線的數量 11 void Dijkstra(int s)12 {13     for(int i=1;i<=n;i++)14     {15     low[i]=map[s][i];16     }17     low[s]=0;18     vis[s]=-1;19     int v;20     for(int i=1;i<n;i++)21     {22         int Min=INF;23         for(int j=1;j<=n;j++)24         {25             if(vis[j]!=-1&&low[j]<Min)26             {27                 Min=low[j];28                 v=j;29             }30         }31         vis[v]=-1;32         for(int j=1;j<=n;j++)33         {34             if(vis[j]!=-1&&low[j]>low[v]+map[j][v])35             low[j]=low[v]+map[j][v];//不斷更新兩點間的最短距離 36         }37     }38 }39 int main()40 {41     while(cin>>n>>m)42     {43         //memset(map,0,sizeof(map));44         for(int i=1;i<=n;i++)45         for(int j=1;j<=n;j++)46         map[i][j]=INF;47         memset(vis,0,sizeof(vis));48         int a,b,c;49         for(int i(1);i<=m;i++)50         {51         scanf("%d %d %d",&a,&b,&c);52         map[a][b]=map[b][a]=c;53         }54         Dijkstra(1);55         for(int i=1;i<=n;i++)56         {57             cout<<low[i]<<" ";58         }59         cout<<endl;60     }61     return 0;62 }

floyd演算法:

floyd演算法比較費空間和時間,不適合處理大量資料;

 1 #include<iostream>  2 using namespace std; 3 const int N=300;//這個地方不應該定義的太大,不然會報錯 4 const int INF=11111111; 5 int main() 6 { 7     int dis[N][N]; 8     int n,m;//分別代表點的數量和路線的條數  9     while(cin>>n>>m)10     {11         for(int i=1;i<=n;i++)12         for(int j=1;j<=n;j++)13         dis[i][j]=INF;14         for(int i=1;i<=n;i++)15         dis[i][i]=0;16         int a,b,c;17         for(int i=1;i<=m;i++)18         { 19             scanf("%d %d %d",&a,&b,&c);20             if(dis[a][b]>c)21             dis[a][b]=dis[b][a]=c;22         }23         for(int k=1;k<=n;k++)24         for(int i=1;i<=n;i++)25         {26             for(int j=i+1;j<=n;j++)27             {28                 if(dis[i][k]!=INF&&dis[k][j]!=INF&&dis[i][j]>dis[i][k]+dis[k][j])29                 dis[j][i]=dis[i][j]=dis[i][k]+dis[k][j];30             }31         }32         int w,u;33         scanf("%d %d",&w,&u);34         if(dis[u][w]==INF)printf("no solution\n");35         else printf("%d\n",dis[u][w]);36     }37     return 0;38 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.