题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1640

题意:中文题诶~

思路:kruscal

题目要求是在边权最大值最小的情况下总权值尽量大,注意其中的优先级;

可以先从小到大加边kruscal一遍得到最小的最大边权,再从大到小加边kruscal一遍即可得出答案;

注意第二边kruscal时要先判当前加的边权不大于限制边权再加边,不然构造的并查集是错的;

代码:

 #include <iostream>
#include <algorithm>
#include <stdio.h>
#define ll long long
using namespace std; const int MAXN=2e5+;
const ll inf=0x7f7f7f7f7f; struct node{
int x, y;
ll v;
}edge[MAXN]; int pre[MAXN], n, m; bool cmp(node a, node b){
return a.v < b.v;
} int find(int x){
return pre[x]==x?x:pre[x]=find(pre[x]);
} ll jion(node edge, ll cnt){
if(edge.v>cnt) return ;
int fx=find(edge.x);
int fy=find(edge.y);
if(fx==fy) return ;
pre[fx]=fy;
return edge.v;
} void init(void){
for(int i=; i<=n; i++){
pre[i]=i;
}
} void kruscal(void){
ll cnt=, ans=, cc=inf;
init();
sort(edge, edge+m, cmp);
for(int i=; i<m; i++){
ll gg=jion(edge[i], cc);
cnt=max(cnt, gg);
}
init();
for(int i=m-; i>=; i--){
ans+=jion(edge[i], cnt);
}
printf("%lld\n", ans);
} int main(void){
scanf("%d%d", &n, &m);
for(int i=; i<m; i++){
scanf("%d%d%lld", &edge[i].x, &edge[i].y, &edge[i].v);
}
kruscal();
return ;
}

51nod1640(kruscal)的更多相关文章

  1. hdu1162(最小生成树 prim or kruscal)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 意义:给出一些点,用线问使所有点直接或间接连通,需要多长: 思路:裸最小生成树: 法1: pri ...

  2. HDU1879 kruscal 继续畅通工程

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  3. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

  4. UVA 1151 Buy or Build (MST最小生成树,kruscal,变形)

    题意: 要使n个点之间能够互通,要使两点直接互通需要耗费它们之间的欧几里得距离的平方大小的花费,这说明每两个点都可以使其互通.接着有q个套餐可以选,一旦选了这些套餐,他们所包含的点自动就连起来了,所需 ...

  5. UVA 1395 Slim Span (最小生成树,MST,kruscal)

    题意:给一个图,找一棵生成树,其满足:最大权-最小权=最小.简单图,不一定连通,权值可能全相同. 思路:点数量不大.根据kruscal每次挑选的是最小权值的边,那么苗条度一定也是最小.但是生成树有多棵 ...

  6. 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...

  7. 【HDU 4463 Outlets】最小生成树(prim,kruscal都可)

    以(x,y)坐标的形式给出n个点,修建若干条路使得所有点连通(其中有两个给出的特殊点必须相邻),求所有路的总长度的最小值. 因对所修的路的形状没有限制,所以可看成带权无向完全图,边权值为两点间距离.因 ...

  8. Kruscal 、 Prime Template

    Kruscal  Template : 很裸的Kruscal Template(求最小生成树中最长路,即最短路中最长路) //#pragma comment(linker, "/STACK: ...

  9. HDOJ 1598 Kruscal

    贪心思想的Kruscal:先对边排序,再从第一条边开始,一旦start point 和 end poiont 连上,就break #include <stdio.h> #include & ...

随机推荐

  1. 百度之星2016资格赛D,水题

    很简单的题,主要是要用字符串哈希,把字符串处理成整数.接下来可以继续用hash,也可以像我一样用个map就搞定了. /* * Author : ben */ #include <cstdio&g ...

  2. Redis(一)基础数据结构

    1.目录 Redis 基础数据结构 string (字符串) list (列表) hash (字典) set (集合) zset (集合) 容器型数据结构的通用规则 过期时间 2.Redis 基础数据 ...

  3. 一文读懂P2P和区块链的异同

    说到P2P,大家的第一反应肯定是网络贷款.非法集资...事实上,P2P不等于网络贷款,他是点对点,人对人的交易模式. 而对于近两年最热的区块链技术,他和P2P有什么区别呢?区块链数据交换模式包括私有链 ...

  4. SVN在myeclipse中新建地址的时出现被拒绝

    新建资源库地址: 控制台提示信息: svn: connection refused by the server svn: connection refused by the server Connec ...

  5. (转)windows下一分钟配置ngnix实现HLS m3u8点播

    一.首先保证nginx能正常运行:          这个就是因为前面我们把nginx的目录加到了Path中,然而nginx启动时各种路径都是以当前工作目录为起始点的,这就导致了系统去“C:\User ...

  6. flex平分测试

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. Codeforces Round #363 (Div. 2) C. Vacations —— DP

    题目链接:http://codeforces.com/contest/699/problem/C 题解: 1.可知每天有三个状态:1.contest ,2.gym,3.rest. 2.所以设dp[i] ...

  8. linuxshell,闲散命令记录

    1. 移动光标到 0:0 位置 printf "\033[0;0H" 2. 清屏 printf "\033[2J" 3. awk接收 外部变量 awk -v v ...

  9. Android-低功耗蓝牙(BLE)-客户端(主机/中心设备)和服务端(从机/外围设备)

    一.Android 低功耗蓝牙(BLE)的API简介 从Android 4.3(API 18)才支持低功耗蓝牙(Bluetooth Low Energy, BLE)的核心功能, BLE蓝牙协议是GAT ...

  10. BluetoothLELibrary 支持1对1连接

    github地址:https://github.com/qindachang/BluetoothLELibrary 该库只支持1对1连接,如果你想1对多设备连接,请移步至 BluetoothLE-Mu ...