POJ1861(Network)-Kruskal

题目在这

Sample Input

4 6
1 2 1
1 3 1
1 4 2
2 3 1
3 4 1
2 4 1

Sample Output

1
4
1 2
1 3
2 3
3 4

题目意思:4个点,6个边,每个边有对应的权值。最后输出一行为路径中最大的边的值,第二行为路径上边的总数,

第三行为每条边的始末编号。题目需要求出最小生成树的最大边的最小值。

 1 /*
 2 Problem: 1861        User:
 3 Memory: 416K        Time: 500MS
 4 Language: C++        Result: Accepted
 5 */
 6 #include <iostream>
 7 #include <algorithm>
 8 using namespace std;
 9
10 #define MAX 15010
11 int p[1010];//存放父亲结点
12
13 struct Edge
14 {
15     int u;
16     int v;
17     int w;
18 }map[MAX],ans[MAX];
19
20 bool cmp(Edge a,Edge b)
21 {
22     return a.w<b.w;
23 }
24
25 int Find(int a)
26 {
27     return a==p[a]?a:a=Find(p[a]);
28 }
29
30 int main()
31 {
32     int N,M,i;
33     int a,b,c;
34     cin>>N>>M;
35     for(i=0;i<=N;i++)
36     {
37         p[i] = i;
38     }
39     for(i=0;i<M;i++)
40     {
41         cin>>a>>b>>c;
42         map[i].u = a;
43         map[i].v = b;
44         map[i].w = c;
45     }
46     sort(map,map+M,cmp);
47     int count = 0;
48     int maxEdge = 0;
49     for(i=0;i<M;i++){
50         int x = Find(map[i].u);
51         int y = Find(map[i].v);
52         if(x != y)
53         {
54             p[x] = y;//不在一个集合,合并
55             ans[count].u = map[i].u;
56             ans[count].v = map[i].v;
57             count ++;
58             if(map[i].w>maxEdge)
59                 maxEdge = map[i].w;
60         }
61     }
62     cout<<maxEdge<<endl;//路径中最长的边
63     cout<<count<<endl;//边的总数
64     for(i=0;i<count;i++)
65         cout<<ans[i].u<<" "<<ans[i].v<<endl;/输出每条路径
66     return 0;
67 }

原文地址:https://www.cnblogs.com/ygsworld/p/11256659.html

时间: 2024-11-08 22:44:05

POJ1861(Network)-Kruskal的相关文章

POJ1861 Network(Kruskal)(并查集)

Network Time Limit: 1000MS     Memory Limit: 30000K Total Submissions: 16047   Accepted: 6362   Special Judge 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

POJ 1861 Network (Kruskal求MST模板题)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14103   Accepted: 5528   Special Judge 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 c

POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14021   Accepted: 5484   Special Judge 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 c

ZOJ 1586 QS Network(kruskal)(基础)

Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunicate with each other via networks. If two QS want to get connected, they

【BZOJ 3732】 Network Kruskal重构树+倍增LCA

Kruskal重构树裸题, Sunshine互测的A题就是Kruskal重构树,我通过互测了解到了这个神奇的东西... 理解起来应该没什么难度吧,但是我的Peaks连WA,,, 省选估计要滚粗了TwT #include<cstdio> #include<cstring> #include<algorithm> #define for1(i,a,n) for(int i=(a);i<=(n);i++) #define for2(i,a,n) for(int i=(a

BZOJ 3732 Network Kruskal重构树

题目大意:给定一个n个点m条边的无向连通图,k次询问两点之间所有路径中最长边的最小值 Kruskal+倍增LCA做法见http://blog.csdn.net/popoqqq/article/details/39755703 LCT做法见http://blog.csdn.net/popoqqq/article/details/39929277 Kruskal重构树真是强大--一不小心手滑就RANK1啥的-- 每加入一条边时,我们并不链接这条边的两端点,而是把这条边两端点所在并查集的根连接起来,而

POJ-1861,Network,最小生成树水题,,注意题面输出有问题,不必理会~~

Network Time Limit: 1000MS   Memory Limit: 30000K          Special Judge http://poj.org/problem?id=1861 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 compa

【BZOJ3732】 Network Kruskal+倍增lca

Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_j ( 1 < = d_j < = 1,000,000,000). 现在有 K个询问 (1 < = K < = 15,000). 每个询问的格式是:A B,表示询问从A点走到B点的所有路径中,最长的边最小值是多少? Input 第一行: N, M, K. 第2..M+1行: 三个正整数

BZOJ 3732 Network Kruskal+倍增LCA

题目大意:给定一个n个点m条边的无向连通图,k次询问两点之间所有路径中最长边的最小值 NOIP2013 货车运输,几乎就是原题...只不过最小边最大改成了最大边最小... 首先看到最大值最小第一反应二分答案 但是二分答案O(kmlogn)明显做不了 这里我们考虑最小生成树 先生成一棵最小生成树,然后每次询问利用倍增LCA求出路径上的最大权值即可 本蒟蒻居然把LCA写挂了... 而且样例还过了... 伤不起啊... 90%达成 剩下一道刷点啥呢... #include<cstdio> #incl