#include<bits/stdc++.h>
#define mx 200009
using namespace std;
typedef long long ll;
struct edge
{
ll u,v,w;
bool operator < (const edge& p) const
{
return w<p.w;
}
};
ll pr[mx];
vector<edge>e;
ll fnd(ll r)
{
return (pr[r]==r)?r:fnd(pr[r]);
}
ll mst(ll n)
{
sort(e.begin(),e.end());
//reverse(e.begin(),e.end());
for(ll i=1;i<=n;i++)
{
pr[i]=i;
}
ll cnt=0,s=0;
for(int i=0;i<(ll)e.size();i++)
{
ll u=fnd(e[i].u);
ll v=fnd(e[i].v);
if(u!=v)
{
pr[u]=v;
cnt++;
s+=e[i].w;
if(cnt==n-1)
break;
}
}
return s;
}
int main()
{
//ofstream o("op.txt");
while(1)
{
ll n,m,sum=0;
scanf("%lld %lld",&n,&m);
if(n==0&&m==0)
break;
for(int i=1;i<=m;i++)
{
ll u,v,w;
scanf("%lld%lld%lld",&u,&v,&w);
sum+=w;
edge get;
get.u=u+1;
get.v=v+1;
get.w=w;
e.push_back(get);
}
printf("%lld\n",sum-mst(n));
e.clear();
}
return 0;
}
#define mx 200009
using namespace std;
typedef long long ll;
struct edge
{
ll u,v,w;
bool operator < (const edge& p) const
{
return w<p.w;
}
};
ll pr[mx];
vector<edge>e;
ll fnd(ll r)
{
return (pr[r]==r)?r:fnd(pr[r]);
}
ll mst(ll n)
{
sort(e.begin(),e.end());
//reverse(e.begin(),e.end());
for(ll i=1;i<=n;i++)
{
pr[i]=i;
}
ll cnt=0,s=0;
for(int i=0;i<(ll)e.size();i++)
{
ll u=fnd(e[i].u);
ll v=fnd(e[i].v);
if(u!=v)
{
pr[u]=v;
cnt++;
s+=e[i].w;
if(cnt==n-1)
break;
}
}
return s;
}
int main()
{
//ofstream o("op.txt");
while(1)
{
ll n,m,sum=0;
scanf("%lld %lld",&n,&m);
if(n==0&&m==0)
break;
for(int i=1;i<=m;i++)
{
ll u,v,w;
scanf("%lld%lld%lld",&u,&v,&w);
sum+=w;
edge get;
get.u=u+1;
get.v=v+1;
get.w=w;
e.push_back(get);
}
printf("%lld\n",sum-mst(n));
e.clear();
}
return 0;
}
No comments:
Post a Comment