各種最短路和路徑輸出

dijkstra

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 50;
const int INF = 65533;
int path[maxn];
int dist[maxn];
int vist[maxn];
int grap[maxn][maxn];
int n;
int st, ed;
void init() {
   for (int i = 1; i <= n; i ++) {
    for (int j = 1; j <= n; j ++) {
        grap[i][j] = INF;
    }
   }
}
void dijkstra(int s) {
   memset(vist, 0, sizeof(vist));
   for (int i = 1; i <= n; i ++) dist[i] = grap[s][i];
   for (int i = 1; i <= n; i ++) path[i] = s;
   dist[s] = 0;
   vist[s] = 1;
   path[s] = -1;
   int u = -1, mind = INF;
   for (int i = 1; i < n; i ++) {
     u = -1;
     mind = INF;
     for (int j = 1; j <= n; j ++) {
        if (!vist[j] && dist[j] < mind) {
            mind = dist[j];
            u = j;
        }
     }
     vist[u] = 1;
     for (int j = 1; j <= n; j ++) {
        if (!vist[j] && dist[j] > dist[u]+grap[u][j]) {
            dist[j] = dist[u] + grap[u][j];
            path[j] = u;
        }
     }
   }
}

void print_road(int s) {
   if (path[s] != -1) print_road(path[s]);
   else
    return;
   printf("-->%d", s);
}
int main() {
    int m;
    while (scanf("%d%d", &n, &m)!=EOF) {
        init();
        int x, y;
        for (int i = 1; i <= m; i ++) {
            scanf("%d%d", &x, &y);
            scanf("%d", &grap[x][y]);
        }
        scanf("%d%d", &st, &ed);
        dijkstra(st);
        printf("%d", st);
        print_road(ed);
        printf("\n");
        printf("%d\n", dist[ed]);
    }
return 0;
}

spfa

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 50;
const int INF = 65533;
struct node{
    int u, v, w;
}temp;
bool vist[maxn];
vector<node>grap[maxn];
int n;
int dist[maxn];
int path[maxn];
int st, ed;
void init() {
   for (int i = 0; i <= n; i ++) {
    vist[i] = false;
    grap[i].clear();
    dist[i] = INF;
    path[i] = -1;
   }
}

bool spfa(int st) {
    queue<int>q;
    q.push(st);
    dist[st] = 0;
    int u;
    int cnt = 0;
    vist[st] = true;
    while (!q.empty()) {
        u = q.front();
        q.pop();
        vist[u] = false;
        cnt ++;
        if (cnt > n) return false;
        for (int i = 0; i <(int)grap[u].size(); i ++) {
            if (dist[grap[u][i].v] > dist[u] + grap[u][i].w) {
                dist[grap[u][i].v] = dist[u]+grap[u][i].w;
                path[grap[u][i].v] = u;
                if (vist[grap[u][i].v] == false) {
                     q.push(grap[u][i].v);
                     vist[grap[u][i].v] = true;
                }
            }
        }
    }
return true;
}

void print_road(int s) {
   if (path[s] != -1) print_road(path[s]);
   else
    return;
   printf("-->%d", s);
}

int main() {
    int m;
    while (scanf("%d%d", &n, &m)!=EOF) {

        init();
        for (int i = 1; i <= m; i ++) {
            scanf("%d%d%d", &temp.u, &temp.v, &temp.w);
            grap[temp.u].push_back(temp);
        }
        scanf("%d%d", &st, &ed);
        if (spfa(st) == false) {
            printf("有環\n");
            continue;
        }

        printf("%d", st);
        print_road(ed);
        printf("\n");
        printf("%d\n", dist[ed]);
    }
return 0;
}
boolman__ford仿照上面

floyd

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 50;
const int INF = 65533;
int grap[maxn][maxn];
int path[maxn][maxn];

int n, m;

void floyd() {
    for (int i = 1; i <= n; i ++) {
        for (int j = 1; j <= n; j ++) {
            path[i][j] = j;
        }
    }

    for (int k = 1; k <= n; k ++) {
        for (int i = 1; i <= n; i ++) {
            for (int j = 1; j <= n; j ++) {
                if (grap[i][j] > grap[i][k] + grap[k][j]) {
                    grap[i][j] = grap[i][k]+grap[k][j];
                    path[i][j] = path[i][k];
                }
            }
        }
    }
}
int main() {
    while (scanf("%d%d", &n) != EOF) {
        for (int i = 1; i <= n; i ++) {
            for (int j = 1; j <= n; j ++) {
                scanf("%d", &grap[i][j]);
            }
        }
        floyd();
        int x, y;
        while (scanf("%d%d", &x, &y), x || y) {
            printf("%d\n", grap[x][y]);
            printf("%d", x);
            while (x != y) {
                printf("-->%d", path[x][y]);
                x = path[x][y];
            }
            printf("\n");
        }
    }
return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章