zoukankan      html  css  js  c++  java
  • 《洛谷P2798 爆弹虐场》

    一开始还觉得很难,结果。一发过了。

    其实观察可以发现就是一个最小生成树。

    对于前k条边,我们去连最小生成树,显然这时比较的是自己想的花费。

    然后剩下的就是老师讲的花费比较,再去连最小生成树。

    // Author: levil
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    typedef pair<int,int> pii;
    const int N = 1e4+5;
    const int M = 2e4+5;
    const LL Mod = 1e9+7;
    #define rg register
    #define pi acos(-1)
    #define INF 1e9
    #define CT0 cin.tie(0),cout.tie(0)
    #define IO ios::sync_with_stdio(false)
    #define dbg(ax) cout << "now this num is " << ax << endl;
    namespace FASTIO{
        inline LL read(){
            LL x = 0,f = 1;char c = getchar();
            while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
            while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
            return x*f;
        }
        void print(int x){
            if(x < 0){x = -x;putchar('-');}
            if(x > 9) print(x/10);
            putchar(x%10+'0');
        }
    }
    using namespace FASTIO;
    void FRE(){/*freopen("data1.in","r",stdin);
    freopen("data1.out","w",stdout);*/}
    
    struct Node{int st,to,dis1,dis2;}e[M];
    bool cmp1(Node a,Node b){return a.dis1 < b.dis1;}
    bool cmp2(Node a,Node b){return a.dis2 < b.dis2;}
    int fa[N];
    int Find(int x)
    {
        return x == fa[x] ? x : fa[x] = Find(fa[x]);
    }
    int main()
    {
        int n,k,m;n = read(),k = read(),m = read();
        for(int i = 1;i <= n;++i) fa[i] = i;
        for(int i = 1;i <= m;++i) e[i].st = read(),e[i].to = read(),e[i].dis1 = read(),e[i].dis2 = read();
        sort(e+1,e+m+1,cmp1);
        int num = k,mx = -1;
        for(int i = 1;i <= m && num > 0;++i)
        {
            int x = e[i].st,y = e[i].to,dis = e[i].dis1;
            int xx = Find(x),yy = Find(y);
            if(xx != yy)
            {
                fa[xx] = yy;
                mx = max(mx,dis);
                num--;
            }
        }
        sort(e+1,e+m+1,cmp2);
        for(int i = 1;i <= m;++i)
        {
            int x = e[i].st,y = e[i].to,dis = e[i].dis2;
            int xx = Find(x),yy = Find(y);
            if(xx != yy)
            {
                fa[xx] = yy;
                mx = max(mx,dis);
            }
        }
        printf("%d
    ",mx);
        system("pause");    
    }
    View Code
  • 相关阅读:
    数据挖掘——统计学分析(五:统计量)
    数据挖掘——统计学分析(四:概率与概率分布)
    linux shell之sed
    ListView常用属性 (2012-01-12 17:20:27)
    android ListView几个比较特别的属性
    android
    android:layout_weight的真实含义
    linux下mysql安装、目录结构、配置
    mysql查看数据库和表的占用空间大小
    Android实战技巧:如何在ScrollView中嵌套ListView
  • 原文地址:https://www.cnblogs.com/zwjzwj/p/13663063.html
Copyright © 2011-2022 走看看