Constructing Roads POJ - 2421

题目链接:https://vjudge.net/problem/POJ-2421

思路:一些村庄,建一些路,使得所有村庄能相连,而且使得所有路长度之和最短。

题目说了,有些村庄之间已经建了路,说明有些路我们不需要建,那么在预处理的时候

把那些已经建过边的两个村庄的距离赋值为0,那么在跑最小生成树板子的时候就完成了

一些路已经建立的情况。

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <queue>
 4 using namespace std;
 5
 6 const int inf = (int)1e9;
 7 const int N = 110;
 8 int g[N][N];
 9 int dis[N];
10 bool vis[N];
11 int n;
12
13 struct node{
14     int loc;
15     int v;
16
17     bool friend operator<(const node& a,const node& b){
18         return a.v > b.v;
19     }
20 };
21
22 priority_queue<node > que;
23
24 int prime(){
25
26     que.push(node{1,0});
27     dis[1] = 0;
28
29     while(!que.empty()){
30         int u = que.top().loc;
31         que.pop();
32
33         vis[u] = true;
34
35         for(int v = 1; v <= n; v++){
36             if(!vis[v] && dis[v] > g[u][v]){
37                 dis[v] = g[u][v];
38                 que.push(node{v,dis[v]});
39             }
40         }
41     }
42
43     int ans = 0;
44     for(int i = 1; i <= n; i++){
45
46     //    printf("%d ",dis[i]);
47         ans += dis[i];
48     }
49
50    // printf("\n");
51
52     return ans;
53 }
54
55 int main(){
56
57     scanf("%d",&n);
58
59     for(int i = 1; i <= n; i++)
60         for(int j = 1; j <= n; j++)
61             scanf("%d",&g[i][j]);
62
63     for(int i = 1; i <= n; i++)
64         dis[i] = inf;
65
66     int m;
67     scanf("%d",&m);
68
69     int u,v;
70     for(int i = 1; i <= m; i++){
71         scanf("%d%d",&u,&v);
72         g[u][v] = g[v][u] = 0;//已经有路的村庄
73     }
74
75     printf("%d\n",prime());
76
77     return 0;
78 }

原文地址:https://www.cnblogs.com/SSummerZzz/p/11822892.html

时间: 2024-08-01 08:00:05

Constructing Roads POJ - 2421的相关文章

(最小生成树)Constructing Roads -- poj -- 2421

链接: http://poj.org/problem?id=2421 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 110; const int INF = 0xfffffff; int n, J[N][N], dist[N], vis[N]; int Prim() { i

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

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算法)

题意:给出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 Constructing Roads(kruskal)(基础)

Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20643   Accepted: 8697 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

POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 8315 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

POJ 2421 Constructing Roads(最小生成树)

题意  在n个村庄之间修路使所有村庄连通  其中有些路已经修好了  求至少还需要修多长路 还是裸的最小生成树  修好的边权值为0就行咯 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int N = 105, M = 10050; int par[N], n, m, mat[N][N]; int ans; str