标签:参考 out 复杂 mini const name init eof index
参考博客:https://blog.csdn.net/lqcsp/article/details/14118871
复杂度:O(n*n)
#include <iostream> #include <string.h> using namespace std; const int MAXN = 2010; const int INF = 1 << 30; int map[MAXN][MAXN]; int N, M; int lowcost[MAXN]; void init() { for(size_t i = 0; i <= N; ++i) for(size_t j = 0; j <= N; ++j) map[i][j] = INF; } int prime() { for(size_t i = 1; i <= N; ++i) lowcost[i] = map[1][i]; int min; bool visited[N + 1];// index begin from 1 not 0 int ans = -1; memset(visited, false, sizeof(visited)); lowcost[1] = 0; visited[1] = true; for(size_t i = 1; i < N; ++i)//loop N - 1 times { min = INF; int k; for(size_t j = 1; j <= N; ++j)// find the minimun edge between two edge set { if(!visited[j] && min > lowcost[j]) { min = lowcost[j]; k = j; } } visited[k] = true; ans = ans > min ? ans : min; for(size_t j = 1; j <= N; ++j)// update the array of lowcost { if(!visited[j] && lowcost[j] > map[k][j]) lowcost[j] = map[k][j]; } } return ans; } int main() { while(cin >> N >> M) { init(); int x, y, c; for(size_t i = 0; i < M; ++i) { cin >> x >> y >> c; if(map[x][y] > c) map[x][y] = c; if(map[y][x] > c) map[y][x] = c; } cout << prime() << endl; } }
标签:参考 out 复杂 mini const name init eof index
原文地址:https://www.cnblogs.com/carcar/p/9104532.html