时限: 1000MS   内存限制: 10000K 提交总数: 37001   接受: 17398 描述 热带岛屿拉格里山的首长有个问题.几年前,大量的外援花在了村庄之间的额外道路上.但是丛林不断地超越道路,因此庞大的道路网太昂贵而无法维护.老年人理事会必须选择停止维护一些道路.左上方的地图显示了目前正在使用的所有道路,以及每月维护这些道路的费用.当然,即使路线不像以前那么短,也需要采取某种方式在所有村庄之间保持通行.长老院长想告诉长老委员会每月要花多少钱才能维持连接所有村庄的道路.在上面的地图…
最小生成树——Minimum Spanning Tree,是图论中比较重要的模型,通常用于解决实际生活中的路径代价最小一类的问题.我们首先用通俗的语言解释它的定义: 对于有n个节点的有权无向连通图,寻找n-1条边,恰好将这n个节点相连,并且这n-1条边的权值之和最小. 对于MST问题,通常常见的解法有两种:Prim算法   或者  Kruskal算法+并查集 对于最小生成树,一定要注意其定义是在无向连通图的基础上,如果在有向图中,那么就需要另外的分析,单纯用无向图中的方法是不能得出正确解的,这一…
并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每个点只保存祖先,不保存父亲) 最小生成树kruskal:贪心算法+并查集数据结构,根据边的多少决定时间复杂度,适合于稀疏图 核心思想贪心,找到最小权值的边,判断此边连接的两个顶点是否已连接,若没连接则连接,总权值+=此边权值,已连接就舍弃继续向下寻找: 并查集数据结构程序: #include<ios…
Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23882   Accepted: 11193 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages som…
Network Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have…
Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expens…
#include<iostream> #include<cstring> #include<string> #include<cstdio> #include<algorithm> using namespace std; #define MAX 80000 int father[MAX], son[MAX]; int v,v2, l; struct Kruskal //存储边的信息 { int a; int b; int value; }; b…
#include<iostream> #include<algorithm> using namespace std; ,tot=; const int N = 1e5; ]; struct ac{ int v,u,w; }edge[N]; bool cmp(ac a,ac b){ return a.w<b.w; } inline int find(int x){ if(x!=f[x])f[x]=find(f[x]);return f[x]; } inline int joi…
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…
题目链接 http://poj.org/problem?id=1251 题意 有n个村庄,村庄之间有道路连接,求一条最短的路径能够连接起所有村庄,输出这条最短路径的长度. 思路 最小生成树问题,使用普利姆算法(Prime)或者克鲁斯卡尔算法(Kruskal)解决. 代码 Prime算法: #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using na…