zoukankan      html  css  js  c++  java
  • Prim && Kruskal

    Electrification Plan

    Prim

     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 const int INF = 0x3f3f3f3f;
     5 const int N = 105;
     6 int n, k;
     7 int dis[N], ct[N][N], vis[N];
     8 
     9 int Prim()
    10 {
    11     int ans = 0;
    12     memset(vis, 0, sizeof(vis));
    13     /*
    14         for(int i = 1; i <= n; i++)
    15             dis[i] = ct[1][i];
    16         vis[1] = 1;
    17     */
    18     while(1)
    19     {
    20         int v = -1;
    21         for(int i = 1; i <= n; i++)
    22         {
    23             if(!vis[i] && (v == -1||dis[v]>dis[i])) v = i;
    24         }
    25         if(v == -1) break;
    26         ans += dis[v];
    27         vis[v] = 1;
    28         for(int i = 1; i <= n; i++)
    29             if(!vis[i])
    30                 dis[i] = min(dis[i], ct[v][i]);
    31     }
    32     return ans;
    33 }
    34 int main()
    35 {
    36     ios::sync_with_stdio(false);
    37     cin.tie(0);
    38     cout.tie(0);
    39     cin >> n >> k;
    40     memset(dis, INF, sizeof(dis));
    41     int tmp;
    42     while(k--)
    43     {
    44         cin >> tmp;
    45         dis[tmp] = 0;
    46     }
    47     for(int i = 1; i <= n; i++)
    48         for(int j = 1; j <= n; j++)
    49             cin >> ct[i][j];
    50     cout << Prim() << endl;
    51     return 0;
    52 }

    Kruskal

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 using namespace std;
     5 const int N = 105;
     6 struct node
     7 {
     8     int l, r, c;
     9     bool operator < (const node & x)
    10     {
    11         return c < x.c;
    12     }
    13 }A[N*N];
    14 int pre[N];
    15 int Find(int x)
    16 {
    17     if(x == pre[x]) return x;
    18     return pre[x] = Find(pre[x]);
    19 }
    20 int main()
    21 {
    22     ios::sync_with_stdio(false);
    23     cin.tie(0);
    24     cout.tie(0);
    25     int n, m;
    26     cin >> n >> m;
    27     for(int i = 1; i <= n; i++)
    28         pre[i] = i;
    29     int t, tmp;
    30     cin >> t;
    31     m--;
    32     while(m--)
    33     {
    34         cin >> tmp;
    35         t = Find(t);
    36         tmp = Find(tmp);
    37         pre[tmp] = t;
    38     }
    39     int cnt = 0;
    40     for(int i = 1; i <= n; i++)
    41     {
    42         for(int j = 1; j <= n; j++)
    43         {
    44             cin >> tmp;
    45             if(i <= j) continue;
    46             A[cnt].l = i, A[cnt].r = j, A[cnt++].c = tmp;
    47         }
    48     }
    49     int ans = 0;
    50     sort(A, A+cnt);
    51     for(int i = 0; i < cnt; i++)
    52     {
    53         int x =A[i].l, y = A[i].r, ct = A[i].c;
    54         x = Find(x), y = Find(y);
    55         if(x == y) continue;
    56         pre[y] = x;
    57         ans += ct;
    58     }
    59     cout << ans << endl;
    60     return 0;
    61 }
  • 相关阅读:
    洛谷P2089 烤鸡
    HDU-1000 A+B Problem
    《新标准C++程序设计》4.7-4.9(C++学习笔记17)
    《新标准C++程序设计》4.6(C++学习笔记16)
    面向对象程序设计寒假作业3
    《新标准C++程序设计》4.5(C++学习笔记15)
    《新标准C++程序设计》4.4(C++学习笔记14)
    《新标准C++程序设计》4.2-4.3(C++学习笔记13)
    洛谷题解P1047 校门外的树
    [lr] 矫正白平衡
  • 原文地址:https://www.cnblogs.com/MingSD/p/8413039.html
Copyright © 2011-2022 走看看