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

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

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

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

一些路已经建立的情况。

 #include <stdio.h>
#include <iostream>
#include <queue>
using namespace std; const int inf = (int)1e9;
const int N = ;
int g[N][N];
int dis[N];
bool vis[N];
int n; struct node{
int loc;
int v; bool friend operator<(const node& a,const node& b){
return a.v > b.v;
}
}; priority_queue<node > que; int prime(){ que.push(node{,});
dis[] = ; while(!que.empty()){
int u = que.top().loc;
que.pop(); vis[u] = true; for(int v = ; v <= n; v++){
if(!vis[v] && dis[v] > g[u][v]){
dis[v] = g[u][v];
que.push(node{v,dis[v]});
}
}
} int ans = ;
for(int i = ; i <= n; i++){ // printf("%d ",dis[i]);
ans += dis[i];
} // printf("\n"); return ans;
} int main(){ scanf("%d",&n); for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
scanf("%d",&g[i][j]); for(int i = ; i <= n; i++)
dis[i] = inf; int m;
scanf("%d",&m); int u,v;
for(int i = ; i <= m; i++){
scanf("%d%d",&u,&v);
g[u][v] = g[v][u] = ;//已经有路的村庄
} printf("%d\n",prime()); return ;
}

Constructing Roads POJ - 2421的更多相关文章

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

    链接: http://poj.org/problem?id=2421 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2113 ...

  2. Constructing Roads POJ - 2421 (最小生成树)

    思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将    所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...

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

    #include<iostream> #include<cstring> #include<algorithm> using namespace std; ; in ...

  4. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  5. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  6. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

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

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

  8. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

  9. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

随机推荐

  1. react相关小技巧

    一.我们在项目中切换路由的时候可能会遇到 Warning: setState(...): Can only update a mounted or mounting component. This u ...

  2. JavaScript计算日期前一天和后一天

    1.页面排版 <button onclick="before()">上一天</button> <button onclick="after( ...

  3. LeetCode 61:旋转链表 Rotate List

    ​给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...

  4. python生成多维数组方法总结(多维创建有问题的情况)

    1.一维 list1=[]* #[,,,,] list2=np.arange() #[,,,,] 2.二维(注意) list2=[[]*]* 这种创建是有问题的!! print(list2)list2 ...

  5. html-加水印--watermark--代码测试

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 使用OpenCL提升OpenCV图像处理性能 | speed up opencv image processing with OpenCL

    本文首发于个人博客https://kezunlin.me/post/59afd8b3/,欢迎阅读最新内容! speed up opencv image processing with OpenCL G ...

  7. Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解

    引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...

  8. 漫谈微服务架构:什么是Spring Cloud,为何要选择Spring Cloud

        Spring Cloud是基于Spring Boot的,因此还在使用SpringMVC的同学要先了解Spring Boot.先上一段官话,Spring Cloud是一个基于Spring Boo ...

  9. RocketMQ多master迁移至多master多slave模式

    一.项目背景 由于当前生产环境RocketMQ机器使用年限较长,已经过保,并且其中一台曾经发生过异常宕机事件.并且早期网络规划较乱,生产.开发.测试等网络没有分开,公司决定对当前网络进行规划,区分各个 ...

  10. 车位iou计算

    车位检测中,判断多帧图像检测出的车位是否是同一个车位.计算其IOU. 判断一个点是否在一个四边形内 Approach : Let the coordinates of four corners be ...