题目链接: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. Html学习之十二(CSS选择器的应用二)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. 遗传算法求解旅行商(TSP)问题 -- python

    参考资料: 遗传算法解决TSP旅行商问题(附:Python实现) 遗传算法详解(GA)(个人觉得很形象,很适合初学者) from itertools import permutations impor ...

  3. MySQL学习笔记8——多表查询

    多表查询 多表查询 *合并结果集 *连接查询 *子查询 合并结果集 *要求被合并的表中,列的类型和列数相同(实际上是查询的结果集列类型和列数相同即可) *UNION,去除重复行 *UNION ALL, ...

  4. LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)

    题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...

  5. 20191028 Codeforces Round #534 (Div. 1) - Virtual Participation

    菜是原罪. 英语不好更是原罪. \(\mathrm{A - Grid game}\) 题解 \(4 \times 4\) 的格子,两种放法. 发现这两种在一起时候很讨厌,于是强行拆分这个格子 上面 \ ...

  6. Java LinkedList用法

    本想找队列Queue,发现那是一个接口,LinkedList实现了Queue接口,可以当作队列来用. 一.概述 Java的LinkedList是一种常用的数据容器,与ArrayList相比,Linke ...

  7. bayer2bmp

    #include <stdlib.h> #include <string.h> #include <getopt.h> #include <stdint.h& ...

  8. h5移动端页面强制横屏

    说明:这个的原文章来自于https://www.jianshu.com/p/9c3264f4a405  ,我做点点补充  ,谢谢原链接的小姐姐 最近公司是要我做一个h5的小视频,因为是视频接视频,并且 ...

  9. 【新特性速递】单元格导航(上下左右键,TAB键和ENTER键)

    上下左右按键 其实单元格导航(上下左右按键,需要启用表格的ShowSelectedCell属性)一直都存在,只不过之前的版本(v5.5.0)有一些小的BUG. BUG1 比如锁定列存在时,上下左右键只 ...

  10. 关于lambda总结-持续更新

    阅读:https://blog.csdn.net/u013541140/article/details/102710138 1 public static void main(String[] arg ...