您的位置:首页 > 其它

POJ 2421 Constructing Roads

2015-08-31 20:22 357 查看
题目链接:http://poj.org/problem?id=2421

Constructing Roads

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 21105Accepted: 8916
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个村庄,已知每个村庄彼此的距离,Q个询问,输入a,b,表示a到b的马路已经修好,求连到所有村庄的马路最小长度是多少

思路一:

prim最小生成树求,唯一的地方就是a到b的马路修完,即处理成距离为0;

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 105
#define INF 0x3f3f3f3f
int mat

,vis
,low
;
int n,m;
int prim()
{
    int i,j,ans=0,pos;
    memset(vis,0,sizeof(vis));
    pos=1;
    vis[1]=1;
    for(i=1; i<=n; i++)
        low[i]=mat[pos][i];
    for(i=1; i<n; i++)
    {
        int MIN=INF;
        for(j=1; j<=n; j++)
            if(!vis[j] && MIN>low[j])
            {
                pos=j,MIN=low[j];
            }
        if(MIN==INF)break;
        ans+=MIN;
        vis[pos]=1;
        for(j=1; j<=n; j++)
        {
            if(!vis[j] && low[j]>mat[pos][j])
            {
                low[j]=mat[pos][j];
            }
        }
    }
    return ans;
}
int main()
{
    int ans,i,j,a,b;
    while(~scanf("%d",&n))
    {
        memset(mat,INF,sizeof(mat));
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
            {
                int u;
                scanf("%d",&u);
                mat[i][j]=(i==j?0:u);
            }
        scanf("%d",&m);
        for(i=1; i<=m; i++)
        {
            scanf("%d%d",&a,&b);
            mat[a]=mat[b][a]=0;
        }
        printf("%d\n",prim());
    }
    return 0;
}


[b]思路二:


Kruskal最小生成树求,点权边注意一下即可;

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 105
#define INF 0x3f3f3f3f
int n,m,k;
int pre
,mat

;
struct pp
{
    int u,v,w;
} edge[N*N];

int cmp(pp a,pp b)
{
    return a.w<b.w;
}
int find(int x)
{
    return pre[x]==x?x:find(pre[x]);
}
int Kruskal()
{
    int i,j,ans=0;
    for(i=1; i<=n; i++)
        pre[i]=i;
    sort(edge+1,edge+k+1,cmp);
    for(i=1; i<=k; i++)
    {
        int fx=find(edge[i].u);
        int fy=find(edge[i].v);
        if(fx!=fy)
        {
            pre[fx]=fy;
            ans+=edge[i].w;
        }
    }
    return ans;
}
int main()
{
    int ans,i,j,a,b;
    while(~scanf("%d",&n))
    {
        memset(mat,INF,sizeof(mat));
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
            {
                int u;
                scanf("%d",&u);
                mat[i][j]=(i==j?0:u);
            }
        scanf("%d",&m);
        for(i=1; i<=m; i++)
        {
            scanf("%d%d",&a,&b);
            mat[a][b]=mat[b][a]=0;
        }
        k=1;
        for(i=1; i<n; i++)
            for(j=i+1; j<=n; j++)
            {
                edge[k].u=i;
                edge[k].v=j;
                edge[k].w=mat[i][j];
                k++;
            }
        printf("%d\n",Kruskal());
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: