zoukankan      html  css  js  c++  java
  • POJ 2421 Constructing Roads

                            Constructing Roads

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 26772   Accepted: 11764

    Description

    There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

    We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

    Input

    The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

    Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

    Output

    You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.

    Sample Input

    3
    0 990 692
    990 0 179
    692 179 0
    1
    1 2
    

    Sample Output

    179

    题意:n个村,然后输入每条路需要建造的花费,然后输入一个m,代表已经有m条路(a-b b-a)求路能修到每个村需要多少钱。
    最小生成树
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<ctime>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<list>
    #define mem(a,b) memset(a,b,sizeof(a))
    #define P pair<int ,int>
    using namespace std;
    const int INF=0x3f3f3f3f;
    const int maxn=100+10;
    int n,m;
    int mp[maxn][maxn],vis[maxn],dis[maxn];
    int read()
    {
        int xx=0,ff=1;char ch;ch=getchar();
        while(ch>'9'||ch<'0'){if(ch=='-') ff=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
        return xx*ff;
    }
    void init()
    {
        mem(vis,0);
        mem(dis,INF);
        mem(mp,INF);
    }
    void prim()
    {
        priority_queue<P,vector< P > ,greater< P > >q;
        int cost=0;
        for(int i=1; i<=n; i++)
            dis[i]=mp[1][i];
            q.push(P(0,1));
            while(!q.empty())
            {
                P p=q.top();
                q.pop();
                int v=p.second;
                if(vis[v]) continue;
                vis[v]=1;
                cost+=p.first;
                for(int i=1;i<=n;i++)
                {
                    if(!vis[i]&&mp[v][i]!=INF)
                    {
                        q.push(P(min(mp[v][i],dis[i]),i));
                    }
                }
            }
        printf("%d
    ",cost);
    }
    int main()
    {
        int m;
        while(~scanf("%d",&n))
        {
            init();
            for(int i=1; i<=n; i++)
                for(int j=1; j<=n; j++)
                    mp[i][j]=read();
            m=read();
            for(int i=0; i<m; i++)
            {
                int u=read();
                int v=read();
                mp[u][v]=mp[v][u]=0;
            }
            prim();
        }
    }

    先码在这里 emmmmm~

    PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

  • 相关阅读:
    安装vim
    linux输入法
    freemarker 超出字符用省略号表示
    三维数组去重
    php 相同的产品 一个背景色
    php 判断白天黑夜
    PHP-preg_replace过滤字符串代码
    checkbox 更新回显
    php socket编程入门
    命令行执行php
  • 原文地址:https://www.cnblogs.com/MengX/p/9066896.html
Copyright © 2011-2022 走看看