最小生成树
Prim算法:
1 #include <iostream> 2 #include <vector> 3 #include <climits> 4 using namespace std; 5 6 int N; 7 vector<vector<int>> graph; 8 vector<int> lowcost; 9 vector<int> closest; 10 11 void solve() { 12 int u = 0; 13 int cost = 0; 14 for (int i = 0; i < N; ++i) { 15 lowcost[i] = graph[0][i]; 16 closest[i] = 0; 17 } 18 closest[0] = -1; 19 for (int i = 0; i < N; ++i) { 20 int tmp_min = INT_MAX; 21 for (int j = 0; j < N; ++j) { 22 if (lowcost[j] != 0 && tmp_min > lowcost[j]) { 23 tmp_min = lowcost[j]; 24 u = j; 25 } 26 } 27 cost += lowcost[u]; 28 lowcost[u] = 0; 29 for (int j = 0; j < N; ++j) { 30 if (lowcost[j] > graph[u][j]) { 31 lowcost[j] = graph[u][j]; 32 closest[j] = u; 33 } 34 } 35 } 36 cout << cost << endl; 37 } 38 39 int main() { 40 while (cin >> N) { 41 graph.assign(N, vector<int>(N)); 42 lowcost.resize(N); 43 closest.resize(N); 44 for (int i = 0; i < N; ++i) { 45 for (int j = 0; j < N; ++j) { 46 cin >> graph[i][j]; 47 } 48 } 49 solve(); 50 } 51 return 0; 52 }
Prim算法堆优化:
1 #include <iostream> 2 #include <vector> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 7 const int INF = 1e9; 8 9 struct edge { 10 int idx; 11 int dist; 12 edge(int _idx, int _dist) : idx(_idx), dist(_dist) {} 13 }; 14 15 struct cmp { 16 bool operator () (const edge &a, const edge &b) { return a.dist > b.dist; } 17 }; 18 19 int N, M; 20 vector<vector<edge>> graph; 21 22 void solve() { 23 int S = 1; 24 priority_queue<edge, vector<edge>, cmp> heap; 25 vector<int> lowcost(N + 1, INF); 26 vector<bool> visit(N + 1, false); 27 lowcost[S] = 0; 28 visit[S] = true; 29 int res = 0; 30 for (int i = 0; i < graph[S].size(); ++i) { 31 auto v = graph[S][i]; 32 lowcost[v.idx] = min(lowcost[v.idx], v.dist); 33 heap.push(edge(v.idx, lowcost[v.idx])); 34 } 35 while (!heap.empty()) { 36 auto u = heap.top(); 37 heap.pop(); 38 if (visit[u.idx]) continue; 39 visit[u.idx] = true; 40 res += lowcost[u.idx]; 41 lowcost[u.idx] = 0; 42 for (int i = 0; i < graph[u.idx].size(); ++i) { 43 auto v = graph[u.idx][i]; 44 if (!visit[v.idx] && lowcost[v.idx] > lowcost[u.idx] + v.dist) { 45 lowcost[v.idx] = lowcost[u.idx] + v.dist; 46 heap.push(edge(v.idx, lowcost[v.idx])); 47 } 48 } 49 } 50 cout << res << endl; 51 } 52 53 int main() { 54 while (cin >> N >> M) { 55 int u, v, len; 56 graph.resize(N + 1); 57 for (int i = 0; i < M; ++i) { 58 cin >> u >> v >> len; 59 graph[u].push_back(edge(v, len)); 60 graph[v].push_back(edge(u, len)); 61 } 62 solve(); 63 } 64 return 0; 65 }
Kruscal算法:
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5 6 struct edge { 7 int a; 8 int b; 9 int cost; 10 }; 11 12 vector<edge> graph; 13 vector<int> father; 14 int N, M; 15 16 int findFather(int i) { 17 while (i != father[i]) i = father[i]; 18 return i; 19 } 20 21 void solve() { 22 sort(graph.begin(), graph.end(), 23 [](const edge &a, const edge &b) { return a.cost < b.cost; }); 24 for (int i = 1; i <= N; ++i) father[i] = i; 25 int cost = 0; 26 for (int i = 0; i < M; ++i) { 27 int fa = findFather(graph[i].a); 28 int fb = findFather(graph[i].b); 29 if (findFather(fa) != findFather(fb)) { 30 cost += graph[i].cost; 31 if (fa < fb) 32 father[fb] = fa; 33 else 34 father[fa] = fb; 35 } 36 } 37 cout << cost << endl; 38 } 39 40 int main() { 41 while (cin >> N >> M) { 42 father.resize(N + 1); 43 graph.resize(M); 44 for (int i = 0; i < M; ++i) { 45 cin >> graph[i].a >> graph[i].b >> graph[i].cost; 46 } 47 solve(); 48 } 49 return 0; 50 }

浙公网安备 33010602011771号