• POJ 2395 Out of Hay


    http://poj.org/problem?id=2395

    裸最小生成树 输出树中最大cost的边值

    直接prim

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <queue>
     5 #include <algorithm>
     6 #define READ() freopen("in.txt", "r", stdin);
     7 #define MAXV 2007
     8 #define MAXE 20007
     9 #define INF 0x3f3f3f3f3f3f3f3f
    10 
    11 using namespace std;
    12 int N, M;
    13 typedef long long ll;
    14 typedef pair<ll, int> P;
    15 
    16 struct Edge
    17 {
    18     int to, next;
    19     ll cost;
    20     Edge() {}
    21     Edge(int to, ll cost, int next) : to(to), cost(cost), next(next) {}
    22 }edge[MAXE];
    23 
    24 int head[MAXV];
    25 int num = 0;
    26 void Add(int from, int to, ll cost)
    27 {
    28     edge[num] = Edge(to, cost, head[from]);
    29     head[from] = num++;
    30 }
    31 
    32 int prim()
    33 {
    34     ll dist[MAXV];
    35     ll rec[MAXV], r = 0;
    36     bool use[MAXV];
    37     priority_queue<P, vector<P>, greater<P> > que; // 从小到大排啊
    38     fill(dist, dist+MAXV, INF);
    39     fill(use, use+MAXV, false);
    40     dist[1] = 0;
    41     que.push(P(dist[1], 1));
    42     while (!que.empty())
    43     {
    44         P p = que.top();
    45         que.pop();
    46         ll d = p.first;
    47         int v = p.second;
    48         if (!use[v]) rec[r++] = dist[v];
    49         use[v] = true;
    50         if (dist[v] < d) continue;
    51         int t = head[v];
    52         while (t != -1)
    53         {
    54             Edge e = edge[t];
    55             if (!use[e.to] && dist[e.to] > e.cost)
    56             {
    57                 dist[e.to] = e.cost;
    58                 que.push(P(dist[e.to], e.to));
    59             }
    60             t = e.next;
    61         }
    62     }
    63     sort(rec, rec+r);
    64     return rec[r-1];
    65 }
    66 
    67 
    68 
    69 int main()
    70 {
    71     READ()
    72     scanf("%d%d", &N, &M);
    73     memset(edge, -1, sizeof(edge));
    74     memset(head, -1, sizeof(head));
    75     for (int i = 0; i < M; i++)
    76     {
    77         int from, to;
    78         ll cost;
    79         scanf("%d%d", &from, &to);
    80         scanf("%lld", &cost);
    81         Add(from, to, cost);
    82         Add(to, from, cost);
    83     }
    84     ll ans = prim();
    85     cout << ans << endl;
    86 }
  • 相关阅读:
    luoguP5024 保卫王国 动态dp
    luoguP4571 [JSOI2009]瓶子和燃料 裴蜀定理
    luoguP3235 [HNOI2014]江南乐 数论分块 + 博弈论
    luoguP4101 [HEOI2014]人人尽说江南好 结论
    hdu 3032 NIm or not Nim? Multi SG
    luoguP4279 [SHOI2008]小约翰的游戏 Anti-SG 博弈论
    luoguP3480 [POI2009]KAM-Pebbles 阶梯Nim
    Educational Codeforces Round 65 (Div. 2)
    [PKUSC2018]主斗地(搜索+贪心)
    Codeforces Round #557 (Div. 1)
  • 原文地址:https://www.cnblogs.com/oscar-cnblogs/p/6427032.html
Copyright © 2020-2023  润新知