• [APIO2008]免费道路



    因为粗心WA了好多次

    快要联赛了我好像状态越来越废了==

    最小生成树好题

    题目可以转化为让你选n-1条边连通整个图

    必须选择正好k条0边

    直接暴力选择k条0边是不可行的,因为会有一些必选情况无法处理

    所以我们先把所有的1边都连上

    然后看有哪些0边是必须要连上的

    记录下这些0边

    然后这些0边是必须要选择的

    然后继续选够k条0边

    然后剩下的1边就肯定能把整张图连通起来辣

    所以剩下的直接跑个最小生成树就行了


    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    const int M = 100005 ;
    const int N = 500005 ;
    using namespace std ;
    inline int read() {
        char c = getchar() ; int x = 0 , w = 1 ;
        while(c>'9'||c<'0') { if(c=='-') w = -1 ; c = getchar() ; }
        while(c>='0' && c<='9'){x = x * 10+c-'0' ; c = getchar() ; }
        return x*w ;
    }
    
    int n , m , k , f[M] , t0 , Num , tot , imp[N] ;
    struct E { int from , to , val ; }edge[N] , Ans[N] ;
    inline bool fir1(E a , E b) { return a.val > b.val ; }
    inline bool fir0(E a , E b) { return a.val < b.val ; }
    int Find(int x) { if(f[x] != x) f[x] = Find(f[x]) ; return f[x] ; }
    int main() {
        n = read() ; m = read() ; k = read() ;
        for(int i = 1 ; i <= n ; i ++) f[i] = i ;
        for(int i = 1 ; i <= m ; i ++) { 
            edge[i].from = read() ; 
            edge[i].to = read() ; 
            edge[i].val = read() ; 
            if(edge[i].val == 0) 
                ++t0 ; 
        }
        sort(edge + 1 , edge + m + 1 , fir1) ;
        for(int i = 1 , u , v , x , y ; i <= m ; i ++) {
            u = edge[i].from , v = edge[i].to ;
            x = Find(u) , y = Find(v) ;
            if(x == y) continue ;
            f[x] = y ;
            if(edge[i].val == 0) imp[++Num] = i ;
            ++tot ;
            if(tot == n - 1) break ;
        }
        if(Num > k || tot < n - 1) { printf("no solution
    ") ; return 0 ; }
        tot = 0 ;
        for(int i = 1 ; i <= n ; i ++) f[i] = i ;
        for(int i = 1 , u , v , x , y ; i <= Num ; i ++) {
            u = edge[imp[i]].from , v = edge[imp[i]].to ;
            x = Find(u) , y = Find(v) ;
            if(x == y) continue ;
            f[x] = y ;
            Ans[++tot] = edge[imp[i]] ;
    	}
        sort(edge + 1 , edge + m + 1 , fir0) ;
        int e0 = tot , st = 1 ;
        if(tot == k) st = t0 + 1 ; 
        for(int i = st , u , v , x , y ; i <= m ; i ++) {
            u = edge[i].from , v = edge[i].to ;
            x = Find(u) , y = Find(v) ;
            if(x == y) continue ;
            if(edge[i].val == 0) ++e0 ; 
    		f[x] = y ;
            Ans[++tot] = edge[i] ;
    		if(e0 == k && i < t0) i = t0 ;
            if(tot == n - 1) break ;
    	}
        if(e0 < k) { printf("no solution
    ") ; return 0 ;}
        for(int i = 1 ; i <= tot ; i ++) printf("%d %d %d
    ",Ans[i].from , Ans[i].to , Ans[i].val) ;
        return 0 ;
    }
    
  • 相关阅读:
    SpringIOC——refresh()
    SpringIOC——scan()
    MySQL——查询性能优化
    [php-error-report]PHP Strict Standards: Only variables should be passed by reference
    [阅读]谈谈个人对“金融是否需要互联网”命题之辩
    [javascript]jsonp-function 代码段
    [javascript-snippet]使用javascript+html5实现图片的灰度处理
    [javascript]Three parts of javascript code snippet
    表格显示高亮
    关于FTP的根目录
  • 原文地址:https://www.cnblogs.com/beretty/p/9601135.html
Copyright © 2020-2023  润新知