• 7-10 公路村村通 (30分)--最小生成树prim


    思路:先用顶点0建立最小生成树,此时生成树有0一个顶点,树到其余顶点的权则为0到各点的权,然后循环:找到lowcost[]的最小值,将其对应顶点纳入最小生成树。设为顶点a,然后判断a到其余各顶点的权是否小于lowcost[]中对应的值,如果小于,则更新lowcost[]的值(即此时最小生成树到其余顶点的最小权),然后进入下一循环。
    1.建立adjvex[ ]用于存储纳入最小生成树的顶点,建立lowcost[ ]用于存储边的权值。
    2.从顶点0开始,lowcost[i]置为arc[0][i]。
    3.初始化完成,开始进行循环。从1开始,逐步确立adjvex[i]的值,即确立在最小生成树中的顶点。
    4.找到lowcost[]中的最小值,假设最终最小值lowcost[j],则用k记录下j值。然后将adjvex[i]置为k(这里将k顶点纳入了生成树),lowcost[k]置0(即k已在生成树中,当然到树的距离为0,为0时表示以后不用再处理)。
    5.k到生成树外j顶点的权若小于lowcost[j],则更新lowcost[j]为arc[k][j]。
    6.到这里,一次循环结束,确定了adjvex[i]的值,也为下一次循环更新了lowcost[]的值(每一次新循环开始,lowcost[]的值都是前一步的最小生成树到各顶点最小权值)。
      1 #include <iostream>
      2 #include <string>
      3 #define MAX 1001
      4 #define INFINITY 65535
      5 
      6 using namespace std;
      7 
      8 struct GNode
      9 {
     10     int V, W;
     11     int G[MAX][MAX];
     12 };
     13 typedef struct GNode* gptr;
     14 gptr g = new struct GNode;
     15 int Count = 1;
     16 bool f[MAX] = { false };
     17 void dfs(int i);
     18 int main()
     19 {
     20     int mon = 0;
     21     int V, W;
     22     int adj[MAX] = { 0 };
     23     int low[MAX] = { 0 };
     24     cin >> V >> W;
     25     g->V = V; g->W = W;
     26     if (g->V-1 > g->W)
     27     {
     28         cout << "-1";return 0;
     29     }
     30     for (int i = 1; i <= g->V; i++)
     31         for (int j = 1;j <= g->V; j++)
     32             g->G[i][j] = g->G[j][i] = INFINITY;
     33     for (int i = 1; i <= g->W; i++)
     34     {
     35         int c1, c2, temp;
     36         cin >> c1 >> c2 >> temp;
     37         g->G[c1][c2] = g->G[c2][c1] = temp;
     38     }
     39     dfs(1);
     40     if (Count != g->V)
     41     {
     42         cout << "-1";
     43         return 0;
     44     }
     45     for (int i = 1;i <= g->V; i++)
     46     {
     47         low[i] = g->G[1][i];
     48     }
     49     adj[1] = 1;
     50     low[1] = 0;
     51     for (int i = 2; i <= g->V; i++)
     52     {
     53         int j = 2;int k = 0;
     54         int min = INFINITY;
     55         while (j <= g->V)
     56         {
     57             if (low[j]!=0 && low[j] < min)
     58             {
     59                 min = low[j];
     60             //    low[j] = k;
     61                 k = j;
     62             }
     63         
     64             j++;
     65         }
     66         mon += min;
     67         low[k] = 0;
     68         adj[i] = k;
     69         for (int j = 1; j <= g->V; j++)
     70         {
     71             if (low[j]!=0 && g->G[k][j] < low[j])
     72             {
     73                 low[j] = g->G[k][j];
     74 
     75             }
     76         }
     77     }
     78     
     79     cout << mon;
     80     return 0;
     81 }
     82 void dfs(int i)
     83 {
     84     for (int j = i+1; j <= g->V; j++)
     85     {
     86         
     87         if (g->G[i][j] != INFINITY)
     88         {
     89             if (!f[j])
     90             {
     91                 f[j] = true;
     92                 dfs(j);
     93                 Count++;
     94             }
     95         }
     96         if (j == g->V) return;
     97     }
     98 
     99     return;
    100 }
  • 相关阅读:
    Stanford NLP 课程笔记之计算字符串距离
    Stanford NLP 课堂笔记之正则表达式
    最长回文子串
    java面向对象
    java中数组的定义
    AtCoder Beginner Contest 100 C(思维)
    Codeforces 1000B Light It Up(思维)
    jq的链式调用.end();
    解决css中display:inline-block的兼容问题
    解决css中display:inline-block产生的空隙问题
  • 原文地址:https://www.cnblogs.com/2020R/p/12568784.html
Copyright © 2020-2023  润新知