图论(链式前向星、最短路、最小生成树)

图论

邻接矩阵

​ 逻辑结构分为两部分:V和E集合,其中,V是顶点,E是边。因此,用一个一维数组存放图中所有顶点数据;用一个二维数组存放顶点间关系(边或弧)的数据,这个二维数组称为邻接矩阵。邻接矩阵又分为有向图邻接矩阵和无向图邻接矩阵

邻接矩阵不适合存放稀疏图。存放无向图时矩阵关于主对角线对称。

邻接矩阵一个主要的用途是与Floyd算法配合求多源最短路径。此时如果图中有重边的话,邻接矩阵中存放权值小的边信息。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int n, m, arr[105][105];

int main() {
    memset(arr, 0x3F, sizeof(arr));
    //int上限为21亿多。0x3F3F3F3F 是10亿多。 既足够大,又乘2不会爆
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int s, e, v;
        cin >> s >> e >> v;
        arr[s][e] = min(arr[s][e], v);  //重边时选较小的存储
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (j != 1)  cout << " ";
            if (arr[i][j] == 0x3F3F3F3F)  cout << 0;
            else  cout << arr[i][j];
        }
        cout << endl;
    }
    return 0;
}

多源最短路径

Floyd

Floyd算法又称为插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法

在这里插入图片描述

D:最短路径的权

S:最短路径经过的点 S [ i ] [ j ] 为 i 到 j 的最优后继 S[i][j]为i到j的最优后继 S[i][j]ij的最优后继

​ 如: S [ 2 ] [ 4 ] = 1  —— S [ 1 ] [ 4 ] = 3 —— S [ 3 ] [ 4 ] = 0 —— S [ 0 ] [ 4 ] = 4 S[2][4] = 1\ ——S[1][4]=3——S[3][4] = 0——S[0][4]=4 S[2][4]=1 ——S[1][4]=3——S[3][4]=0——S[0][4]=4(结束)

void init() {
    memset(D, 0x3F, sizeof(D));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            S[i][j] = j;
        }
    }
    return ;
}

for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (i == j)  continue;
            if (D[i][k] + D[k][j] < D[i][j]) {
                D[i][j] = D[i][k] + D[k][j];
                S[i][j] = S[i][k];
            }
        }
    }
}

例题HDU 1385

题目大意:有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度。 如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费)。 现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和。 求最小花费,如果有多条路经符合,则输出字典序最小的路径。

注意:数据保证所有城市都连通,并且 N < 500 N < 500 N<500

运用Floyd算法求出每两点的最短路径即可。 对应输出路径。

输出最短路径,和最短路径长度,最短路径如不唯一,输出字典序最小的那组
input:
5
0 3 22 -1 4
3 0 5 -1 -1
22 5 0 9 20
-1 -1 9 0 4
4 -1 20 4 0
5 17 8 3 1
1 3
3 5
2 4
-1 -1
0

output:
From 1 to 3 :
Path: 1-->5-->4-->3
Total cost : 21

From 3 to 5 :
Path: 3-->4-->5
Total cost : 16

From 2 to 4 :
Path: 2-->1-->5-->4
Total cost : 17
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

#define N 500
const int inf = 0x3F3F3F3F;

int map[N + 10][N + 10], path[N + 10][N + 10], cost[N + 10];

void init(int n) {
    memset(map, 0, sizeof(map));
    memset(path, 0, sizeof(path));
    memset(cost, 0, sizeof(cost));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            path[i][j] = j;
            scanf("%d", &map[i][j]);
            if (map[i][j] == -1)  map[i][j] = inf;
        }
    }
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &cost[i]);
    }
    return ;
}

void Floyd(int n) {
    for (int k = 1; k <= n; ++k) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (map[i][j] > map[i][k] + map[k][j] + cost[k]) {
                    map[i][j] = map[i][k] + map[k][j] + cost[k];
                    path[i][j] = path[i][k];
                }
                //当权值相同判断最优后继的字典序
                else if (map[i][j] == map[i][k] + map[k][j] + cost[k] && path[i][j] > path[i][k]) {
                    path[i][j] = path[i][k];
                }
            }
        }
    }
    return ;
}

int main() {
    int n, s, e;
    while (~scanf("%d", &n) && n) {
        init(n);
        Floyd(n);
        while (scanf("%d%d", &s, &e) && (s != -1 || e != -1)) {
            printf("From %d to %d :\nPath: %d", s, e, s);
            if (s != e) {
                for (int i = path[s][e]; ; i = path[i][e]) {
                    printf("-->%d", i);
                    if (i == e)  break;
                }
            }
            printf("\nTotal cost : %d\n\n", map[s][e]);
        }
    }
    return 0;
}

