zoukankan      html  css  js  c++  java
  • [PA2014]Kuglarz

    [PA2014]Kuglarz

    题目大意:

    有一个长度为(n(nle2000))0/1串,你可以花(c_{i,j})的钱,询问区间([i,j])的异或和。问至少要多少元才能知道原来的序列。

    思路:

    最小生成树。

    源代码:

    #include<cstdio>
    #include<cctype>
    #include<algorithm>
    inline int getint() {
    	register char ch;
    	while(!isdigit(ch=getchar()));
    	register int x=ch^'0';
    	while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
    	return x;
    }
    typedef long long int64;
    const int N=2001,M=2001000;
    struct Edge {
    	int u,v,w;
    	bool operator < (const Edge &rhs) const {
    		return w<rhs.w;
    	}
    };
    Edge edge[M];
    class DisjointSet {
    	private:
    		int anc[N];
    		int find(const int &x) {
    			return x==anc[x]?x:anc[x]=find(anc[x]);
    		}
    	public:
    		void reset(const int &n) {
    			for(register int i=0;i<=n;i++) anc[i]=i;
    		}
    		void merge(const int &x,const int &y) {
    			anc[find(x)]=find(y);
    		}
    		bool same(const int &x,const int &y) {
    			return find(x)==find(y);
    		}
    };
    DisjointSet s; 
    int main() {
    	const int n=getint();
    	int m=0;
    	for(register int i=0;i<n;i++) {
    		for(register int j=i+1;j<=n;j++) {
    			edge[m++]=(Edge){i,j,getint()};
    		}
    	}
    	std::sort(&edge[0],&edge[m]);
    	s.reset(n);
    	int64 ans=0;
    	for(register int i=0;i<m;i++) {
    		const int &u=edge[i].u,&v=edge[i].v;
    		if(s.same(u,v)) continue;
    		s.merge(u,v);
    		ans+=edge[i].w;
    	}
    	printf("%lld
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    代码互改
    第一次个人编程作业
    第一次博客
    个人总结
    第三次个人作业--用例图设计
    第二次结对作业
    第一次结对作业
    记录浏览他人代码
    中文编程作业
    第一篇随笔
  • 原文地址:https://www.cnblogs.com/skylee03/p/10195815.html
Copyright © 2011-2022 走看看