当前位置:   article > 正文

算法总结归纳(第十二天)(剩余的图论)

算法总结归纳(第十二天)(剩余的图论)

目录

一、图论

Ⅰ、spfa算法

spfa求最短路

思路:

代码:

spfa判断负环

思路:

代码:

Ⅱ、floyd算法

思路:

代码:

Ⅲ、prime算法

思路:

代码:

Ⅳ、kruskai算法

思路:

代码:

Ⅴ、染色法判定二分图

思路:

代码:

Ⅵ、匈牙利算法(二分图)

思路

代码:


一、图论

Ⅰ、spfa算法

spfa求最短路

题目链接:spfa求最短路

思路:

本题使用的是队列求解,思路与dijkstra有相似之处,使用邻接表进行存储,使用w数组存储每个边的权重,然后t表示上一层的结点,j表示它的儿子结点,dist[j] > dist[t] + w[i]来更新边长,从而使得边长变为最小。

代码:
  1. #include<iostream>
  2. using namespace std;
  3. #include<cstring>
  4. #include<queue>
  5. const int N = 1e6 + 10;
  6. int e[N], ne[N], h[N], w[N], idx;
  7. int dist[N];
  8. bool st[N], cnt;
  9. int n, m;
  10. void add(int a, int b, int c)
  11. {
  12. e[idx] = b; ne[idx] = h[a]; w[idx] = c; h[a] = idx ++;
  13. }
  14. int spfa()
  15. {
  16. memset(dist, 0x3f3f3f3f, sizeof(dist));
  17. dist[1] = 0;
  18. st[1] = true;
  19. queue<int> q;
  20. q.push(1);
  21. while(q.size())
  22. {
  23. int t = q.front();
  24. q.pop();
  25. st[t] = false;
  26. for(int i = h[t]; i != -1; i = ne[i])
  27. {
  28. int j = e[i];
  29. if(dist[j] > dist[t] + w[i])
  30. {
  31. dist[j] = dist[t] + w[i];
  32. if(!st[j])
  33. {
  34. q.push(j);
  35. st[j] = true;
  36. }
  37. }
  38. }
  39. }
  40. if(dist[n] == 0x3f3f3f3f){
  41. cnt = true;
  42. return 1;
  43. }
  44. return dist[n];
  45. }
  46. int main()
  47. {
  48. cin>>n>>m;
  49. memset(h, -1, sizeof(h));
  50. while( m -- )
  51. {
  52. int x, y, z;
  53. cin>>x>>y>>z;
  54. add(x, y, z);
  55. }
  56. int t = spfa();
  57. if(cnt) puts("impossible");
  58. else cout<<t;
  59. return 0;
  60. }

spfa判断负环

题目链接:spfa判断负环

思路:

本题判断负环就是根据判断是否有环来判断,cnt数组用来判断当前点经过的边数有几条,如果大于n条边,说明经过了n + 1个点。

代码:
  1. #include<iostream>
  2. using namespace std;
  3. #include<cstring>
  4. #include<queue>
  5. const int N = 1e5 + 10;
  6. int idx, e[N], ne[N], h[N], w[N];
  7. int n, m;
  8. bool st[N];
  9. int cnt[N], dist[N];
  10. void add(int a, int b, int c)
  11. {
  12. e[idx] = b; ne[idx] = h[a]; w[idx] = c; h[a] = idx++;
  13. }
  14. bool spfa()
  15. {
  16. queue<int> q;
  17. for(int i = 1; i<=n; i++){
  18. q.push(i);
  19. st[i] = true;
  20. }
  21. while(q.size())
  22. {
  23. int t = q.front();
  24. q.pop();
  25. st[t] = false;
  26. for(int i = h[t]; i != -1; i = ne[i])
  27. {
  28. int j = e[i];
  29. if(dist[j] > dist[t] + w[i]){
  30. dist[j] = dist[t] + w[i];
  31. cnt[j] = cnt[t] + 1;
  32. if(cnt[j] >= n) return true;
  33. if(!st[j])
  34. {
  35. q.push(j);
  36. st[j] = true;
  37. }
  38. }
  39. }
  40. }
  41. return false;
  42. }
  43. int main()
  44. {
  45. cin>>n>>m;
  46. memset(h, -1, sizeof(h));
  47. while( m -- )
  48. {
  49. int a, b, c;
  50. cin>>a>>b>>c;
  51. add(a, b, c);
  52. }
  53. if(spfa()) puts("Yes");
  54. else puts("No");
  55. return 0;
  56. }

