POJ - 2421(最小生成树.prim)

题目:

题目链接:

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

Constructing Roads

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 27947 Accepted: 12316

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

题目思路 :

只要把邻接矩阵 已经建好的路径改成0  然后就用模板就好了

AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int road[1005][1005],vis[1005],dis[1005];
const int inf = 0x3f3f3f3f;

int main()
{
//    freopen("in.txt","r",stdin);
    int n,sum,minroad,fre,j,i,q,a,b;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            scanf("%d",&road[i][j]);
        scanf("%d",&q);
        for(int i=1;i<=q;i++)//把已经建好的路径修改为0
        {
            scanf("%d %d",&a,&b);
            road[a][b]=0;
            road[b][a]=0;
        }

        for(i=1;i<=n;i++)
            dis[i]=road[1][i];
        memset(vis,0,sizeof(vis));

        vis[1]=1;
        sum=0;
        fre=1;
        while(fre < n)
        {
            minroad = inf;
            for(j=0,i=1;i<=n;i++)
            {
                if(vis[i]==0 && dis[i]<minroad)
                {
                    minroad = dis[i];
                    j=i;
                }
            }
            sum += minroad;
            fre++;
            vis[j]=1;
            for(i=1;i<=n;i++)
                if(vis[i]==0 && road[j][i]<dis[i])
                    dis[i] = road[j][i];
        }
        printf("%d\n",sum);
    }

}

原文地址:https://www.cnblogs.com/20172674xi/p/9536193.html

时间: 2024-10-02 18:45:51

POJ - 2421(最小生成树.prim)的相关文章

POJ 1789 (最小生成树 Prim)

题目描述:给予n个长度为7的字符串,定义两字符串间的代价为同一位置不同的字符的个数,现在要联通所有的字符串求最小代价.    思路:一开始使用Krustal算法,然而因为是稠密图导致TLE,换用Prim. Krustal:(TLE) #include <cstdio> #include <algorithm> #include <iostream> #include <queue> #define N 2005 using namespace std; st

Constructing Roads POJ - 2421 最小生成树板子题

#include<iostream> #include<cstring> #include<algorithm> using namespace std; const int N=110; int p[N]; struct edge{ int a; int b; int w; }e[N*N]; int map[N][N],flag[N][N],num,n; bool cmp(edge a,edge b) { return a.w<b.w; } int find(i

poj 2421

// poj 2421 prim/*最小生成树,用Prim算法有n个城镇,已知每两个城镇的距离,其中某些城镇之间的道路已经修好,要求用最少的路径修完剩下的城镇之间的路*/ #include<stdio.h>#define InF 2000int g[110][110],bz[110],low[110];int min,ans=0,Q,n,a,b; void prim(){ int i,j,k; bz[1]=1,j=1; for (i=2;i<=n;i++) { low[i]=g[1][i

HDU 1102 &amp;&amp; POJ 2421 Constructing Roads (经典MST~Prim)

链接:http://poj.org/problem?id=2421  或   http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem 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

POJ 2485-Highways(最小生成树prim)

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22433   Accepted: 10341 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Fl

POJ 1258 Agri-Net (最小生成树+Prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39820   Accepted: 16192 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

poj 1258 Agri-Net (最小生成树 prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39499   Accepted: 16017 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

POJ 1258 Agri-Net (prim最小生成树)

最小生成树模板题 #include<bits/stdc++.h> using namespace std; int n,a; int dist[120],m[120][120]; void prim() {     bool p[1020];     for(int i=2;i<=n;i++)     {         p[i]=false;         dist[i]=m[1][i];     }     dist[1]=0,p[1]=true;     for(int i=1;

POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法

题目链接:POJ 2421 Constructing Roads 修建道路 Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19698   Accepted: 8221 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e