鍥捐 鏈€鐭矾 鍩虹

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍥捐 鏈€鐭矾 鍩虹相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/fat' title='fat'>fat   鏃堕棿   turn   鐢宠   鍙屽悜   -o   spfa   

鍥捐鍩虹 锛?鏈€鐭矾

(v_i) 鍜屾渶鍚庝竴涓《鐐?(v_j) 鏄悓涓€涓《鐐癸紝鍒欑О杩欐潯璺緞涓哄洖璺?/p>

杩為€?/strong> 锛?涓や釜鐐硅繛閫氾紝鎸?u 鍜?v 鐩镐簰鍙揪

鍥捐繛閫?/strong> 锛氭棤鍚戝浘 浠绘剰涓や釜鑺傜偣 鐩镐簰鍙揪 锛?/p>

寮鸿繛閫?/strong> 锛?鏈夊悜鍥?浠绘剰 涓や釜鑺傜偣 鐩镐簰鍙揪

鍥剧殑瀛樺偍缁撴瀯锛?/h3>
  • 閭绘帴鐭╅樀 锛堜簩缁存暟缁勶級 瀵逛簬 n 涓《鐐癸紝闇€瑕?(O(n^2)) 绌洪棿

    • 涓ラ噸娴垂绌洪棿锛岄€傜敤浜?绋犲瘑鍥?/strong> 锛?鎴栬€呮槸 锛岄鐩殑杈撳叆 宸茬煩闃电殑鏂瑰紡缁欏嚭

    • G[N] [N]

      #include <iostream>
      #include <cstring>
      using namespace std;
      const int N = 1e3 + 233,INF = 0x3f3f3f3f; //INF 鏈€澶у€?int g[N][N];
      int main(){
          int n,m,x,y,w;
          memset(g,0x3f,sizeof g); // 鍒濆鍖栧浘锛屽叏閮戒笉鑱旈€?    cin >> n >> m;
          while(m--){
              cin >> x >> y >> w;
              g[x][y] = w; // 鍗曞悜杈癸紝x to y 锛屾潈閲?涓?w
              g[y][x] = w; // 涓ゆ潯閮藉啓锛岀瓑浜庡弻鍚戣竟锛?鍥句腑閮戒负鍙屽悜杈癸紝灏辨垚鏃犲悜鍥句簡銆?    }
          for(int i = 1;i <= n; ++i){
              for(int j = 1;j <= n; ++j){
                  cout << g[i][j] <<" ";
              }
              cout << endl;
          }
          return 0;
      }
  • 閭绘帴琛?/strong>

    瀵瑰浘涓殑姣忎釜椤剁偣閮藉缓绔嬩竴涓崟閾捐〃锛屽瓨鍌ㄨ繖涓《鐐圭殑杩炲嚭鐨勭偣

    • vector Adj[N];

    • vector 鑷韩搴曞眰鍑芥暟鐨勭己闄凤紝 姣忔鎵╁ぇ绌洪棿锛岄兘浼歝opy涓€杈癸紝鐒跺悗鎵╁ぇ鍘熸潵绌洪棿鐨勪袱鍊?/p>

    • vector G[]锛?br /> 浼樼偣锛?br /> 1.鍐欒捣鏉ユ瘮閾惧紡鍓嶅悜鏄熷揩锛堝ぇ姒?br /> 2.姣忎釜椤剁偣鐨勫嚭杈归兘鏄敤vector瀛樺偍鐨勶紝鏂逛究鎵ц涓€浜汼TL涓殑鍑芥暟锛堟瘮濡傛帓搴忥級
      缂虹偣
      1.STL浼氱暐鎱竴浜?br /> 2.娴垂绌洪棿锛岀敱浜巚ector鐢宠绌洪棿鐨勬柟寮忔槸涓ゅ€嶆墿瀹癸紝閬囧埌鍗$┖闂寸殑棰樼洰鐨勬椂鍊欎細璺?/p>

      #include <iostream>
      #include <cstring>
      #include <vector>
      using namespace std;
      const int N = 1e3 + 233,INF = 0x3f3f3f3f;
      struct edge
      {
          int node ,weight;
          edge(int node_,int weight_)://鏋勯€犲嚱鏁帮紝璁╂垜浠彲浠ョ洿鎺ョ粰缁撴瀯浣撹祴鍊?        node(node_),weight(weight_){}
      };
      vector<edge> v[N];
      int n,m;
      int main()
      {
          cin >> n >> m;
        while(m--)
          {
              int x,y,w;
              cin >> x >> y >> w;
              v[x].push_back(edge(y,w));//妯℃嫙閾捐〃锛屽瓨杈?        //v[y].push_back(edge(x,w));//鍙屽悜
          }
          for(int i = 1;i <= n; ++i){
              for(int j = 0;j < v[i].size(); ++j){
                  cout << i << " " << v[i][j].node <<" " << v[i][j].weight <<"  ";
              }
              cout << endl;
          }
          return 0;
      }
  • 閾惧紡鍓嶅悜鏄?/strong> 锛堟暟缁勬ā鎷熼偦鎺ヨ〃锛?/p>

    • 鍏堝浼氭暟缁勬ā鎷熼摼琛紝灏变細鏁扮粍妯℃嫙閭绘帴琛ㄤ簡
    • 閬垮厤浜哻opy鏃堕棿鐨勬氮璐癸紝闈炲父蹇€?/li>
    • 涓昏浣跨敤杩欑瀛樺浘缁撴瀯

    鍓嶇疆鐭ヨ瘑锛屾暟缁勬ā鎷熼摼琛?澶存彃娉?

    #include <iostream>
    #include <cstring>
    #include <vector>
    using namespace std;
    const int N = 1e3 + 233,INF = 0x3f3f3f3f;
    int e[N],ne[N],h,idx; 
    // head 琛ㄧず澶寸粨鐐圭殑涓嬫爣
    // e[i] 琛ㄧず鑺傜偣i鐨勫€?// ne[i] 琛ㄧず鑺傜偣i鐨刵ext鎸囬拡鏄灏?// idx 瀛樺偍褰撳墠宸茬粡鐢ㄥ埌浜嗗摢涓偣
    void init(){ // 鍒濆鍖栦负澶磋妭鐐?涓?-1
        h = -1,idx = 0;
    }
    void add(int x){
        e[idx] = x;// 淇濆瓨x鐨勫€?鍒?idx 杩欎釜鎸囬拡鐨勪綅缃?    ne[idx] = h;// idx 鐨勪笅涓€涓寚閽?涓?澶磋妭鐐?    h = idx++; // 澶磋妭鐐?= idx 鎸囬拡  锛?idx ++
    }
    int main(){
        int n ,x;
        cin >> n;
        init();
        while(n--){
            cin >> x;
            add(x);
        }
        for(int i = h;i != -1; i = ne[i]){
            cout << e[i] << " ";
        }
        return 0;
    }
    #include <iostream>
    #include <cstring>
    #include <vector>
    using namespace std;
    const int N = 1e3 + 233,INF = 0x3f3f3f3f;
    int e[N],ne[N],h[N],w[N],idx;//鏁扮粍妯℃嫙閭绘帴琛紝 h浠h〃 n涓ご鑺傜偣
    // e瀛樻瘡鏉¤竟锛宯e鎸囧悜涓嬩竴鏉¤竟锛宧鏄摼琛ㄥご锛寃鏄瘡涓《鐐圭殑鏉冮噸锛宨dx鏄储寮曞€?void add(int a,int b,int c){// 閲囩敤澶存彃娉?    e[idx] = b;
        ne[idx] = h[a];
        w[b] = c;
        h[a] = idx++;
    }
    int n,m;
    int main()
    {
        memset(h,-1,sizeof h);//鍒濆鍖栧ご鑺傜偣锛屽叏涓?1
        int a,b,c;
        cin>>n>>m;
        while(m--)
        {
            cin >> a >> b >> c;
            add(a,b,c); // 鍗曞悜杈?        //add(b,a,c) 鍙屽悜
        }
        for(int i = 0;i < n; ++i){
            for(int j = h[i];j != -1;j = ne[j]){
                int v = e[j];
                cout << i <<" " <<v<<" "<<w[v]<<" ";
            }
            cout << endl;
        }
        return 0;
    }

