zoukankan      html  css  js  c++  java
  • CF76A.Gift [最小生成树]

    CF76A.Gift

    题意:noi2014魔法森林弱化版QwQ,最小化(max(g_i)*G + max(s_i)*S)的最小生成树


    考虑按g升序加边,用已在生成树中的边和新加入的边求当前最小生成树。

    复杂度(O(nm))

    vector真好用

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    using namespace std;
    typedef long long ll;
    const int N = 205, M = 50005;
    const ll inf = 2e18+5;
    
    inline int read() {
    	int 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*10+c-'0'; c=getchar();}
    	return x*f;
    }
    
    int n, m, G, S;
    struct edge {
    	int x, y, g, s;
    	bool operator <(const edge &r) const {
    		return g < r.g;
    	}
    } e[M];
    bool cmp_s(const edge &a, const edge &b) {
    	return a.s < b.s;
    }
    int fa[N];
    int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
    
    vector<edge> v;
    vector<edge>::iterator it;
    ll ans = inf;
    ll kruskal(int m) { //printf("kruskal %d
    ", m);
    	int mg = 0, ms = 0;
    	for(int i=1; i<=n; i++) fa[i] = i;
    	int cnt = 0;
    	for(it = v.begin(); it != v.end(); ) {
    		int x = find(it->x), y = find(it->y);
    		//printf("%d %d    %d %d
    ", e[i].x, e[i].y, x, y); cout << endl;
    		if(x == y) {
    			it = v.erase(it);
    			continue; 
    		}
    		fa[y] = x;
    		mg = max(mg, it->g);
    		ms = max(ms, it->s);
    		if(++cnt == n-1) break;
    		it++;
    	}
    	if(cnt == n-1) return (ll) mg * G + (ll) ms * S;
    	else return inf;
    }
    
    void solve() {
    	sort(e+1, e+1+m);
    	for(int i=1; i<=n-2; i++) v.push_back(e[i]);
    	sort(v.begin(), v.end(), cmp_s);
    	for(int p=n-1; p<=m; p++) { //printf("ppp %d
    ", p);
    		edge now = e[p];
    		int flag = 0;
    		for(it = v.begin(); it != v.end(); it++) {
    			if(it->s > now.s) {
    				v.insert(it, now);
    				flag = 1;
    				break;
    			}
    		}
    		if(!flag) v.insert(it, now);
    
    		ans = min(ans, kruskal(p));
    	}
    	if(ans == inf) ans = -1;
    	cout << ans;
    }
    
    int main() {
    	//freopen("in", "r", stdin);
    	cin >> n >> m >> G >> S;;
    	for(int i=1; i<=m; i++) {
    		e[i].x = read();
    		e[i].y = read();
    		e[i].g = read();
    		e[i].s = read();
    	}
    	solve();
    }
    
    
  • 相关阅读:
    ADB 多设备中指定设备的启动
    vuex 绑定表单,多个输入框的解决办法
    vue 自动触发点击事件
    js 打印dom的所有属性
    邮件分享
    table表格 td设置固定宽度
    Echarts--Y坐标标题显示不全
    ExtJS5.0 菜鸟的第一天
    字体设置
    短信验证码+倒计时
  • 原文地址:https://www.cnblogs.com/candy99/p/9408652.html
Copyright © 2011-2022 走看看