题意,在给出的图中,使用最小花费的边,使这个图仍然连通。

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=10005;

int head[maxn];
int n,len=0,counter;
long long ans;
struct node{
    int v,cost,u;
	//操作符的重写默认是小于等于
    bool operator < (const node& tmp) const{
		return cost< tmp.cost;

}
}gra[maxn];
struct BingCha{
//并查集
    int father[105];
    void initial(){
	//初始化不要忘记
        for(int i=1;i<=n;i++){
            father[i]=i;
        }
    }
    int getfather(int v){
        if(father[v]==v)return v;
        return father[v]=getfather(father[v]);
	//状态压缩
    }
    bool findtwo(int a,int b){
	//看是否是同根的
        return getfather(a)==getfather(b);
    }
    void unioned(int a,int b){
        int fa=father[a],fb=father[b];
        father[fa]=fb;
    }
}jihe;
void addedge(int u,int v,int cost){
    gra[len].u=u;
    gra[len].v=v;
    gra[len].cost=cost;
    len++;
}
void init(){
    len=0;
    counter=1;ans=0;
    int x;
    memset(head,-1,sizeof(head));
//memset gra?
    for(int i=1;i<=n;i++){//add the edges
        for(int j=1;j<=n;j++){
                scanf("%d",&x);
                if(i<j){
                    addedge(i,j,x);
                }
            }
        }
    }

int main(){
    while(scanf("%d",&n)!=EOF){
        init();
        sort(gra,gra+len);
		//已经有序,那么从小值开始选边添加即可,也保证了最优结果
        int idx=0;
        jihe.initial();
        while(counter<n || idx<len){
            if(!jihe.findtwo(gra[idx].u,gra[idx].v)){
			//如果不同根,就选择这条边,执行更新操作
                ans+=gra[idx].cost;
                counter++;
                jihe.unioned(gra[idx].u,gra[idx].v);
            }
            idx++;
        }
            printf("%d\n",ans);

    }
}

  2421 也是裸的mst:只要把已经建了的作为输入先处理一下就好了。

 #include <cstdio>
 #include <algorithm>
 #include <cstring>
 using namespace std;
 ;

 int n,len,counter;
 long long ans;
 struct node{
     int v,cost,u;
     bool operator < (const node& tmp) const{
         return cost< tmp.cost;

 }
 }gra[maxn*(maxn-)>>];
 struct BingCha{
     ];
     void initial(){
         ;i<=n;i++){
             father[i]=i;
         }
     }
     int getfather(int v){
         if(father[v]==v)return v;
         return father[v]=getfather(father[v]);
     }
     bool findtwo(int a,int b){
         return getfather(a)==getfather(b);
     }
     void unioned(int a,int b){
         int fa=getfather(a),fb=getfather(b);
         father[fa]=fb;
     }
 }jihe;
 void addedge(int u,int v,int cost){
     gra[len].u=u;
     gra[len].v=v;
     gra[len].cost=cost;
     len++;
 }
 int findxy(int x,int y){

     if(x>y){int z=y;y=x;x=z;}
     ,id=;
     ;i<x;i++){
         id+=j--;
     }
     y-=x;// here!!!
     id+=y;
     return id;
 }
 void init(){
     len=;
     counter=;ans=;
     jihe.initial();
     int x,q,y;
 //memset gra?
     ;i<=n;i++){//add the edges
         ;j<=n;j++){
                 scanf("%d",&x);
                 if(i<j){
                     addedge(i,j,x);
                 }
         }
     }
     scanf("%d",&q);
     ;i<=q;i++){
         scanf("%d%d",&x,&y);
         ;
         if(!jihe.findtwo(gra[idx].u,gra[idx].v)){
             counter++;
             jihe.unioned(gra[idx].u,gra[idx].v);
         }
     }
 }

 int main(){
     while(scanf("%d",&n)!=EOF){
         init();
         sort(gra,gra+len);
         ;
         while(counter<n){//|| idx<=len
             if(!jihe.findtwo(gra[idx].u,gra[idx].v)){
                 ans+=gra[idx].cost;
                 counter++;
                 jihe.unioned(gra[idx].u,gra[idx].v);
             }
             idx++;
         }
             printf("%d\n",ans);

     }
 }

Agri Net POJ1258 && Constructing Roads POJ2421的更多相关文章

  1. HDU1102&&POJ2421 Constructing Roads 2017-04-12 19:09 44人阅读 评论(0) 收藏

    Constructing Roads Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

  2. POJ2421 Constructing Roads

    Constructing Roads 这道题很水,就是一个裸的最小生成树,最不过把已经连接的节点的值再设为0. 代码: #include<cstdio> #include<cstri ...

  3. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

  4. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  5. [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  6. HDU 1102 Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. Constructing Roads (MST)

    Constructing Roads Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  8. HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  9. hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

随机推荐

  1. URL学习笔记

    不多说,先上代码,代码的注释写的已经挺详细的了 //URL:统一资源定位符,一个URL的对象,对应着互联网上的一个资源. //我们可以通过URL的对象调用其相应的方法,将此资源读取(即所谓的“下载”) ...

  2. mysql 主从同步 Last_SQL_Error

    参考文章: http://kerry.blog.51cto.com/172631/277414/ http://hancang2010.blog.163.com/blog/static/1824602 ...

  3. 一些达成共识的JavaScript编码风格约定

    如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要.与其他 ...

  4. HTML5新增结构标签

    引言 在本节中,笔者将向大家讲述三部分内容,分别介绍HTML5时代的召唤,跟HTML4的区别,以及HTML5中带来的新的结构标签. HTML5时代的召唤 HTML4与HTML5的区别 HTML5新结构 ...

  5. 代码动态创建checkbox

    根据数据库的内容动态创建Checkbox控件并显示在Panel上 dataset ds=new dataset(); CheckBox[ ] cb=new CheckBox[ds.tables[0]. ...

  6. c# 判断点是否在区域内 点在区域内 在多边形内 判断

    方法一 算法 : public int isLeft(Point P0, Point P1,Point P2)        {            int abc= ((P1.X - P0.X) ...

  7. drop column与set unused

    8i以前,如果需要删除表中的列,需要删除表然后重新建.现在,但我们需要删除一个列时,可以有以下两种方法: Logical Delete Physical Delete Logical Delete(逻 ...

  8. 滚珠菜单动效-b

    原型从网上找的,动效使用了CAAnimation和UIDynamic物理引擎.  gitHub  :https://github.com/BearRan/FlowMenuAnimation 大致步骤如 ...

  9. 解决Cisco VPN Client:Reason 442: Failed to Enable Virtual Adapter VPN连接问题

    大公司里肯定涉及不同地点的办公问题,这样VPN的使用就频繁了,今天遇到一个VPN连接问题,分享给大家,看一眼,以后不在这问题上耗费太多功夫. 在win7上连接vpn时抛出“failed to enab ...

  10. replace()替换文字

    var test = text.innerHTML; b = test.replace(/任晓强/g,"你好"); console.log(b); html: <div id ...