• 啊哈 算法 中 图相关算法


     1 #include <stdio.h>
     2 #include <iostream>
     3 
     4 using namespace std;
     5 
     6 int book[101], sum, n, e[101][101];
     7 
     8 void dfs(int cur) {
     9     int i;
    10     std::cout << cur << " ";
    11     sum++;
    12     if (sum == n) return;
    13     for (i = 1; i <= n; i++) {
    14         if (e[cur][i] == 1 && book[i] == 0) {
    15             book[i] = 1;
    16             dfs(i);
    17         }
    18     }
    19     return;
    20 }
    21 
    22 
    23 
    24 int main()
    25 {
    26     int i = 0, j = 0, m = 0,a =0, b = 0;
    27     
    28     m = 5, n = 5;
    29 
    30     if (n == 0 && m == 0) {
    31         std::cin >> m >> n;
    32     }
    33 
    34     for (i = 1; i <= n; i++)
    35         for (j = 1; j <= n; j++)
    36             if (i == j) e[i][j] = 0;
    37             else e[i][j] = 99999999;
    38     //==============================        
    39     e[1][2] = e[2][1] = 1;
    40     e[1][3] = e[3][1] = 1;
    41     e[1][5] = e[5][1] = 1;
    42     e[2][4] = e[4][2] = 1;
    43     e[3][5] = e[5][3] = 1;
    44     //==============================
    45     /*for (i = 1; i <= m; i++) {
    46         std::cin >> a >> b;
    47         e[a][b] = 1;
    48         e[b][a] = 1;
    49     }*/
    50 
    51     book[1] = 1;
    52     dfs(1);
    53     
    54     system("pause");
    55 
    56     return 0;
    57 }
    DFS
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
        int i = 0, j = 0, n = 0, m = 0, a = 0, b = 0, cur = 0, book[101] = { 0 }, e[101][101];
        int que[10001], head, tail;
    
        n = 5; m = 5;
        if (n == 0 && m == 0) {
            std::cin >> n >> m;
        }
        for (i = 1; i <= n; i++)
            for (j = 1; j <= n; j++)
                if (i == j) e[i][j] = 0;
                else e[i][j] = 99999999;
    
        //=======================================
        e[1][2] = e[2][1] = 1;
        e[1][3] = e[3][1] = 1;
        e[1][5] = e[5][1] = 1;
        e[2][4] = e[4][2] = 1;
        e[3][5] = e[5][3] = 1;
        //=======================================
        /*for (i = 1; i <= m; i++) {
            std::cin >> a >> b;
            e[a][b] = e[b][a] = 1;
        }*/
        //=======================================
    
        head = 1; tail = 1;
    
        que[tail] = 1;
        tail++;
        book[1] = 1;
    
        while (head < tail) {
            cur = que[head];
            for (i = 1; i <= n; i++) {
                if (e[cur][i] == 1 && book[i] == 0) {
                    que[tail] = i;
                    tail++;
                    book[i] = 1;
                }
                if (tail > n) {
                    break;
                }
            }
            head++;
        }
    
        for (i = 1; i < tail;i++) {
            std::cout << que[i] << " ";
        }
        system("pause");
        return 0;
    }
    BFS
     1 // 333.cpp: 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 
     6 #include <stdio.h>
     7 #include <iostream>
     8 
     9 using namespace std;
    10 
    11 int min = 99999999, book[101], n, e[101][101];
    12 
    13 void dfs(int cur, int dis) {
    14     int j;
    15     if (dis > min) return;
    16     if (cur == n) {
    17         if (dis < min) min = dis;
    18         return;
    19     }
    20 
    21     for (j = 1; j <= n; j++) {
    22         if (e[cur][j] != 99999999 && book[j] == 0) {
    23             book[j] = 1;
    24             dfs(j, dis + e[cur][j]);
    25             book[j] = 0;
    26         }
    27     }
    28 
    29     return;
    30 }
    31 
    32 
    33 
    34 int main()
    35 {
    36     int i, j, m = 0, a, b, c;
    37     n = 5; m = 8;
    38     if(n == 0 && m == 0)
    39         std::cin >> n >> m;
    40 
    41     for (i = 1; i <= n; i++)
    42         for (j = 1; j <= n; j++)
    43             if (i == j) e[i][j] = 0;
    44             else e[i][j] = 99999999;
    45 
    46     //=====================================
    47     e[1][2] = 2;
    48     e[1][5] = 10;
    49     e[2][3] = 3;
    50     e[2][5] = 7;
    51     e[3][1] = 4;
    52     e[3][4] = 4;
    53     e[4][5] = 5;
    54     e[5][3] = 3;
    55 
    56     //======================================
    57     /*for (i = 1; i <= m; i++) {
    58         std::cin >> a >> b >> c;
    59         e[a][b] = c;
    60     }*/
    61     //==================================
    62     book[1] = 1;
    63     dfs(1, 0);
    64     std::cout << min << std::endl;
    65 
    66 
    67     system("pause");
    68 
    69     return 0;
    70 }
    有向DFS
    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    selenium3 + python
    selenium3 + python
    Linux 文件查找
    Linux 重定向与管道
    MySQL —— 基本查询方法
    Linux 用户和文件权限管理
    Linux bash介绍与使用
    Linux 文件操作
    Linux 文件系统简介
    Linux 帮助命令
  • 原文地址:https://www.cnblogs.com/itdef/p/9157282.html
Copyright © 2020-2023  润新知