鍥剧殑閬嶅巻

DFS 锛堟眰杩為€氬潡锛夛紝姹傛鎷夊洖璺?涓?鍝堝瘑椤垮洖璺?/p>

渚嬮 锛?https://vjudge.net/problem/UVA-572

  • 鐢?dfs 鏈?鍏釜鏂瑰悜 鎼?锛屾瘡鎵惧埌涓€涓?锛屽仛涓€娆℃爣璁帮紝鏈€鍚庤緭鍑鸿繛閫氬潡鐨勬暟鐩?/li>
#include <iostream>
#include <cstring>
using namespace std;
const int N = 233;
char g[N][N];
int n,m,ans;
int dx[8] = {0, 0,1,1, 1,-1,-1,-1}; // 鏂瑰悜鏁扮粍 
int dy[8] = {1,-1,0,1,-1, 0, 1,-1};
bool pd(int x,int y){ // 鍒ゆ柇鏄惁瓒婄晫
    if(x >= 1 && x <= m && y >= 1 && y <= n) return 1;
    else return 0;
}
void dfs(int x,int y){ // dfs姹傝繛閫氬潡
    for(int i = 0;i < 8; ++i){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(pd(nx,ny) && g[nx][ny] == '@'){
            g[nx][ny] = '*';
            dfs(nx,ny);
        }
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    while(cin >> m >> n){
        if(m == 0 && n == 0) break;
        memset(g,0,sizeof g);
        ans = 0;
        for(int i = 1;i <= m; ++i){
            for(int j = 1;j <= n; ++j)
                cin >> g[i][j];
        }
        for(int i = 1;i <= m; ++i){
            for(int j = 1;j <= n; ++j){
                if(g[i][j] == '@'){
                    ans++; // 杩為€氬潡涓暟++
                    g[i][j] = '*';
                    dfs(i,j);
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

BFS 锛堣竟鏉冧负 1 鐨勬渶鐭矾绠楁硶 锛岄槦鍒楀疄鐜?锛?/p>

渚嬮 锛?https://vjudge.net/problem/OpenJ_Bailian-3752

  • 鐢ㄩ槦鍒?鎵╁睍 锛屽鏋滄壘鍒?鍑哄彛锛岀洿鎺ヨ烦鍑哄嵆鍙?/li>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 50;
char g[N][N];
int r,c,ans;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
bool vis[N][N]; // 鍒ゆ柇鏄惁璁块棶杩?鐐?struct Ponit{
    int x,y,t; // x,y 鍧愭爣锛?t 浠h〃褰撳墠鐨勬鏁?    Ponit(int x,int y,int t):x(x),y(y),t(t){} // 鏋勯€犲嚱鏁?};
bool pd(int x,int y){
    if(x >= 1 && x <= r && y >= 1 && y <= c) return 1;
    else return 0;
}
void bfs(){
    queue<Ponit> q;
    q.push({1,1,1}); // 鍒濆鍖?闃熷垪
    vis[1][1] = 1;
    while(q.size()){
        Ponit t = q.front();
        q.pop();
        for(int i = 0;i < 4; ++i){
            int nx = t.x + dx[i];
            int ny = t.y + dy[i];
            if(nx == r && ny == c) {
                ans = t.t + 1;
                return ;
            }
            if(pd(nx,ny) && g[nx][ny] == '.' && !vis[nx][ny]){
                vis[nx][ny] = 1;
                q.push({nx,ny,t.t + 1});
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin >> r >> c;
    for(int i = 1;i <= r; ++i){
        for(int j = 1;j <= c; ++j)
            cin >> g[i][j];
    }
    bfs();
    cout << ans;


    return 0;
}

#include <bits/stdc++.h> using namespace std; const int N = 510; int g[N][N],n,m,dist[N]; bool st[N]; // dist[i] 浠h〃 1 鍒?i鐨勬渶鐭矾 int dijkstra(){ memset(dist,0x3f,sizeof dist); dist[1] = 0; // 寰幆 n 娆★紝姣忔纭畾涓€涓渶鐭矾鐨勫€? for(int i = 0;i < n; ++i){ int t = -1; // 鍦ㄦ墍鏈?st 涓?false鐨勭偣涓紝鎵惧埌 dist鏈€灏忕殑鐐? for(int j = 1;j <= n; ++j){ if(!st[j] && (t == -1 || dist[t] > dist[j])){ t = j; } } st[t] = 1; for(int j = 1;j <= n; ++j){ dist[j] = min(dist[j],dist[t] + g[t][j]); // dist[t] + g[t][j] 琛ㄧず锛?1 - t鐨勬渶鐭矾 鍔犱笂 t 鍒?j鐨勮窛绂伙紝 // 绛変环浜?1 - j 鐨勮窛绂? } } if(dist[n] == 0x3f3f3f3f) return -1; else return dist[n]; } int main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin >> n >> m; memset(g,0x3f,sizeof g); while(m--){ int a,b,c; cin >> a >> b >> c; g[a][b] = min(g[a][b],c); // a 鍜?b涔嬮棿鍙兘鏈夐噸杈癸紝鍙栨渶灏忕殑杈逛负鏉冨€? } cout << dijkstra(); return 0; }

鍫嗕紭鍖杁ij

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1e5 + 233;
typedef pair<int,int> PII;
int dist[N],e[N],w[N],ne[N],h[N],idx,n,m,x,y,z;
bool st[N];
void add(int a,int b,int c){
    e[idx] = b,w[idx] = c,ne[idx] = h[a],h[a] = idx ++;
}
int dij(){
    memset(dist,0x3f,sizeof dist);
    priority_queue<PII,vector<PII>,greater<PII>> heap; // 鍒濆鍖栧爢
    dist[1] = 0;
    heap.push({0,1});
    while(heap.size()){
        auto t = heap.top();// log n 鎵惧埌鏈€灏忕殑璺濈
        heap.pop();
        int distance = t.first,ver = t.second;
        if(st[ver]) continue;
        st[ver] = true;
        for(int i = h[ver];i != -1;i = ne[i]){
            int j = e[i];
            if(dist[j] > distance + w[i]){
                dist[j] = distance + w[i];
                heap.push({dist[j],j});
            }
        }   
    }
    if(dist[n] == 0x3f3f3f3f) return -1;
    else return dist[n];
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin >> n >> m;
    memset(h,-1,sizeof h);
    while(m--){
        cin >> x >> y >> z;
        add(x,y,z);
    }
    cout << dij();
    return 0;
}

Bell-Ford O(ne) 鍙互澶勭悊璐熸潈杈癸紝涓嶈兘澶勭悊璐熸潈鍥炶矾锛岃礋鏉冨洖璺病鏈夋渶鐭矾

// 鍙兘瀛樿竟

绠楁硶姝ラ锛?/p>

杩涜n-1娆℃澗寮涙搷浣?/p>

姣忔寰幆 m 娆?锛屾洿鏂版瘡鏉¤竟鐨勮窛绂?/p>

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 23;
int dist[510],st[510];
int n , m;
struct edge{
    int from,to,w;
}e[N];
void bf(){
    memset(dist,0x3f,sizeof dist);
    dist[1] = 0;
    for(int i = 0;i < n-1; ++i){
        for(int j = 0;j < m; ++j){
            int a = e[i].from,b = e[i].to, t = e[i].w;
            dist[b] = min(dist[b],dist[a] + t);
        }
    }
}
int main(){
    cin >> n >> m;
    for(int i = 0;i < m; ++i){
        cin >> e[i].from >> e[i].to >> e[i].w;
    }
    bf();
    for(int i = 1;i <= n; ++i){
        cout << dist[i] <<" ";
    }
    return 0;;
}

SPFA 锛堥槦鍒椾紭鍖栫増鏈殑 Bell-Ford锛?鍙互鍒ゆ柇璐熺幆锛屽鏄撹缃戞牸鍥惧崱锛?/p>

dist[b] = min(dist[b],dist[a] + t);  // 浼樺寲 dist a
while(闃熷垪涓嶇┖ )  , {
    鍙栧嚭闃熷ご  t
    鍒犻櫎闃熷ご
    鍒犳帀闃熷ご鐨勬爣璁?    鏇存柊閬嶅巻  t 鐨勬墍鏈夊嚭杈?{
        鏇存柊姣忎釜鐐圭殑璺濈锛屽鏋滄洿鏂版垚鍔燂紝骞朵笖褰撳墠鐨勭偣娌℃湁鍏ラ槦锛?            閭d箞灏卞姞鍏ラ槦鍒?            涓€涓偣鍙兘鍏ラ槦涓€娆★紝涓嶈兘澶氭鍏ラ槦銆?    }
    
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 23;
int dist[510],st[510];
int e[N],ne[N],w[N],h[N],idx,n,m;
int a, b,c;
void add(int a,int b,int c){
    e[idx] = b;
    w[idx] = c;
    ne[idx] = h[a];
    h[a] = idx++;
}
void spfa(){
    memset(dist,0x3f,sizeof dist);
    dist[1] = 0;
    st[1] = 1;
    queue<int> q; // 瀛樻墍鏈夊彉灏忎簡鐨勮妭鐐?    q.push(1);
    while(q.size()){
        int t = q.front();
        q.pop();
        st[t] = 0;
        for(int i = h[t];i != -1; i = ne[i]){
            int j = e[i];
            if(dist[j] > dist[t] + w[i]){
                
                dist[j] = dist[t] + w[i];
                //cnt[j] = cnt[t] + 1;  鍒ゆ柇璐熺幆
                //if (cnt[j] >= n) return true;  鐩存帴璺冲嚭
                if(st[j] == 0){
                    st[j] = 1;
                    q.push(j);
                }
            }
        }
    }

}
int main(){
    cin >> n >> m;
    memset(h,-1,sizeof h);
    for(int i = 0;i < m; ++i){
        cin >> a >> b >> c;
        add(a,b,c);
    }
    spfa();
    for(int i = 1;i <= n; ++i){
        cout << dist[i] <<" ";
    }
    return 0;;
}

spfa https://blog.csdn.net/u011644423/article/details/38345631

? https://skywt.cn/posts/spfasummary/

POJ 1511锛?259

澶氭簮姹囨渶鐭矾 DP鎬濇兂锛屾毚鍔?,鍙兘鐢?閭绘帴鐭╅樀瀛樺浘

Floyed O(n^3), 杩樺彲浠ョ敤鏉ュ垽鏂紝鍥句腑鐨勪袱涓偣鏄惁鐩歌繛

void floyd(){
    for(int k = 1;k <= n; ++i){
        for(int i = 1;i <= n; ++i){
            for(int j = 1;j <= n; ++j){
                g[i][j] = min(g[i][j],g[i][k] + g[k][j]);
            }
        }
    }
}

涔犻鎺ㄨ崘锛?/p>

HDU 1869锛?665锛?596锛?734

POJ 1125

鏈€鐭矾鐨勫簲鐢?锛?宸垎绾︽潫绯荤粺锛堜笉绛夊紡姹傝В锛?/p>

涔犻锛?/h3>

kuangbin鏈€鐭矾涓撻 锛宧ttps://vjudge.net/contest/324762

鎺ㄨ崘闃呰锛?/p>

  • 娲涜胺鏃ユ姤 锛?/li>

https://www.luogu.org/blog/wym483739/xue-tu-lun-ni-zhen-di-liao-xie-zui-duan-lu-ma-post

https://studyingfather.blog.luogu.org/some-coding-tips-for-oiers some tips for oiers

https://www.luogu.org/blog/chengni5673/tu-lun-di-xiao-ji-qiao-yi-ji-kuo-zhan 鍥捐灏忔妧宸?/p>

https://www.luogu.org/blog/little-sun/dijkstra dij璇﹁В

https://www.luogu.org/blog/encore/io-you-hua-nei-suo-shi oi 浼樺寲

https://oi-wiki.org/graph oi-wiki-鍥捐涓撻

以上是关于鍥捐 鏈€鐭矾 鍩虹的主要内容,如果未能解决你的问题,请参考以下文章

骞跺彂鐩稿叧闂浠ュ強java鍩虹鐭ヨ瘑

绗竴绔狅細鍩虹绠楁硶

[LeetCode] 244. Shortest Word Distance II 鏈€鐭崟璇嶈窛绂?II

[kuangbin甯︿綘椋瀅涓撻鍥?鏈€鐭矾缁冧範 F - Wormholes 锛堝垽鏂礋鐜級

Linux绯荤粺鍩虹鐭ヨ瘑鐩綍銆佸厓鏁版嵁銆佽蒋纭摼鎺ャ€佹枃浠剁鐞嗗懡浠ゃ€乿im鏌ユ壘鏇挎崲銆乿imtab缂╄繘

銆愯绋嬭璁★綔C++銆慍++绋嬪簭璁捐鍩虹锛氱煩闃佃繍绠楀櫒

(c)2006-2024 SYSTEM All Rights Reserved IT常识