• 还是畅通工程 HDU


    题目链接:https://vjudge.net/problem/HDU-1233

    思路:

    最小生成树板子。

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <queue>
     4 using namespace std;
     5 
     6 const int N = 110;
     7 const int inf = (int)1e9;
     8 int g[N][N];
     9 bool vis[N];
    10 int dis[N];
    11 struct node{
    12     int loc;
    13     int w;
    14 
    15     bool friend operator<(const node& a,const node& b){
    16         return a.w > b.w;
    17     }
    18 };
    19 priority_queue<node > que;
    20 
    21 void init(int& n){
    22 
    23     for(int i = 1; i <= n; i++){
    24 
    25         dis[i] = inf;
    26         vis[i] = 0;
    27         for(int j = 1; j <= n; j++)
    28             if(i == j) g[i][j] = 0;
    29             else g[i][j] = inf;
    30     }
    31 
    32 }
    33 
    34 int prime(int& n){
    35 
    36     while(!que.empty()) que.pop();
    37 
    38     que.push(node{1,0});
    39     dis[1] = 0;
    40 
    41     int u;
    42     while(!que.empty()){
    43         u = que.top().loc;
    44         que.pop();
    45         vis[u] = 1;
    46 
    47         for(int v = 1; v <= n; v++){
    48             if(!vis[v] && dis[v] > g[u][v]){
    49                 dis[v] = g[u][v];
    50                 que.push(node{v,dis[v]});
    51             }
    52         }
    53     }
    54 
    55     int ans = 0;
    56     for(int i = 1; i <= n; i++) ans += dis[i];
    57     return ans;
    58 }
    59 
    60 int main(){
    61 
    62     int n;
    63     while(~scanf("%d",&n) && n){
    64 
    65         int u,v,w;
    66         int len = n*(n-1)/2;
    67 
    68         init(n);
    69 
    70         for(int i = 1; i <= len; i++){
    71             scanf("%d%d%d",&u,&v,&w);
    72 
    73             if(g[u][v] > w)
    74                 g[u][v] = g[v][u] = w;
    75         }
    76 
    77         printf("%d
    ",prime(n));
    78     }
    79 
    80 
    81     return 0;
    82 }
  • 相关阅读:
    js-添加删除记录-修改
    js-添加删除记录-添加
    js-添加删除记录-删除
    dom增删改
    事件的冒泡
    div随鼠标在浏览器的窗口任意移动
    多选框全选练习
    python-day3
    python-day2
    python-day1
  • 原文地址:https://www.cnblogs.com/SSummerZzz/p/11823091.html
Copyright © 2020-2023  润新知