[POJ2377]Bad Cowtractors(最大生成树,Kruskal)

题目链接:http://poj.org/problem?id=2377

于是就找了一道最大生成树的AC了一下,注意不连通的情况啊,WA了一次。

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <fstream>
 24 #include <cassert>
 25 #include <cstdio>
 26 #include <bitset>
 27 #include <vector>
 28 #include <deque>
 29 #include <queue>
 30 #include <stack>
 31 #include <ctime>
 32 #include <set>
 33 #include <map>
 34 #include <cmath>
 35 using namespace std;
 36 #define fr first
 37 #define sc second
 38 #define cl clear
 39 #define BUG puts("here!!!")
 40 #define W(a) while(a--)
 41 #define pb(a) push_back(a)
 42 #define Rint(a) scanf("%d", &a)
 43 #define Rll(a) scanf("%lld", &a)
 44 #define Rs(a) scanf("%s", a)
 45 #define Cin(a) cin >> a
 46 #define FRead() freopen("in", "r", stdin)
 47 #define FWrite() freopen("out", "w", stdout)
 48 #define Rep(i, len) for(int i = 0; i < (len); i++)
 49 #define For(i, a, len) for(int i = (a); i < (len); i++)
 50 #define Cls(a) memset((a), 0, sizeof(a))
 51 #define Clr(a, x) memset((a), (x), sizeof(a))
 52 #define Full(a) memset((a), 0x7f7f, sizeof(a))
 53 #define lrt rt << 1
 54 #define rrt rt << 1 | 1
 55 #define pi 3.14159265359
 56 #define RT return
 57 #define lowbit(x) x & (-x)
 58 #define onenum(x) __builtin_popcount(x)
 59 typedef long long LL;
 60 typedef long double LD;
 61 typedef unsigned long long ULL;
 62 typedef pair<int, int> pii;
 63 typedef pair<string, int> psi;
 64 typedef map<string, int> msi;
 65 typedef vector<int> vi;
 66 typedef vector<LL> vl;
 67 typedef vector<vl> vvl;
 68 typedef vector<bool> vb;
 69
 70 typedef struct Edge {
 71     int u, v, w;
 72     Edge() {}
 73     Edge(int uu, int vv, int ww) : u(uu), v(vv), w(ww) {}
 74 }Edge;
 75 const int maxn = 1010;
 76 const int maxm = 20020;
 77 int n, m;
 78 int pre[maxn];
 79 Edge edge[maxm];
 80
 81 bool cmp(Edge a, Edge b) {
 82     RT a.w > b.w;
 83 }
 84
 85 int find(int x) {
 86     return x == pre[x] ? x : pre[x] = find(pre[x]);
 87 }
 88
 89 int unite(int x, int y) {
 90     int fx = find(x);
 91     int fy = find(y);
 92     if(fx != fy) {
 93         pre[fy] = fx;
 94         return 1;
 95     }
 96     return 0;
 97 }
 98
 99 int main() {
100     // FRead();
101     int u, v, c;
102     while(~Rint(n) && ~Rint(m)) {
103         Rep(i, n+5) pre[i] = i;
104         Rep(i, m) {
105             Rint(u); Rint(v); Rint(c);
106             edge[i] = Edge(u, v, c);
107         }
108         sort(edge, edge+m, cmp);
109         int ret = 0;
110         Rep(i, m) {
111             if(unite(edge[i].u, edge[i].v)) {
112                 ret += edge[i].w;
113             }
114         }
115         int flag = 0;
116         For(i, 1, n+1) {
117             if(pre[i] == i) flag++;
118             if(flag > 1) break;
119         }
120         if(flag > 1) printf("-1\n");
121         else printf("%d\n", ret);
122     }
123     RT 0;
124 }
时间: 2024-07-29 17:50:23

[POJ2377]Bad Cowtractors(最大生成树,Kruskal)的相关文章

POJ2377 Bad Cowtractors【Kruskal】【求最大生成树】

Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10933 Accepted: 4614 Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ

POJ2377 Bad Cowtractors 【最大生成树】

Bad Cowtractors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10885   Accepted: 4586 Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N

POJ2377Bad Cowtractors(最大生成树)

POJ2377Bad Cowtractors 题目大意:给一个带权无向图,求最大生成树. 解题思路: 因为最小生成树按照kruskal的贪心算法是可以证明正确的,那么反向我们取最大的权值的边,然后不断的加入形成的生成树就是最大生成树. 代码: #include <cstdio> #include <algorithm> using namespace std; const int maxn = 1005; const int maxm = 20005; int N, M; int

【luogu1967】【noip2013】 货车运输 [生成树kruskal LCA ]

P1967 货车运输最大生成树+倍增算路径最小值 最大生成树就是kruskal时将边改为降序 然后就和普通kruskal一样 然后就是用的LCA倍增模板中说的其它骚操作一样 可以在预处理的时候还可以顺便记录下这段路径的权值最大值 最小值或者权值和之类的信息,这样就可以在O(logn)的时间内求出树上两点间路径权值的最大值.最小值还有权值和 #include<iostream> #include<cstdio> #include<queue> #include<cs

poj2377 Bad Cowtractors

思路: 最大生成树. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 8 struct edge 9 { 10 int a, b, cost; 11 }; 12 edge es[20005]; 13 int ran[1005]; 1

HDU 4786 Fibonacci Tree 并查集+生成树=kruskal

Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2149    Accepted Submission(s): 682 Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him t

poj - 2377 Bad Cowtractors(最大生成树)

http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她想要花费越多越好,并且任意两个农场都需要连通,并且不能存在环.后面两个条件保证最后的连通图是一棵树. 输出最小花费,如果没办法连通所有节点输出-1. 最大生成树问题,按边的权值从大道小排序即可,kruskal算法可以处理重边的情况,但是在处理的时候,不能仅仅因为两个节点在同一个连通子图就判断图不合法

poj图论解题报告索引

最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman-ford,spfa) poj1511 - Invitation Cards(单源来回最短路径,spfa邻接表) poj1797 - Heavy Transportation(最大边,最短路变形,dijkstra,spfa,bellman-ford) poj2240 - Arbitrage(汇率问题,

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734