小说版本
工具总览
#include<bits/stdc++.h>
using namespace std;
int graph[105][105],node_count;
bool is_visit[105];
void clear()
{
memset(graph,0,sizeof(graph));
node_count = 0;
memset(is_visit,0,sizeof(is_visit));
is_visit[1] = 1;
}
void get_graph_nne()
{
cin >> node_count;
for(int i = 1;i<=node_count;i++)
{
int node1,node2,edge;
cin >> node1 >> node2 >> edge;
graph[node1][node2] = edge;
graph[node2][node1] = edge;
}
}
void get_graph_matrix_all()
{
cin >> node_count;
for(int i = 1;i<=node_count;i++)
for(int j = 1;j<=node_count;j++)
{
int q;
cin >> q;
graph[i][j] = q;
}
}
void get_graph_matrix_half()
{
cin >> node_count;
for(int i = 2;i<=node_count;i++)
for(int j = 1;j<i;j++)
{
int q;
cin >> q;
graph[i][j] = q;
graph[j][i] = q;
}
}
void put_graph_matrix_all()
{
for(int i = 1;i<=node_count;i++)
{
for(int j = 1;j<=node_count;j++)
cout << graph[i][j] << " ";
cout << endl;
}
}
void put_graph_matrix_half()
{
for(int i = 1;i<=node_count;i++)
{
for(int j = 1;j<i;j++)
cout << graph[i][j] << " ";
cout << endl;
}
}
void put_graph_nne()
{
for(int i = 1;i<=node_count;i++)
{
for(int j = 1;j<=i;j++)
{
if(graph[i][j] != 0)
{
cout << i << " " << j << " " << graph[i][j] << endl;
}
}
}
}
void bfs()
{
queue<int> q;
q.push(1);
is_visit[1] = 1;
while(!q.empty())
{
int node = q.front();
q.pop();
for(int i = 1;i<=node_count;i++)
{
if(is_visit[i])continue;
if(graph[node][i] == 0)continue;
// do something more...
q.push(i);
is_visit[i] = 1;
}
}
}
void dfs(int node,int step)
{
for(int i = 1;i<=node_count;i++)
{
if(graph[node][i] == 0)continue;
if(is_visit[i])continue;
// do something more...
cout << i << endl;
is_visit[i] = 1;
dfs(i,step+1);
is_visit[i] = 0;
}
}
int main()
{
clear();
}正文
第一次见到图论,是在 Grath Editor 上所见。一见便兴趣大增,画了个头像 学了图的基本知识。
此后,乱写了一份已被判定为 CE 的代码,但是,里面却是我自己研究出来的邻接矩阵。(我也是在备考CSP时得到这个名词的)
与此同时,刚刚学完 BFS 和 DFS,试图应用其到图论上。但是,你说一维 BFS 可以干什么呢?
可以做题 没用。
至少在图论上如此。
后来学了链表,后知后觉地发现这是一种特殊的图。
#include<iostream>
#include<list>
using namespace std;
int main()
{
list<int> tu;//图
......(输入一些似乎没用的数)
for(auto i = tu.begin();i<tu.end();i++)
{
//哦
//好像写错了
cout << "第" << i << "个点是" << *i << endl;
}
}然后。。。
万能头被我整没了。
然后,我重新写了一个没有判定为 CE 的输入。输入格式是受到 Graph Editor 格式的启发。
void get_graph_nne()
{
cin >> node_count;
while(node_count--)
{
int node1,node2,edge;
cin >> node1 >> node2 >> edge;
graph[node1][node2] = edge;
graph[node2][node1] = edge;
}
}但是,这个输入是按照 节点1 - 节点2 - 权值 的顺序输入的(此后称其为 nne ,就是 Node-Node-Edge 的意思)。我们还需要用矩阵输入。
void get_graph_matrix_all()
{
cin >> node_count;
for(int i = 1;i<=node_count;i++)
for(int j = 1;j<=node_count;j++)
{
int q;
cin >> q;
graph[i][j] = q;
}
}原本还有一个半矩阵输入 get_graph_matrix_half ,但好像有 bug。
(改好了)
void get_graph_matrix_half()
{
cin >> node_count;
for(int i = 2;i<=node_count;i++)
for(int j = 1;j<i;j++)
{
int q;
cin >> q;
graph[i][j] = q;
graph[j][i] = q;
}
}输入输出很简单,但是呢,我们没有输出。原理都一样,把cin换成cout就行了。
void put_graph_matrix_all()
{
for(int i = 1;i<=node_count;i++)
{
for(int j = 1;j<=node_count;j++)
cout << graph[i][j] << " ";
cout << endl;
}
}
void put_graph_matrix_half()
{
for(int i = 1;i<=node_count;i++)
{
for(int j = 1;j<i;j++)
cout << graph[i][j] << " ";
cout << endl;
}
}
void put_graph_nne()
{
for(int i = 1;i<=node_count;i++)
{
for(int j = 1;j<=i;j++)
{
if(graph[i][j] != 0)
{
cout << i << " " << j << " " << graph[i][j] << endl;
}
}
}
}然后,我发现,用 nne 输入再输出一遍是可用自己排序的。
但是大家通过瞪眼法可用发现,很少有题目只会用到这6个函数。
然后我就写了一个bfs,一个dfs。
先不说具体怎么写的,先说图的清空问题。
(原本想把clear简写成clr,但是看到有人把color简写成clr就放弃了)
void clear()
{
memset(graph,0,sizeof(graph));
node_count = 0;
memset(is_visit,0,sizeof(is_visit));
is_visit[1] = 1;
}很简单,不是吗?