• Buy or Build UVA


    题意:
    大概意思是有 n 个点,现在有 q 个方案 ,第 i 个方案耗费为 ci ,使 Ni 个点联通 ,当然也可以直接使两点联通 ,现求最小生成树的代价。
    两点直接联通的代价是欧几里得距离的平方;
     
    由于0<=q<=8,所以我们考虑二进制枚举;
    该位为1表示选择该方案,然后每次求一遍cost ,最后取 min 即可;
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<bitset>
    #include<ctime>
    #include<deque>
    #include<stack>
    #include<functional>
    #include<sstream>
    //#include<cctype>
    //#pragma GCC optimize("O3")
    using namespace std;
    #define maxn 1000005
    #define inf 0x3f3f3f3f
    #define INF 9999999999
    #define rdint(x) scanf("%d",&x)
    #define rdllt(x) scanf("%lld",&x)
    #define rdult(x) scanf("%lu",&x)
    #define rdlf(x) scanf("%lf",&x)
    #define rdstr(x) scanf("%s",x)
    typedef long long  ll;
    typedef unsigned long long ull;
    typedef unsigned int U;
    #define ms(x) memset((x),0,sizeof(x))
    const long long int mod = 1e9 + 7;
    #define Mod 1000000000
    #define sq(x) (x)*(x)
    #define eps 1e-3
    typedef pair<int, int> pii;
    #define pi acos(-1.0)
    const int N = 1005;
    #define REP(i,n) for(int i=0;i<(n);i++)
    typedef pair<int, int> pii;
    inline ll rd() {
    	ll x = 0;
    	char c = getchar();
    	bool f = false;
    	while (!isdigit(c)) {
    		if (c == '-') f = true;
    		c = getchar();
    	}
    	while (isdigit(c)) {
    		x = (x << 1) + (x << 3) + (c ^ 48);
    		c = getchar();
    	}
    	return f ? -x : x;
    }
    
    ll gcd(ll a, ll b) {
    	return b == 0 ? a : gcd(b, a%b);
    }
    ll sqr(ll x) { return x * x; }
    
    /*ll ans;
    ll exgcd(ll a, ll b, ll &x, ll &y) {
    	if (!b) {
    		x = 1; y = 0; return a;
    	}
    	ans = exgcd(b, a%b, x, y);
    	ll t = x; x = y; y = t - a / b * y;
    	return ans;
    }
    */
    
    
    
    ll qpow(ll a, ll b, ll c) {
    	ll ans = 1;
    	a = a % c;
    	while (b) {
    		if (b % 2)ans = ans * a%c;
    		b /= 2; a = a * a%c;
    	}
    	return ans;
    }
    int T;
    int n, q;
    int cost[maxn];
    
    int fa[maxn];
    int cnt;
    vector<int>vc[maxn];
    
    struct point {
    	int x, y;
    }pint[maxn];
    
    struct node {
    	int x, y;
    	int w;
    }edge[maxn];
    
    bool cmp(node a, node b) {
    	return a.w < b.w;
    }
    
    int dis(int a, int b, int x, int y) {
    	return ((a - x)*(a - x) + (b - y)*(b - y));
    }
    
    void init(int n) {
    	for (int i = 0; i <= n; i++)fa[i] = i;
    }
    
    int findfa(int x) {
    	if (x == fa[x])return x;
    	return fa[x] = findfa(fa[x]);
    }
    
    void Union(int p, int q) {
    	if (findfa(q) != findfa(p)) {
    		fa[findfa(p)] = findfa(q);
    	}
    }
    
    int ans;
    
    int kruskal() {
    	int res = 0;
    	int ct = 0;
    	for (int i = 0; i < cnt; i++) {
    		int u = edge[i].x; int v = edge[i].y;
    		if (findfa(u) != findfa(v)) {
    			Union(u, v); res += edge[i].w;
    			ct++;
    			if (ct == n - 1)break;
    		}
    	}
    	return res;
    }
    
    
    void sol() {
    	init(n);
    	ans = kruskal();
    	
    	for (int i = 0; i < (1 << q); i++) {
    		init(n);int cst = 0;
    		for (int j = 0; j < q; j++) {
    			if (((i >> j) & 1)==0)continue;
    			cst += cost[j];
    			for (int k = 1; k < vc[j].size(); k++) {
    				Union(vc[j][k], vc[j][0]);
    			}
    		}
    		ans = min(ans, cst + kruskal());
    	}
    	printf("%d
    ", ans);
    }
    
    int main()
    {
    	//ios::sync_with_stdio(0);
    	rdint(T); int kase = 1;
    	while (T--) {
    		if (kase > 1)printf("
    ");
    		kase++;
    		rdint(n); rdint(q); cnt = 0;
    		for (int i = 0; i < 10; i++)vc[i].clear();
    		for (int i = 0; i < q; i++) {
    			int tmp; rdint(tmp); rdint(cost[i]);
    			while (tmp--) {
    				int x; rdint(x); vc[i].push_back(x);
    			}
    		}
    		
    		for (int i = 1; i <= n; i++) {
    			rdint(pint[i].x); rdint(pint[i].y);
    		}
    		for (int i = 1; i <= n; i++) {
    			for (int j = i + 1; j <= n; j++) {
    				edge[cnt].x = i; edge[cnt].y = j; edge[cnt].w = dis(pint[i].x, pint[i].y, pint[j].x, pint[j].y); cnt++;
    			}
    		}
    		sort(edge, edge + cnt, cmp);
    		sol();
    		
    	}
    
        return 0;
    }
    
    EPFL - Fighting
  • 相关阅读:
    Java 8与Runtime.getRuntime().availableProcessors()
    nginx配置
    周末完成工作小结
    CentOS 8 安装MySQL 8.0
    centOS8网络获取不了
    IUAP平台新增菜单存储过程
    centOS8安装Docker
    Mybatis里用到的设计模式
    2020,回顾过往,展望未来
    使用 Apache SSI(Server Side Includes) 制作多语言版静态网页
  • 原文地址:https://www.cnblogs.com/zxyqzy/p/9968100.html
Copyright © 2020-2023  润新知