单源最短路径

Dijkstra

核心思想:两个集合(已选集合、未选集合),起初已选集合只有起点,其它点都在未选集合中。将连接两个集合的边缘信息更新。每次选出未选集合中距起点最近距离的一个点,此时该点到起点距离为最优,并纳入已选集合。

权值必须非负

这个是因为迪杰斯特拉算法是基于贪心策略,每次都找一个距源点最近的点,然后将该距离定为这个点到源点的最短路径;但如果存在负权边,那么直接得到的最短路不一定是最短路径

在这里插入图片描述

邻接矩阵版本

复杂度 O ( n 2 ) O(n^2) O(n2)

求出源 s t a r t start start到所有点的最短路径,传入顶点数 n n n,和邻接矩阵 c o s t [ ] [ ] cost[][] cost[][]

返回各点的最短路径 l o w c o s t [ ] lowcost[] lowcost[]

p r e [ ] pre[] pre[]中记录的是 s t a r t start start到点 i i i路径上的父节点, p r e [ s t a r t ] = − 1 pre[start] =-1 pre[start]=1 (此项不是 d i j k s t r a dijkstra dijkstra算法必要项)

const int MAXN = 1e3 + 5; //邻接矩阵大小,注意容易溢出

const int INF = 0x3f3f3f3f; //不能过大,容易运算时溢出

int pre[MAXN];    //存每个顶点的父节点(由谁标记而来,可反向输出最短路径)

void Dijkstra(int (*cost)[MAXN], int *lowcost, int n, int start) {
    bool vis[MAXN] = {0};           //标记顶点数组(已选&未选)
    
	//初始化
    for (int i = 1; i <= n; ++i) {
        lowcost[i] = INF;
        vis[i] = false;
        pre[i] = -1;
    }
    lowcost[start] = 0;
    
    for (int j = 1; j < n; ++j) {
        //找出本轮纳入已选集合的顶点
        int k = -1;
        int Min = INF;
        for (int i = 1; i <= n; ++i) {
            if (!vis[i] && lowcost[i] < Min) {
                Min = lowcost[i];
                k = i;
            }
        }
        if (k == -1)  break;
        vis[k] = true;
        
        //将新纳入已选集合顶点的路径信息更新到答案(lowcost)数组中
        for (int i = 1; i <= n; ++i) {
            if (!vis[i] && lowcost[k] + cost[k][i] < lowcost[i]) {
                lowcost[i] = lowcost[k] + cost[k][i];
                pre[i] = k;
            }
        }
    }
    return ;
}

//cost 邻接矩阵    lowcost 最短路答案
int cost[MAXN][MAXN], lowcost[MAXN];

int main() {
    memset(cost, 0x3f, sizeof(cost));
    int n, m, s, u, v, w;
    cin >> n >> m >> s;
    while (m--) {
        //注意:
        //测试数据中如果有重复边,只记录最短边
        //一定要注意,题目是有向边还是无向边
        cin >> u >> v >> w;
        if (w < cost[u][v])  cost[u][v] = w;
    }
    Dijkstra(cost, lowcost, n, s);
    
    //输出起点到每个点的最短路径
    for (int i = 1; i <= n; ++i) {
        if (lowcost[i] == INF)  cout << int(2e31 - 1) << " ";
        else  cout << lowcost[i] << " ";
    }
    cout << endl;
    
    //可以用类似并查集的方式输出最短路径
    for (int i = 1; i <= n; ++i) {
        printf("%d->%d : ", i, s);
        for (int j = pre[i]; ~j; j = pre[j]) {
            cout << j << " ";
        }
        cout << endl;
    }
    return 0;
}

链式前向星(邻接表) + 堆优化版本

O ( E l o g E ) O(ElogE) O(ElogE)

注意加边(有向图、无向图两种情况)

#746. 最短路

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

//无向图边数应是正常的二倍
#define N 200000

int head[N + 10], edg[N + 10], val[N + 10], Next[N + 10], tot, ans[N + 10];

struct node {
    int now, v;
    bool operator< (const node &b) const {
        return this->v > b.v;
    }
};

priority_queue<node> que;

void add_edg(int s, int e, int v) {
    ++tot;
    edg[tot] = e;
    val[tot] = v;
    Next[tot] = head[s];
    head[s] = tot;
    return ;
}