Ⅱ、floyd算法

题目链接:floyd求最短路

思路:

floyd算法就是简单的三重循环,时间复杂度是n的三次方。

多源汇问题,具有多个源点,算法实现基于动态规划推出来的,直接背熟模板就可以。

  1. for(int k = 1; k <= n; k++){
  2. for(int i = 1; i<=n; i++){
  3. for(int j = 1; j<=n; j++){
  4. d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  5. }
  6. }
  7. }

上面这段是核心代码,只需要背熟就好。具体推导可问度娘。

代码:

  1. #include<iostream>
  2. using namespace std;
  3. const int N = 210, INF = 1e9;
  4. int n, m, k, x, y, z;
  5. int d[N][N];
  6. void floyd()
  7. {
  8. for(int k = 1; k <= n; k++){
  9. for(int i = 1; i<=n; i++){
  10. for(int j = 1; j<=n; j++){
  11. d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  12. }
  13. }
  14. }
  15. }
  16. int main()
  17. {
  18. cin>>n>>m>>k;
  19. for(int i = 1; i <= n; i++){
  20. for(int j = 1; j<=n; j++){
  21. if(i == j) d[i][j] = 0;
  22. else d[i][j] = INF;
  23. }
  24. }
  25. while(m -- )
  26. {
  27. cin>>x>>y>>z;
  28. d[x][y] = min(d[x][y], z);
  29. }
  30. floyd();
  31. while( k -- )
  32. {
  33. cin>>x>>y;
  34. if(d[x][y] > INF / 2) puts("impossible");
  35. else cout<<d[x][y]<<endl;
  36. }
  37. return 0;
  38. }

Ⅲ、prime算法

题目链接:prime求最小生成树

思路:

prime判断最小生成树,就是使用一个dt数组表示当前数组距离生成树的距离大小,同时使用邻接矩阵g进行图的存储,st数组表示的是否已经存储进树里面。

代码:

  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. const int N = 510;
  6. int g[N][N];//存储图
  7. int dt[N];//存储各个节点到生成树的距离
  8. int st[N];//节点是否被加入到生成树中
  9. int n, m;//n 个节点,m 条边
  10. void prim()
  11. {
  12. memset(dt,0x3f, sizeof(dt));//初始化距离数组为一个很大的数(10亿左右)
  13. int res= 0;
  14. dt[1] = 0;//从 1 号节点开始生成
  15. for(int i = 0; i < n; i++)//每次循环选出一个点加入到生成树
  16. {
  17. int t = -1;
  18. for(int j = 1; j <= n; j++)//每个节点一次判断
  19. {
  20. if(!st[j] && (t == -1 || dt[j] < dt[t]))//如果没有在树中,且到树的距离最短,则选择该点
  21. t = j;
  22. }
  23. //如果孤立点,直返输出不能,然后退出
  24. if(dt[t] == 0x3f3f3f3f) {
  25. cout << "impossible";
  26. return;
  27. }
  28. st[t] = 1;// 选择该点
  29. res += dt[t];
  30. for(int i = 1; i <= n; i++)//更新生成树外的点到生成树的距离
  31. {
  32. if(dt[i] > g[t][i] && !st[i])//从 t 到节点 i 的距离小于原来距离,则更新。
  33. {
  34. dt[i] = g[t][i];//更新距离
  35. }
  36. }
  37. }
  38. cout << res;
  39. }
  40. int main()
  41. {
  42. memset(g, 0x3f, sizeof(g));//各个点之间的距离初始化成很大的数
  43. cin >> n >> m;//输入节点数和边数
  44. while(m --)
  45. {
  46. int a, b, w;
  47. cin >> a >> b >> w;//输出边的两个顶点和权重
  48. g[a][b] = g[b][a] = min(g[a][b],w);//存储权重
  49. //如果有重边,那么选择权重最小的那一个。
  50. }
  51. prim();
  52. return 0;
  53. }

Ⅳ、kruskai算法

题目链接:kruskal算法求最小生成树

思路:

本题使用并查集的思路,开始每个点都是独立的,当合并之后,res加上边的权重,然后将两个点的祖宗合并,然后就相当于合并一条边,cnt表示合并的边的条数,n个点,说明至少有n - 1条边,如果小于n - 1,那说明图不连通。

