POJ 1258 Agri-Net (Prim&Kruskal)

來源:互聯網
上載者:User

標籤:網路   out   eof   from   產生樹   oid   關係   using   margin   

題意:FJ想串連光纖在各個農場以便網路普及,現給出一些串連關係(給出鄰接矩陣),從中選出部分邊,使得整個圖連通。求邊的最小總花費。

思路:裸的最小產生樹,本題為稠密圖,Prim演算法求最小產生樹更優,複雜度O(n^2)

prim:

#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;int mat[110][110];bool vis[110];int d[110];int Prim(int n) {int ans = 0;int p = 0;vis[0] = 1;for (int j = 1; j < n; ++j) {d[j] = mat[p][j];}for (int i = 1; i < n; ++i) {p = -1;for (int j = 1; j < n; ++j) {if (vis[j]) continue;if (p == -1 || d[j] < d[p]) {p = j;}}ans += d[p];vis[p] = 1;for (int j = 1; j < n; ++j) {if (vis[j]) continue;d[j] = min(d[j], mat[p][j]);}}return ans;}int main() {int n, i, j;while (scanf("%d", &n) != EOF) {memset(vis, 0, sizeof(vis));for (i = 0; i < n; ++i) {for (j = 0; j < n; ++j) {scanf("%d", &mat[i][j]);}}int ans = Prim(n);printf("%d\n", ans);}return 0;}

kruskal:

#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;int N; // 節點數量struct edge {int from, to, dist;bool operator<(const edge &b) const {return dist < b.dist;}} es[10006];int par[105];void init() {for (int i = 1; i <= N; ++i) par[i] = i;}int find(int x) {return x == par[x] ? x : par[x] = find(par[x]);}void unite(int x, int y) {x = find(x);y = find(y);if (x != y) par[x] = y;}int kruskal() {int res = 0;init();int E = N*N;sort(es + 1, es + 1 + E);for (int i = 1; i <= E; ++i) {edge e = es[i];//printf("u:%d v:%d d:%d\n", e.from, e.to, e.dist);if (find(e.from) != find(e.to)) {unite(e.from, e.to);res += e.dist;}}return res;}void solve() {cout << kruskal() << endl;}int main(){while (cin >> N) {int d;int id;for (int u = 1; u <= N; ++u) for (int v = 1; v <= N; ++v) {cin >> d;id = (u - 1)*N + v;es[id].from = u;es[id].to = v;es[id].dist = d;}solve();}return 0;}

POJ 1258 Agri-Net (Prim&Kruskal)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.