Algorithm


Problem link- https://www.spoj.com/problems/ULM09/

ULM09 - Dark roads

 

Economic times these days are tough, even in Byteland. To reduce the operating costs, the government of Byteland has decided to optimize the road lighting. Till now every road was illuminated all night long, which costs 1 Bytelandian Dollar per meter and day. To save money, they decided to no longer illuminate every road, but to switch off the road lighting of some streets. To make sure that the inhabitants of Byteland still feel safe, they want to optimize the lighting in such a way, that after darkening some streets at night, there will still be at least one illuminated path from every junction in Byteland to every other junction.

What is the maximum daily amount of money the government of Byteland can save, without making their inhabitants feel unsafe?

Input

The input file contains several test cases. Each test case starts with two numbers m and n, the number of junctions in Byteland and the number of roads in Byteland, respectively. Input is terminated by m = n = 0. Otherwise, 1 ≤ m ≤ 200000 and m-1 ≤ n ≤ 200000. Then follow n integer triples x, y, z specifying that there will be a bidirectional road between x and y with length z meters (0 ≤ x, y < m and x ≠ y). The graph specified by each test case is connected. The total length of all roads in each test case is less than 231.

Output

For each test case print one line containing the maximum daily amount the government can save.

Sample

Input:
7 11
0 1 7
0 3 5
1 2 8
1 3 9
1 4 7
2 4 5
3 4 15
3 5 6
4 5 8
4 6 9
5 6 11
0 0

Output:
51

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
 
#define MOD (ll)1000000007
#define pb   push_back
#define EPS 1e-9
#define FOR(i,n)  for(int i = 0;i < n; i++)
#define FORE(i,a,b) for(int i = a;i <= b; i++)
#define pi(a)   printf("%d\n", a)
#define all(c)  c.begin(), c.end()
#define tr(container, it)   for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define gc getchar_unlocked
#define sdi(a, b)   si(a);si(b)
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

void si(int &x){
    register int c = gc();
    x = 0;
    int neg = 0;
    for(;((c < 48 || c>57) && c != '-');c = gc());
    if(c=='-') {neg=1;c=gc();}
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x=-x;
}



const int MAXN = 2e5+5;
int parent[MAXN], buildings, streets;
pair < ll, pair<int, int> > p[MAXN];

void initialize(){
	for(int i = 0;i <= buildings; i++){
		parent[i] = i;
	}
}

int root(int x){
	while(parent[x] != x){
		parent[x] = parent[parent[x]];
		x = parent[x];
	}
	return x;
}

void union1(int x, int y){
	int root_a = root(x);
	int root_b = root(y);
	parent[root_a] = root_b;
}

ll kruskal(pair<ll, pair<int, int> > p[]){
	int x, y;
	ll cost, minimumCost = 0;
	for(int i = 0;i < streets; i++){
		x = p[i].second.first;
		y = p[i].second.second;
		cost = p[i].first;
		//check if the selected edge makes a cycle or not.
		if(root(x)!=root(y)){
			minimumCost += cost;
			union1(x, y);
		}
	}
	return minimumCost;
}

int main(){
	io;
	// int t;
	// cin>>t;
	while(1){

		// int price;
		cin>>buildings>>streets;
		if(buildings == 0 && streets == 0)
			break;

		ll weight, cost, minimumCost;
		initialize();
		int x,y;
		ll totalCost = 0;
		for(int i = 0;i < streets; i++){
			cin>>x>>y>>weight;
			totalCost += weight;
			p[i] = make_pair(weight, make_pair(x, y));
		}
		sort(p, p+streets);
		minimumCost = kruskal(p);
		ll ans = totalCost-minimumCost;
		cout<<ans<<endl;
	}
	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
7 11
0 1 7
0 3 5
1 2 8
1 3 9
1 4 7
2 4 5
3 4 15
3 5 6
4 5 8
4 6 9
5 6 11
0 0

Output

x
+
cmd
51
Advertisements

Demonstration


SPOJ Solution-Dark roads-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python