算法分析第一次作业

最小生成树算法:Kruskal算法rime算法的分析

题目来源:https://vjudge.net/problem/HDU-1863

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<bitset>
 7 #include<set>
 8 #include<deque>
 9 #include<queue>
10 #include<vector>
11 //#include<unordered_map>
12 #include<map>
13 #include<stack>
14 using namespace std;
15 #define ll long long
16 #define ull unsigned long long
17 #define pii pair<int,int>
18 #define Pii pair<ll,ll>
19 #define m_p make_pair
20 #define l_b lower_bound
21 #define u_b upper_bound 
22 const int inf = 0x3f3f3f3f;
23 const ll linf = 0x3f3f3f3f3f3f3f3f;
24 const int maxn = 3e5 + 11;
25 const int maxm = 2e3 + 11;
26 const int mod = 1e9 + 7;
27 const double eps = 1e-5;
28 ll rd() { ll x = 0, f = 1; char ch = getchar(); while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }return x * f; }
29 inline ll qpow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) { res *= a; res %= mod; }b >>= 1; a = a * a%mod; }return res; }
30 inline ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a%b); }
31 //iterator 
32 //head
33 int fa[maxn], n, m, ans;
34 struct node {
35     int from, to, val;
36     bool operator <(const node &a) {
37         return this->val < a.val;
38     }
39 }tree[maxn];
40 int find(int x) {
41     return fa[x] == x ? x : fa[x] = find(fa[x]);
42 }
43 void merge(int x, int y) {
44     fa[find(x)] = find(y);
45 }
46 bool same(int x, int y) {
47     return find(x) == find(y);
48 }
49 void Kruskal() {
50     ans = 0;
51     for (int i = 1; i <= m; i++) {
52         if (same(tree[i].from, tree[i].to)) continue;
53         ans += tree[i].val;
54         merge(tree[i].from, tree[i].to);
55     }
56     int count = 0;
57     for (int i = 1; i <= n; i++) {
58         if (find(i) == i) ++count;
59     }
60     if (count > 1) ans = -1;
61 }
62 void init() {
63     for (int i = 1; i <= n; i++) {
64         fa[i] = i;
65     }
66 }
67 int main() {
68     while (cin>>m && m != 0) {
69         n = rd();
70         init();
71         for (int i = 1; i <= m; i++) {
72             tree[i].from = rd();
73             tree[i].to = rd();
74             tree[i].val = rd();
75         }
76         sort(tree + 1, tree + m + 1);
77         Kruskal();
78         if (ans == -1) cout << "?" << '\n';
79         else cout << ans << '\n';
80     }
81 }
View Code

猜你喜欢

转载自www.cnblogs.com/tinkerx/p/12366493.html