Bad Cowtractors
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 has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie.
Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
Output
* Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.
Sample Input
5 8 1 2 3 1 3 7 2 3 10 2 4 4 2 5 8 3 4 6 3 5 2 4 5 17
Sample Output
42
Hint
OUTPUT DETAILS:
The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.
//#include<bits/stdc++.h>
#define _CRT_SBCURE_NO_DEPRECATE
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
//#define UP(i,x,y) for(int i=x;i<=y;i++)
//#define DOWN(i,x,y) for(int i=x;i>=y;i--)
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lson k<<1
#define rson k<<1|1
#define mid (l+r)/2
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
//typedef unsigned long long ull;
#define MOD 1000000007
const int maxn = 10050;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
int n, m, s, t;
int len;
struct road
{
int u, v, cost;
};
road G[maxn*5];
bool vis[maxn];
int par[maxn<<1];
void init()
{
for(int i = 0; i < maxn<<1; i++) par[i] = i;
}
int Find(int x)
{
return par[x]==x? x : par[x] = Find(par[x]);
}
bool same(int x, int y)
{
int fx = Find(x), fy = Find(y);
if(fx == fy)return true;
else return false;
}
void unite(int x, int y)
{
int fx = Find(x), fy = Find(y);
if(fx == fy) return;
else{
par[fx] = fy;
return;
}
}
bool cmp(road a, road b)
{
return a.cost>b.cost;
}
ll kruskal()
{
sort(G, G+len, cmp);
init();
ll res = 0;
int cnt = 1;
for(int i = 0; i < len; i++)
{
road e = G[i];
if(!same(e.u, e.v))
{
unite(e.u, e.v);
res+=e.cost;
cnt++;
}
}
if(cnt == n)
return res;
else
return -1;
}
int main()
{
//freopen("out.txt", "w", stdout);
while(~sdd(n,m))
{
ms(G,0);
len = 0;
int ta, tb, tc;
for(int i = 0; i < m; i++)
{
sddd(ta, tb, tc);
G[len].u = ta;
G[len].v = tb;
G[len++].cost = tc;
}
ll ans = kruskal();
printf("%lld\n", ans);
}
return 0;
}