zoukankan      html  css  js  c++  java
  • UVa 11631

      题目大意:政府为了减小开支决定关闭一些路灯,同时保证照亮的路能连接所有路口。

      又是一个MST问题,Kruskal算法,不过数据规模比较大,又Submission Error了...扔这吧...

     1 #include <cstdio>
     2 #include <vector>
     3 #include <algorithm>
     4 using namespace std;
     5 typedef pair<int, int> ii;
     6 #define MAXN 200100
     7 
     8 int p[MAXN];
     9 
    10 int find(int x)
    11 {
    12     return p[x] == x ? x : p[x]=find(p[x]);
    13 }
    14 
    15 int main()
    16 {
    17 #ifdef LOCAL
    18     freopen("in", "r", stdin);
    19 #endif
    20     int n, m;
    21     while (scanf("%d%d", &n, &m) && (n || m))
    22     {
    23         int u, v, w;
    24         int total_cost = 0;
    25         vector<pair<int, ii> > EdgeList;
    26         for (int i = 0; i < m; i++)
    27         {
    28             scanf("%d%d%d", &u, &v, &w);
    29             total_cost += w;
    30             EdgeList.push_back(make_pair(w, make_pair(u, v)));
    31         }
    32         sort(EdgeList.begin(), EdgeList.end());
    33         for (int i = 0; i < n; i++)
    34             p[i]= i;
    35         int cost = 0;
    36         for (int i = 0; i < EdgeList.size(); i++)
    37         {
    38             w = EdgeList[i].first;
    39             u = EdgeList[i].second.first;
    40             v = EdgeList[i].second.second;
    41             int pu = find(u);
    42             int pv = find(v);
    43             if (pu != pv)
    44             {
    45                 cost += w;
    46                 p[pv] = pu;
    47             }
    48         }
    49         printf("%d
    ", total_cost-cost);
    50     }
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    几种常见排序算法
    62.Android之各分辨率定义的图片规格
    MVC
    EasyUI datebox 只读和取值
    WebForm带有图片的验证码
    WebForm水印照片
    ajax完整结构
    jquery简单动画
    webform数据导出
    WebForm 发送邮箱
  • 原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3327280.html
Copyright © 2011-2022 走看看