3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
3 5 HintHint Huge input, scanf is recommended.
#include#include #define N 100 int n; int map[N][N], low[N]; bool vis[N]; void Init(){ memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i ++){ low[i] = 0xfffffff; for(int j = 1; j <= n; j ++){ map[i][j] = 0xfffffff; } } } int Prim(int choose){ low[choose] = 0; vis[choose] = 1; for(int i = 1; i <= n; i ++){ low[i] = map[choose][i]; } int ans = 0; for(int j = 1; j < n; j ++){ int Min = 0xfffffff; for(int i = 1; i <= n; i ++){ if(!vis[i] && Min > low[i]){ Min = low[i]; choose = i; } } vis[choose] = 1; ans += Min; for(int i = 1; i <= n; i ++){ if(!vis[i] && low[i] > map[choose][i]){ low[i] = map[choose][i]; } } } return ans; } int main() { int start, end, len; while(scanf("%d", &n), n){ Init(); for(int i = 0; i < n * (n - 1) / 2; i ++){ scanf("%d%d%d", &start, &end, &len); if(map[start][end] > len){ map[start][end] = map[end][start] = len; } } int ans = Prim(1); printf("%d\n", ans); } return 0; }
#include#include #include #define N 100 using namespace std; struct Node{ int next; int len; }; int n; int low[N]; bool vis[N]; vector q[N]; void Init(){ memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i ++){ low[i] = 0xfffffff; q[i].clear(); } } int Prim(int choose){ low[choose] = 0; vis[choose] = 1; for(int i = 0; i < q[choose].size(); i ++){ int next = q[choose].at(i).next; int len = q[choose].at(i).len; low[next] = len; } int ans = 0; for(int j = 1; j < n; j ++){ int Min = 0xfffffff; for(int i = 1; i <= n; i ++){ if(!vis[i] && Min > low[i]){ Min = low[i]; choose = i; } } vis[choose] = 1; ans += Min; for(int i = 0; i < q[choose].size(); i ++){ int next = q[choose].at(i).next; int len = q[choose].at(i).len; if(!vis[next] && low[next] > len){ low[next] = len; } } } return ans; } int main() { Node nw; int start, end, len; while(scanf("%d", &n), n){ Init(); for(int i = 0; i < n * (n - 1) / 2; i ++){ scanf("%d%d%d", &start, &end, &len); nw.next = end; nw.len = len; q[start].push_back(nw); nw.next = start; q[end].push_back(nw); } int ans = Prim(1); printf("%d\n", ans); } return 0; }
#include#include #define N 110 using namespace std; struct Node{ int start; int end; int len; friend bool operator < (const Node& a, const Node& b){ return a.len > b.len; } }; int n; priority_queue q; int Father[N]; void Init(){ for(int i = 0; i <= n; i ++){ Father[i] = i; } } int GetFather(int cur){ return Father[cur] == cur ? cur : Father[cur] = GetFather(Father[cur]); } bool Join(int start, int end){ int root1 = GetFather(start); int root2 = GetFather(end); if(root1 == root2){ return 0; } else{ Father[root1] = root2; return 1; } } int Kruskal(){ int ans = 0; while(!q.empty()){ Node cur = q.top(); q.pop(); if(Join(cur.start, cur.end)){ ans += cur.len; } } return ans; } int main() { Node nw; int start, end, len; while(scanf("%d", &n), n){ Init(); for(int i = 0; i < n * (n - 1) / 2; i ++){ scanf("%d%d%d", &start, &end, &len); nw.start = start; nw.end = end; nw.len = len; q.push(nw); } int ans = Kruskal(); printf("%d\n", ans); } return 0; }