lightoj1123 Trail Maintenance

思路:每新进一条边就MST一下,然后对于加入的边会形成环的话就删掉。不然会TLE的。

// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
// #define DEBUG
#ifdef DEBUG
#define debug(...) printf( __VA_ARGS__ )
#else
#define debug(...)
#endif
#define CLR(x) memset(x, 0,sizeof x)
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct Point{
	double x, y;
	Point(){}
	Point(double _x, double _y){
		x = _x;
		y = _y;
	}
	Point operator + (const Point& rhs)const{
		return Point(x + rhs.x, y + rhs.y);
	}
	Point operator - (const Point& rhs)const{
		return Point(x - rhs.x, y - rhs.y);
	}
	double operator ^ (const Point& rhs)const{
		return (x * rhs.y - y * rhs.x);
	}
	double operator * (const Point& rhs)const{
		return (x * rhs.x + y * rhs.y);
	}
	Point operator * (const double Num)const{
		return Point(x * Num,y * Num);
	}
	friend ostream& operator << (ostream& output,const Point& rhs){
		output << "(" << rhs.x << "," << rhs.y << ")";
		return output;
	}
};
typedef Point Vector;
/*向量的模*/
inline double Length(const Vector& A){//Get the length of vector A;
	return sqrt(A * A);//sqrt(x*x+y*y);
}
/*向量夹角*/
inline double Angle(const Vector& A,const Point& B){
	return acos(A * B/Length(A)/Length(B));
}
/*向量A旋转rad弧度*/
inline Point Rotate(const Vector& A, double rad){
	return Vector(A.x*cos(rad) - A.y*sin(rad),A.x*sin(rad) + A.y*cos(rad));
}
const int maxn = 210;
struct Edge{
	int u, v, w;
	bool operator < (const Edge& rhs)const{
		return w < rhs.w;
	}
}E[maxn];
int n, m, cnt;
int F[maxn];
int Find(int x){
	if (F[x] == -1) return x;
	return F[x] = Find(F[x]);
}
int GetMinCost(){
	int pos = -1;
	int ans = 0;
	int sum = 0;
	memset(F, -1,sizeof F);
	for (int i = 0;i < cnt;++i){
		int t1 = Find(E[i].u);
		int t2 = Find(E[i].v);
		if (t1 != t2){
			F[t1] = t2;
			sum += E[i].w;
			ans++;
		}
		if (t1 == t2 && pos == -1){
			pos = i;
		}
	}
	if (pos != -1) E[pos] = E[--cnt];
	if (ans == n - 1) return sum;
	return -1;
}
int main()
{	
	ios::sync_with_stdio(false);
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t, icase = 0;
	// scanf("%d",&t);
	cin >> t;
	while(t--){
		// scanf("%d%d",&n,&m);
		cin >> n >> m;
		cnt = 0;
		printf("Case %d:\n", ++icase);
		int a, b, c;
		for (int i = 0;i < m;++i){
			// scanf("%d%d%d",&a,&b,&c);
			cin >> a >> b >> c;
			E[cnt].u = a;
			E[cnt].v = b;
			E[cnt].w = c;
			cnt++;
			sort(E,E + cnt);
			printf("%d\n", GetMinCost());
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值