Constructing Roads (Prim+heap)

5人阅读 评论(0) 收藏 举报

                                                Constructing Roads   POJ - 2421
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

最小生成树水题 这里使用下prim+heap优化。之前就写过这些模板题,今天用优先队列优化一下。

#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\n",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();
    }
}

查看评论

HDOJ 1102 Constructing Roads(最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...
  • zwj1452267376
  • zwj1452267376
  • 2015-08-11 21:07:43
  • 659

POJ-2421-Constructing Roads(最小生成树 普利姆)

D - Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u...
  • qq_32680617
  • qq_32680617
  • 2016-05-21 19:43:15
  • 455

HDOJ--1102--Constructing Roads(包含题意)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...
  • gui951753
  • gui951753
  • 2015-08-11 20:30:22
  • 331

HDOJ 1102 Constructing Roads(最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...
  • cs_zlg
  • cs_zlg
  • 2012-04-05 17:49:50
  • 2303

poj2421Constructing Roads(最小生成树)

思路:一些已经连好的边添加一条权值为0的路,然后就是上模板了,又SB忘记init() #include #include #include #include #include #i...
  • qq_21057881
  • qq_21057881
  • 2016-01-31 23:16:29
  • 420

HDU 1102 Constructing Roads (裸的并查集)

题目意思:有n个村庄,编号1-n,以矩阵的形式给出任意两个村庄之间的距离,然后告诉已经有q个村庄已经修好了路,问现在要打算使所有村庄都联通需要修路的最小长度。 思路就是构造一棵最小生成树,所以将距离排...
  • IAccepted
  • IAccepted
  • 2014-06-07 19:55:30
  • 1711

Constructing Roads(1102)hdu

Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)...
  • dqsyguoguo
  • dqsyguoguo
  • 2014-11-04 20:38:59
  • 413

HUT F - Constructing Roads

继续最短路模板题额。。 只是多了一个条件就是有些路是已经修好的,那么只要把修好的路之间的距离设为0,再Dijkstra一遍就出答案~                              ...
  • sky_fighting
  • sky_fighting
  • 2013-04-14 21:58:28
  • 594

POJ2421 Constructing Roads(最小生成树)

题意: 村庄之间连通,要求输出最小权值和,注意有些村庄已经连通 要点: Prim算法稍微变一下,因为是求权值和,已经连通的之间权值改为0即可,反正为0后面算的时候肯定是最小值,会加入集合的,然后...
  • SeasonJoe
  • SeasonJoe
  • 2016-04-03 15:02:03
  • 155

hdu 1102 Constructing Roads 最小生成树prim模板题

题目链接 题意:有n个村庄,给出n*n的矩阵表示村庄之间的距离。已经建造了Q条路,问建造的最短距离使所有的村庄连通。 最小生成树模板题。 #include #include #include ...
  • zchahaha
  • zchahaha
  • 2016-04-15 19:19:36
  • 586
    个人资料
    持之以恒
    等级:
    访问量: 1108
    积分: 347
    排名: 18万+
    文章存档
    最新评论