zoukankan      html  css  js  c++  java
  • POJ1287(最小生成树入门题)

    Networking
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 7753   Accepted: 4247

    Description

    You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area. 
    Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

    Input

    The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line. 
    The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i. 

    Output

    For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

    Sample Input

    1 0
    
    2 3
    1 2 37
    2 1 17
    1 2 68
    
    3 7
    1 2 19
    2 3 11
    3 1 7
    1 3 5
    2 3 89
    3 1 91
    1 2 32
    
    5 7
    1 2 5
    2 3 7
    2 4 8
    4 5 11
    3 5 10
    1 5 6
    4 2 12
    
    0

    Sample Output

    0
    17
    16
    26
    赤裸裸的最小生成树问题,下面用三种方式分别实现。
    /*
        Kruskal 1287    Accepted    420K    16MS    G++
    */ 
    #include"cstdio"
    #include"algorithm"
    using namespace std;
    const int MAXN=1000005;
    struct Edge{
        int u,v,cost;
    }es[MAXN];
    bool comp(const Edge &a,const Edge &b)
    {
        return a.cost < b.cost;
    }
    
    int par[MAXN];
    int rnk[MAXN];
    void init(int n)
    {
        for(int i=0;i<=n;i++)
        {
            par[i]=i;
        }    
    }
    
    int fnd(int x)
    {
        if(par[x]==x)
        {
            return x;
        }
        return par[x]=fnd(par[x]);    
    }
    
    void unite(int u,int v)
    {
        int a=fnd(u);
        int b=fnd(v);
        if(a==b)    return ;
        
        if(rnk[a]<rnk[b])
        {
            par[a]=b;
        }
        else{
            par[b]=a;
            if(rnk[a]==rnk[b])    rnk[a]++;
        }
    }
    
    bool same(int u,int v)
    {
        return fnd(u)==fnd(v);
    }
    
    int main()
    {
        int V,E;
        while(scanf("%d",&V)!=EOF&&V)
        {
            scanf("%d",&E);
            for(int i=0;i<E;i++)
            {
                scanf("%d%d%d",&es[i].u,&es[i].v,&es[i].cost);    
            }
            sort(es,es+E,comp);
            init(V);
            int ans=0;
            int cnt=0;
            for(int i=0;i<E;i++)
            {
                if(!same(es[i].u,es[i].v))
                {
                    unite(es[i].u,es[i].v);
                    ans+=es[i].cost;
                    cnt++;
                }
                if(cnt==V-1)    break;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    /*
        Prim     1287    Accepted    408K    16MS    G++
    */ 
    #include"cstdio"
    using namespace std;
    const int MAXN=105;
    const int INF=0x3fffffff;
    int mp[MAXN][MAXN];
    int V,E;
    inline int min(int a,int b)
    {
        return a>b?b:a;
    }
    int d[MAXN];
    int vis[MAXN];
    int prim(int s)
    {
        int ans=0;
        for(int i=1;i<=V;i++)
        {
            d[i]=mp[s][i];
            vis[i]=0;
        }
        d[s]=0,vis[s]=1;
        for(int i=1;i<=V-1;i++)
        {
            int mincost,k;
            mincost=INF;
            
            for(int i=1;i<=V;i++)
            {
                if(!vis[i]&&mincost>d[i])
                {
                    mincost=d[i];
                    k=i;
                }
            }
            vis[k]=1;
            ans+=mincost;
            for(int i=1;i<=V;i++)
            {
                if(!vis[i]&&d[i]>mp[k][i])
                {
                    d[i]=mp[k][i];
                }
            }
        }
        return ans;
    }
    
    int main()
    {    
        while(scanf("%d",&V)&&V)
        {
            for(int i=1;i<=V;i++)
                for(int j=1;j<=V;j++)
                    if(i==j)    mp[i][j]=0;
                    else mp[i][j]=INF;
            scanf("%d",&E);
            for(int i=0;i<E;i++)
            {
                int u,v,co;
                scanf("%d%d%d",&u,&v,&co);
                mp[u][v]=mp[v][u]=min(mp[u][v],co);
            }
            printf("%d
    ",prim(1));        
        }
    
        return 0;
    }
    /*
        堆优化prim   1287    Accepted    604K    16MS    G++
    */ 
    #include"cstdio"
    #include"cstring"
    #include"queue"
    using namespace std;
    const int MAXN=10005;
    const int INF=0x3fffffff;
    struct Edge{
        int to,cost,next;
    }es[MAXN];
    struct Node{
        int d,u;
        Node(int cd,int cu):d(cd),u(cu){ }
        bool operator<(const Node& a) const
        {
            return d > a.d;
        }
    };
    int V,E;
    int head[105];
    int cnt;
    void add_edge(int u,int v,int co)
    {
        es[cnt].to=v;
        es[cnt].cost=co;
        es[cnt].next=head[u];
        head[u]=cnt;
        cnt++;
    }
    int d[105];
    int vis[105];
    int prim(int s)
    {
        int ans=0;
        for(int i=1;i<=V;i++)
        {
            vis[i]=0;
            d[i]=INF;
        }
        d[s]=0;
        priority_queue<Node> que;
        que.push(Node(0,s));
        while(!que.empty())
        {
            Node no=que.top();que.pop();
            int v=no.u;
            if(vis[v])    continue;
            ans+=no.d;
            vis[v]=1;
            for(int i=head[v];i!=-1;i=es[i].next)
            {
                Edge e=es[i];
                if(d[e.to]>e.cost)
                {
                    d[e.to]=e.cost;
                    que.push(Node(d[e.to],e.to));
                }
            }
        }
        return ans;
    }
    int main()
    {    
        while(scanf("%d",&V)&&V)
        {
            cnt=0;
            memset(head,-1,sizeof(head));
            scanf("%d",&E);    
            for(int i=0;i<E;i++)
            {
                int u,v,co;
                scanf("%d%d%d",&u,&v,&co);
                add_edge(u,v,co);
                add_edge(v,u,co);
            }
            
            int ans=prim(1);
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    restful架构风格设计准则(四)资源表示和资源访问
    洛谷P2178 [NOI2015]品酒大会(后缀自动机 线段树)
    HDU 6138 Fleet of the Eternal Throne(后缀自动机)
    BZOJ1278: 向量vector(计算几何 随机化乱搞)
    BZOJ2564: 集合的面积(闵可夫斯基和 凸包)
    POJ 1113 Wall(思维 计算几何 数学)
    POJ 3304 Segments(直线与线段相交)
    洛谷P1742 最小圆覆盖(计算几何)
    洛谷P4555 [国家集训队]最长双回文串(manacher 线段树)
    洛谷P3193 [HNOI2008]GT考试(dp 矩阵乘法)
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5155688.html
Copyright © 2011-2022 走看看