7001 – Bus Problem

Problem Link

Algorithm:

Kruskal

 

View Code

#include <bits/stdc++.h>
using namespace std;

#define INF 10000000

typedef unsigned long long ll;
typedef vector vi;
typedef vector< vi > vvi;
typedef pair<int, int> ii;
typedef vector vii;
typedef vector vvii;
typedef set si;
typedef map<string, int> msi;

#define all(x) x.begin(), x.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define spresent(c,x) ((c).find(x) != (c).end()) // for set,map
#define present(c,x) (find(all(c),x) != (c).end()) // for vector
#define uu first
#define vv second
#define fr(i,a,b) for(int i=int(a);i<=int(b);++i) #define nfr(i,a,b) for(int i= int(a);i>=int(b);–i)
#define mx 10010
struct edge{
int u,v,w;
bool operator<(const edge x) const{
return w<x.w; } }e[100010]; int pr[mx]; int find(int r) { if(pr[r]!=r) return pr[r]=find(pr[r]); else return r; } int main() { int t,n,m; cin>>t;
while(t–)
{
for(int i=0;i<=mx;i++) pr[i]=i; ll s=0; cin>>n>>m;
for(int i=0;i<m;i++) { cin>>e[i].u>>e[i].v>>e[i].w;

s+=(1ll*e[i].w);
}

sort(e,e+m);

for(int i=0;i<m;i++)
{
int u=find(e[i].u);
int v=find(e[i].v);
if(u!=v)
{
pr[v]=u;
s-=(1ll)*e[i].w;

}
}
cout<<s<<endl;

}

}

 

Advertisements

About waprogramming

I am in CSE,from CUET
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s