蓝桥杯之图

打印 上一主题 下一主题

主题 1790|帖子 1790|积分 5370

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
图:

对于图来说,重点在于之后的最短路径算法,这边简朴做一下相识即可






代码:

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. #include<list>
  5. #include<queue>
  6. using namespace std;
  7. class Digraph
  8. {
  9. private:
  10.         //顶点类型
  11.         struct Vertic
  12.         {
  13.                 Vertic(string data) :data_(data) {
  14.                 }
  15.                 string data_;//存储顶点信息
  16.                 list<int>adjList_;//邻接链表
  17.         };
  18.         vector<Vertic>vertics;//邻接表结构
  19. public:
  20.         void readFile(string file)//读取配置文件生成邻接表
  21.         {
  22.                 FILE* pf = fopen(file.c_str(), "r");
  23.                 if (pf == nullptr)
  24.                 {
  25.                         throw file + "not exists!";
  26.                 }
  27.                 //占用0号位置,利用emplace_back()可以利用自定义对象的构造函数
  28.                 vertics.emplace_back("");
  29.                 while (!feof(pf))
  30.                 {
  31.                         char line[1024] = { 0 };
  32.                         fgets(line, 1024, pf);
  33.                         string Line(line);
  34.                         //增加一个节点信息
  35.                         vertics.emplace_back(Line.substr(0,Line.size()-1));
  36.                         fgets(line, 1024, pf);
  37.                         char* vers = strtok(line, ",");
  38.                         while (vers != nullptr)
  39.                         {
  40.                                 int a = atoi(vers);
  41.                                 if(a>0)
  42.                                         vertics.back().adjList_.emplace_back(a);
  43.                                 vers=strtok(NULL, ",");
  44.                         }
  45.                 }
  46.                 fclose(pf);
  47.         }
  48.         //输出邻接表信息
  49.         void show()const
  50.         {
  51.                 for (int i=1;i<vertics.size();i++)
  52.                 {
  53.                         cout << vertics[i].data_ << " ";
  54.                         for (auto a : vertics[i].adjList_)
  55.                         {
  56.                                 cout << a << " ";
  57.                         }
  58.                         cout << endl;
  59.                 }
  60.         }
  61.         //图的深度优先遍历
  62.         void dfs()
  63.         {
  64.                 vector<bool>state(vertics.size(), 0);
  65.                 dfs_(1,state);
  66.                 cout << endl;
  67.         }
  68.         //图的广度优先遍历
  69.         void bfs()
  70.         {
  71.                 queue<int>que;
  72.                 vector<bool>state(vertics.size(), 0);
  73.                 que.push(1);
  74.                 state[1] = true;
  75.                 while (!que.empty())
  76.                 {
  77.                         int vertic=que.front();
  78.                         que.pop();
  79.                         cout << vertics[vertic].data_ << " ";
  80.                         for (auto a : vertics[vertic].adjList_)
  81.                         {
  82.                                 if (state[a] == false)
  83.                                 {
  84.                                         que.push(a);
  85.                                         state[a] = true;
  86.                                 }
  87.                                        
  88.                         }
  89.                 }
  90.                 cout << endl;
  91.         }
  92.         //不带权值的最短路径,类似与广度优先遍历,一层一层找肯定会比深度遍历要强
  93.         void shortPath(int start,int end)
  94.         {
  95.                 queue<int>que;
  96.                 vector<bool>state(vertics.size(), 0);
  97.                 vector<int>path(vertics.size(), 0);
  98.                 que.push(start);
  99.                 state[start] = true;
  100.                 while (!que.empty())
  101.                 {
  102.                         int vertic = que.front();
  103.                         if (vertic == end)
  104.                                 break;
  105.                         que.pop();
  106.                         //cout << vertics[vertic].data_ << " ";
  107.                         for (auto a : vertics[vertic].adjList_)
  108.                         {
  109.                                 if (state[a] == false)
  110.                                 {
  111.                                         que.push(a);
  112.                                         state[a] = true;
  113.                                         path[a] = vertic;
  114.                                 }
  115.                         }
  116.                 }
  117.                 printPath(path,end);
  118.                 cout << endl;
  119.         }
  120. private:
  121.         void dfs_(int start,vector<bool>&state)
  122.         {
  123.                 if (state[start])
  124.                 {
  125.                         return;
  126.                 }
  127.                 cout << vertics[start].data_ << " ";
  128.                 state[start] = true;
  129.                 for (auto a : vertics[start].adjList_)
  130.                 {
  131.                         dfs_(a, state);
  132.                 }
  133.         }
  134.         void printPath(vector<int>& path,int end)
  135.         {
  136.                 if (end == 0)
  137.                         return;
  138.                 printPath(path, path[end]);
  139.                 cout << vertics[end].data_ << " ";
  140.         }
  141. };
  142. int main()
  143. {
  144.         Digraph graph;
  145.         graph.readFile("jiedian.txt");
  146.         graph.show();
  147.         graph.dfs();
  148.         graph.bfs();
  149.         graph.shortPath(1, 8);
  150.         return 0;
  151. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

曹旭辉

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表