POJ 2421(prim)

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

这个题和poj1258是一样的,只要在1258的基础上那么几行代码,就可以A,水。

题意:还是n连通问题,和1258不同的就是这个还有几条路在之前就已经连通了的,所以不需要再去连。

 1 #include <stdio.h>
 2 #include <string.h>
 3
 4
 5 #define inf 100009
 6
 7 bool mark[1001];
 8 int a[1001][1001],dis[1001],ans,n;
 9
10 int prim()
11 {
12    for(int i=1;i<=n;i++)
13     dis[i]=inf;dis[1]=0;
14     for(int i=1;i<=n;i++){
15         int tep=inf;int k=0;
16         for(int j=1;j<=n;j++){
17             if(mark[j]&&dis[j]<tep)
18             {
19                 tep=dis[j];
20                 k=j;
21             }
22         }
23         if(tep==inf) return 0;
24         ans+=tep;
25         mark[k]=false;
26         for(int j=1;j<=n;j++)
27             if(mark[j]&&dis[j]>a[k][j])
28                 dis[j]=a[k][j];
29        }
30    return 0;
31 }
32
33 int main()
34 {
35     while(scanf("%d",&n)!=EOF)
36     {
37         int x=0,c,b;
38         memset(mark,true,sizeof(mark));
39         ans=0;
40         for(int i=1;i<=n;i++)
41             for(int j=1;j<=n;j++)
42                 scanf("%d",&a[i][j]);
43         scanf("%d",&x);
44         while(x--)
45         {
46             scanf("%d%d",&b,&c);
47             a[b][c]=0;
48             a[c][b]=0;
49         }
50         prim();
51         printf("%d\n",ans);
52     }
53 }
时间: 2024-08-04 07:48:15

POJ 2421(prim)的相关文章

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 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

POJ 2421 Constructing Roads (最小生成树)

Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2421 Appoint description:  System Crawler  (2015-05-27) Description There are N villages, which are numbered from 1 to N, and y

poj 2485(prim最小生成树)

Highways Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways

POJ 2349 Prim

Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9973 Accepted: 3302 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technolo

POJ 2031 prim

Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4400 Accepted: 2255 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are ex

POJ 2421 Constructing Roads(Kruskal算法)

题意:给出n个村庄之间的距离,再给出已经连通起来了的村庄.求把所有的村庄都连通要修路的长度的最小值. 思路:Kruskal算法 课本代码: //Kruskal算法 #include<iostream> using namespace std; int fa[120]; int get_father(int x){ return fa[x]=fa[x]==x?x:get_father(fa[x]);//判断两个节点是否属于一颗子树(并查集) } int main(){ int n; int p[

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 e