首页 > 学院 > 开发设计 > 正文

POJ1287 Networking(最小生成树,Kruskal,Prim)

2019-11-06 06:30:15
字体:
来源:转载
供稿:网友

题目:

Networking
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10030 Accepted: 5469

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 02 31 2 372 1 171 2 683 71 2 192 3 113 1 71 3 52 3 893 1 911 2 325 71 2 52 3 72 4 84 5 113 5 101 5 64 2 120

Sample Output

0171626

Source

Southeastern Europe 2002

[Submit]   [Go Back]   [Status]   [Discuss]

思路:

题目很水,就基本是这两种算法的模板

代码1(Kruskal):

#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <string>#include <iostream>#include <stack>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define N 1000+20#define M 10000+20#define MOD 1000000000+7#define inf 0x3f3f3f3fusing namespace std;int n,m,len;int pre[N];struct node{    int u,v,w;} map[N];bool cmp(node a,node b){    return a.w<b.w;}void init(){    for(int i=1; i<=n; i++)        pre[i]=i;}int find(int x){    if(x==pre[x])        return x;    else    {        pre[x]=find(pre[x]);        return pre[x];    }}int mix(int x,int y){    int fx=find(x);    int fy=find(y);    if(fx!=fy)    {        pre[fy]=fx;        return 1;    }    return 0;}void Kruskal(){    sort(map+1,map+m+1,cmp);    int cnt=0;    int sum=0;    for(int i=1; i<=m; i++)    {        if(mix(map[i].u,map[i].v))        {            cnt++;            sum+=map[i].w;        }    }    if(cnt==n-1)//进行n-1次就可以了    {        printf("%d/n",sum);        return;    }}int main(){    while(scanf("%d",&n)&&n)    {        scanf("%d",&m);        init();        if(m==0)        {            printf("0/n");            continue;        }        for(int i=1;i<=m;i++)            scanf("%d%d%d",&map[i].u,&map[i].v,&map[i].w);        Kruskal();    }    return 0;}代码2(Prim):

#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <string>#include <iostream>#include <stack>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define N 1000+20#define M 10000+20#define MOD 1000000000+7#define inf 0x3f3f3f3fusing namespace std;int n,m;int map[N][N],dis[N];bool vis[N];void init(){    for(int i=1; i<=n; i++)        for(int j=1; j<=n; j++)            i==j?map[i][j]=0:map[i][j]=inf;}void Prim(){    int minn;    int k;    for(int i=1; i<=n; i++)    {        dis[i]=map[1][i];        vis[i]=0;    }    vis[1]=1;//从1号点开始    int sum=0;    for(int i=1; i<=n-1; i++)    {        minn=inf;        for(int j=1; j<=n; j++)        {            if(!vis[j]&&dis[j]<minn)            {                minn=dis[j];                k=j;            }        }        vis[k]=1;        sum+=dis[k];        for(int j=1; j<=n; j++)            if(!vis[j]&&map[k][j]<dis[j])                dis[j]=map[k][j];    }    printf("%d/n",sum);}int main(){    while(scanf("%d",&n)&&n)    {        scanf("%d",&m);        if(m==0)        {            printf("0/n");            continue;        }        init();        int a,b,c;        for(int i=1; i<=m; i++)        {            scanf("%d%d%d",&a,&b,&c);            map[a][b]=map[b][a]=min(c,map[a][b]);//注意判重边        }        Prim();    }    return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表