-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
114 lines (110 loc) · 3.17 KB
/
Copy pathgraph.cpp
File metadata and controls
114 lines (110 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <deque>
#include <iterator>
using namespace std;
class graph
{
private:
vector<vector<int>> node;
int node_count;
public:
//用于从输入中构建图
int build(){
cout << "首先输入节点个数,然后每行输入每个节点的邻接节点" << endl;
cin >> node_count;
string buffer;
getline(cin, buffer);
node.clear();
int count = node_count;
while (count > 0){
node.push_back(vector<int>());
getline(cin, buffer);
istringstream is(buffer);
int temp;
while (is >> temp){
node[node.size() - 1].push_back(temp);
}
count--;
}
return 0;
}
//进行转置
void transpose(){
vector<vector<int>> temp(node.size());
for (int i = 0; i < node.size(); i++){
for (int j = 0; j < node[i].size(); j++){
temp[node[i][j]].push_back(i);
}
}
node = move(temp);
}
//给定节点,输出它到每个节点的最短距离
vector<int> minDist(int src){
vector<int> ans(node_count, -1);
if (src >= node_count)
return ans;
ans[src] = 0;
vector<int> visited(node_count, 0);
visited[src] = 1;
deque<int> buffer;
for (int i = 0; i < node[src].size(); i++){
buffer.push_back(node[src][i]);
ans[node[src][i]] = 1;
}
while (!buffer.empty()){
int this_node = buffer.front();
for (int i = 0; i < node[this_node].size(); i++){
int next_node = node[this_node][i];
if (visited[next_node] == 1)
continue;
visited[next_node] = 1;
ans[next_node] = ans[this_node] + 1;
buffer.push_back(next_node);
}
buffer.pop_back();
}
return ans;
}
//拓扑排序
vector<int> sort(){
vector<int> ans;
vector<int> stack;
//统计入度
vector<int> inCount(node_count, 0);
vector<int> visited(node_count, 0);
for (int i = 0; i < node.size(); i++){
for (int j = 0; j < node[i].size(); j++){
inCount[node[i][j]]++;
}
}
for (int i = 0; i < inCount.size(); i++){
if (inCount[i] == 0)
subSort(i, stack, visited);
}
return stack;
}
private:
void subSort(int node_index, vector<int> &stack, vector<int> &visited){
if (visited[node_index] == 1)
return;
visited[node_index] = 1;
for (int i = 0; i < node[node_index].size(); i++){
subSort(node[node_index][i], stack, visited);
}
stack.push_back(node_index);
}
};
int main(){
graph graph;
graph.build();
vector<int> ans = graph.sort();
copy(ans.begin(), ans.end(), ostream_iterator<int>(cout, " "));
cout << endl;
graph.transpose();
ans = graph.sort();
copy(ans.begin(), ans.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}