zoukankan      html  css  js  c++  java
  • POJ 2395 Out of Hay(MST)

    【解题思路】找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够深厚且模板有错误导致提交错误
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<queue>
     6 #include<algorithm>
     7 #define INF 1000000002
     8 #define MAXN 2002
     9 #define SIZE 20200
    10 
    11 using namespace std;
    12 
    13 int eh[MAXN], tot, dist[MAXN];
    14 bool visit[MAXN];
    15 int n, m;
    16 struct Edge{
    17     int v, u, cost, next;
    18     Edge(){}
    19     Edge(int a, int b, int c, int d){
    20         v = a, u = b, cost = c, next = d;
    21     }
    22     Edge(int a, int b){v = a, cost = b;}
    23     bool operator < (const Edge &x) const{
    24         return cost > x.cost;
    25     }
    26 };
    27 struct Edge edge[SIZE];
    28 
    29 int prim(int s)
    30 {
    31     for(int i = 1; i <= n; ++i) visit[i] = false, dist[i] = INF;
    32     dist[s] = 0;
    33     priority_queue<Edge> que;
    34     que.push(Edge(s, 0));
    35     int ans = 0;
    36     while(!que.empty())
    37     {
    38         Edge tmp = que.top();
    39         que.pop();
    40         int u = tmp.v, cost = tmp.cost;
    41         if(visit[u]) continue;
    42         if(cost > ans) ans = cost; 
    43         visit[u] = true;
    44         for(int i=eh[u]; i != -1; i = edge[i].next)
    45         {
    46             int v = edge[i].u;
    47             if(!visit[v] && dist[v] > edge[i].cost)
    48             {
    49                 dist[v] = edge[i].cost;
    50                 que.push(Edge(v, dist[v]));
    51             }
    52         }
    53     }
    54     return ans;
    55 }
    56 
    57 void addedge(int a, int b, int c)
    58 {
    59     Edge e = Edge(a, b, c, eh[a]);
    60     edge[tot] = e;
    61     eh[a] = tot++;
    62 }
    63 
    64 void init()
    65 {
    66     tot = 0;
    67     memset(eh, -1, sizeof(eh));
    68 }
    69 
    70 int main()
    71 {
    72     scanf("%d%d", &n, &m);
    73     init();
    74     for(int i = 1; i <= m; ++i)
    75     {
    76         int u, v, cost;
    77         scanf("%d%d%d", &u, &v, &cost);
    78         addedge(v, u, cost);
    79         addedge(u, v, cost); 
    80     }
    81     printf("%d
    ", prim(1));
    82     return 0;
    83 }
  • 相关阅读:
    php基本语法学习
    mac下charles使用
    python--logging模块
    CSS样式(二)
    CSS样式(一)
    HTML、CSS基础知识(四)
    HTML、CSS基础知识(三)
    HTML、CSS基础知识(二)
    HTML、CSS基础知识(一)
    python学习(三十四)第一个框架
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/3213910.html
Copyright © 2011-2022 走看看