【floyd】HDU 1874 暢通工程續

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   2014   

之後的題解偏重實用/總結性質,盡量理解演算法本身而不是題,時間複雜度什麼的也可以放放。

很久之前做過這個題,當時使用dijkstra做的,關於幾個最短路演算法,分類的話可以分為以下幾種。

1、單源最短路:已知起點(終點),計算從源點到其他各個頂點的最短路徑長度。

典型演算法:Dijkstra,Bellman-Ford(可以算負的,比較慢),spfa(負權能用,加了鬆弛操作,速度比較炸天)

2、全域最短路:從一點到另一點,典型如Floyd,A*啟發學習法演算法。

重新用floyd寫一遍:

#include <iomanip>#include <string.h>#include <iostream>using namespace std;const int INF=0x3f3f3f3f;int map[305][305];int path[305][305];bool visited[10005];int prev[10005];int waypoint;void clearmap(){    for (int i=0;i<105;i++)    {        for (int j=0;j<105;j++)        {            map[i][j]=INF;        }    }    memset(path,INF,sizeof(path));    memset(prev,0,sizeof(prev));    memset(visited,0,sizeof(visited));}void floyd(){for(int k=0;k<waypoint;k++){for(int i=0;i<waypoint;i++){for(int j=0;j<waypoint;j++){if(map[i][k]!=INF && map[k][j]!=INF){if(map[i][j]>map[i][k]+map[k][j]){map[i][j]=map[i][k]+map[k][j];path[i][j]=path[k][j];}}}}}}int main(){    int route;    while (cin>>waypoint>>route)    {       clearmap();       for(int i=0;i<route;i++)       {               int a,b,dis;               cin>>a>>b>>dis;               if(map[a][b]>dis)               {               map[a][b]=map[b][a]=dis;                   }                  }       floyd();        int start,end;        cin>>start>>end;                if(start==end)        {        cout<<0<<endl;        }        else if(map[start][end]!=INF)            cout<<map[start][end]<<endl;        else            cout<<"-1"<<endl;    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.