zoukankan      html  css  js  c++  java
  • poj1251Jungle Roads

    http://poj.org/problem?id=1251

    /*poj1251Jungle Roads
    最小生成树(裸题)
    题意:给出道路(两端点和权值) 求最小费用使所有城市相连
    */

    View Code
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define MAXN 27
    int pre[MAXN];
    int fee;
    struct node
    {
    int u,v,w;
    }e[MAXN*MAXN];
    bool cmp(const node&a,const node&b)
    {
    return a.w<b.w;
    }
    int find(int x)
    {
    int r=x;
    while(pre[r]!=r)r=pre[r];
    int i=x,j;
    while(i!=r)
    {
    j=pre[i];
    pre[i]=r;
    i=j;
    }
    return r;
    }
    void join(int x,int y,int i)
    {
    int fx=find(x),fy=find(y);
    if(fx!=fy)
    {
    pre[fx]=fy;
    if(i!=-1)fee+=e[i].w;
    }
    return;
    }
    int main()
    {
    int n,m,num;
    char ch;
    while(scanf("%d",&n)==1)
    {
    if(!n)break;
    num=0,fee=0;
    for(int i=0;i<n;i++)pre[i]=i;
    for(int i=0;i<n-1;i++)
    {
    cin>>ch>>m;
    int u=ch-'A';
    for(int j=0;j<m;j++)
    {
    cin>>ch>>e[num].w;
    e[num].v=ch-'A';
    e[num].u=u;
    num++;
    }
    }
    sort(e,e+num,cmp);

    for(int i=0;i<num;i++)
    {
    join(e[i].u,e[i].v,i);
    }
    cout<<fee<<endl;
    }
    }
  • 相关阅读:
    [ Docker ] 基础安装使用及架构
    [ Docker ] 基础概念
    Nginx
    ELK
    SVN + Jenkins 构建自动部署
    Zabbix
    ELK
    ELK 部署文档
    vue.js在visual studio 2017下的安装
    vue.js是什么
  • 原文地址:https://www.cnblogs.com/sook/p/2228234.html
Copyright © 2011-2022 走看看