void Dijkstra(int s) {
    memset(ans, 0x3f, sizeof(ans));
    que.push((node){s, 0});
    ans[s] = 0;
    while (!que.empty()) {
        node temp = que.top();
        que.pop();
        //只有第一次以该节点进行向外探索时,相等,后续路径权值只会越来越多
        if (ans[temp.now] != temp.v)  continue;   
        for (int i = head[temp.now]; i; i = Next[i]) {
            if (ans[edg[i]] > val[i] + temp.v) {
                ans[edg[i]] = val[i] + temp.v;
                que.push((node){edg[i], ans[edg[i]]});
            }
        }
    }
    return ;
}

int main() {
    int n, m, s;
    cin >> n >> m >> s;
    for (int i = 0; i < m; ++i) {
        int s, e, v;
        scanf("%d%d%d", &s, &e, &v);
        add_edg(s, e, v);
        add_edg(e, s, v);
    }
    Dijkstra(s);
    for (int i = 1; i <= n; ++i) {
        if (ans[i] != 0x3f3f3f3f)  printf("%d\n", ans[i]);
        else  printf("-1\n");
    }
    return 0;
}


最小生成树

P3366 【模板】最小生成树

prim

核心思想:两个集合(已选集合、未选集合),起初已选集合只有起点,其它点都在未选集合中。将连接两个集合的边缘信息更新。每次选出未选集合中距已选集合最近距离的一个点,此时该点到已选集合距离为最优,并纳入已选集合。

#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

struct node {
    // 将now节点归入已选集合中所要付出的代价v
    int now, v;
    bool operator<(const node &b) const {
        return this->v > b.v;
    }
};

const int N = 5e3;   // 顶点数
const int M = 2e5;   // 边数

struct edge {
    int e, v, next;
} edg[M * 2 + 5];

int tot, head[N + 5];

void add_edg(int a, int b, int c) {
    ++tot;
    edg[tot].e = b;
    edg[tot].v = c;
    edg[tot].next = head[a];
    head[a] = tot;
    return ;
}

// dis[i]  将i节点归入已选集合中所要付出的代价  
// flag[i]  i节点是否归入已选集合
int dis[N + 5], flag[N + 5];

int prim(int n) {
    // ans 最小生成树大学  cnt 已归入已选集合节点的数量
    int ans = 0, cnt = 0;
    memset(dis, 0x3f, sizeof(dis));
    priority_queue<node> que;
    que.push((node){n, 0});
    dis[n] = 0;
    while (!que.empty()) {
        node temp = que.top();
        que.pop();
        if (flag[temp.now])  continue;
        flag[temp.now] = 1;
        ans += temp.v;
        cnt++;
        if (cnt == n)  break;
        for (int i = head[temp.now]; i; i = edg[i].next) {
            int e = edg[i].e, v = edg[i].v;
            if (flag[e] == 0 && dis[e] > v) {
                dis[e] = v;
                que.push((node){e, v});
            }
        }
    }
    if (cnt == n)  return ans;
    else  return -1;
}

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        add_edg(a, b, c);
        add_edg(b, a, c);
    }
    int ans = prim(n);
    if (~ans) {
        cout << ans << endl;
    } else {
        cout << "orz" << endl;
    }
    return 0;
}

Kruskal

核心思想:初始每个顶点独立成一个集合,每次选最短边到图中去尝试能否连接两个不相交集合。选取n-1条边即可,集合关系用并查集维护

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 5e3;
const int MAXM = 2e5;

int F[MAXN + 5];

struct Edge {
    int u, v, w;
} edge[MAXM * 2 + 5];

int tol;

void add_edg(int u, int v, int w) {
    edge[tol].u = u;
    edge[tol].v = v;
    edge[tol++].w = w;
    return ;
}

bool cmp(Edge a, Edge b) {
    return a.w < b.w;
}

int find(int x) {
    if (F[x] == -1)  return x;
    else  return F[x] = find(F[x]);
}

int Kruskal(int n) {
    memset(F, -1, sizeof(F));
    sort(edge, edge + tol, cmp);
    int cnt = 0, ans = 0;
    for (int i = 0; i < tol; ++i) {
        int u = edge[i].u;
        int v = edge[i].v;
        int w = edge[i].w;
        int t1 = find(u);
        int t2 = find(v);
        if (t1 != t2) {
            ans += w;
            F[t1] = t2;
            cnt++;
        }
        if (cnt == n - 1)  break;
    }
    if (cnt < n - 1)  return -1;
    return ans;
}

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        add_edg(a, b, c);
        add_edg(b, a, c);
    }
    int ans = Kruskal(n);
    if (~ans) {
        cout << ans << endl;
    } else {
        cout << "orz" << endl;
    }
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值