代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstring>
  4. using namespace std;
  5. const int N = 2e5 + 10;
  6. int p[N]; int n, m;
  7. int cnt = 0, res = 0;//记录存进去的边长
  8. struct E
  9. {
  10. int a, b, w;
  11. bool operator < (const E& rhs){//通过边长进行排序
  12. return this->w < rhs.w;
  13. }
  14. } edge[N];
  15. int find(int x)
  16. {
  17. if(p[x] != x) p[x] = find(p[x]);
  18. return p[x];
  19. }
  20. void klskr()
  21. {
  22. for(int i = 1; i<=m; i++){
  23. int pa = find(edge[i].a);
  24. int pb = find(edge[i].b);
  25. if(pa != pb){
  26. res += edge[i].w;
  27. p[pa] = pb;
  28. cnt ++;
  29. }
  30. }
  31. }
  32. int main()
  33. {
  34. cin>>n>>m;
  35. for(int i = 1; i<=n; i++) p[i] = i;
  36. for(int i = 1; i<=m; i++){
  37. int a, b, w;
  38. cin>>a>>b>>w;
  39. edge[i] = {a, b, w};
  40. }
  41. sort(edge + 1, edge + m + 1);
  42. klskr();
  43. if(cnt < n - 1)
  44. {
  45. cout<<"impossible";
  46. return 0;
  47. }
  48. cout<<res;
  49. return 0;
  50. }

Ⅴ、染色法判定二分图

题目链接:染色法判定二分图

思路:

使用邻接表存储图。dfs用来判断于结点染色与上一层结点是否相同。相同,说明不是二分图,不相同,说明是二分图。

代码:

  1. #include<iostream>
  2. using namespace std;
  3. #include<cstring>
  4. #include<algorithm>
  5. const int N = 1e5 + 10, M = 2e5 + 10;
  6. int idx, e[M], ne[M], h[N];
  7. int color[N];
  8. int n, m;
  9. void add(int a, int b)
  10. {
  11. e[idx] = b; ne[idx] = h[a]; h[a] = idx ++ ;
  12. }
  13. bool dfs(int u, int c)
  14. {
  15. color[u] = c;
  16. for(int i = h[u]; i != -1; i = ne[i])
  17. {
  18. int t = e[i];
  19. if(!color[t])
  20. {
  21. if(!dfs(t, 3 - c)) return false;
  22. }
  23. else if(color[t] && color[t] != 3 - c)
  24. {
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. int main()
  31. {
  32. memset(h, -1, sizeof(h));
  33. cin>>n>>m;
  34. while( m -- )
  35. {
  36. int a, b;
  37. cin>>a>>b;
  38. add(a, b); add(b, a);
  39. }
  40. for(int i = 1; i<=n; i++)
  41. {
  42. if(!color[i]){
  43. if(!dfs(i, 1)){
  44. puts("No");
  45. return 0;
  46. }
  47. }
  48. }
  49. puts("Yes");
  50. return 0;
  51. }

Ⅵ、匈牙利算法(二分图)

题目链接:匈牙利算法

思路

本题也是二分图类型,本题,match表示当前点是否有配对的点,如果有其它配对点或者没有配对点则可以和当前点配对,否则就不配对。

代码:

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. const int N = 510, M = 1e5 + 10;
  5. int e[M], ne[M], idx, h[N];
  6. int match[N];
  7. int n1, n2, m;
  8. bool st[N];
  9. void add(int a, int b)
  10. {
  11. e[idx] = b; ne[idx] = h[a]; h[a] = idx ++;
  12. }
  13. bool find(int x)
  14. {
  15. for(int i = h[x]; i != -1; i = ne[i])
  16. {
  17. int j = e[i];
  18. if(!st[j])
  19. {
  20. st[j] = true;
  21. if(match[j] == 0 || find(match[j])){
  22. match[j] = x;
  23. return true;
  24. }
  25. }
  26. }
  27. return false;
  28. }
  29. int main()
  30. {
  31. scanf("%d%d%d", &n1, &n2, &m);
  32. memset(h, -1, sizeof(h));
  33. while( m -- )
  34. {
  35. int a, b;
  36. cin>>a>>b;
  37. add(a, b);
  38. }
  39. int res = 0;
  40. for(int i = 1; i<=n1; i++){
  41. memset(st, false, sizeof(st));
  42. if(find(i)) res ++;
  43. }
  44. cout<<res;
  45. return 0;
  46. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/52742
推荐阅读
相关标签
  

闽ICP备14008679号