LightOJ 1123 Trail Maintenance(增量最小生成树)

                                                                    1123 - Trail Maintenance

    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

Tigers in the Sunderbans wish to travel freely among the N fields (numbered from 1 to N), even though they are separated by trees. The tigers wish to maintain trails between pairs of fields so that they can travel from any field to any other field using the maintained trails. Tigers may travel along a maintained trail in either direction.

The tigers do not build trails. Instead, they maintain deer trails that they have discovered. On any week, they can choose to maintain any or all of the deer animal trails they know about. Always curious, the tigers discover one new deer trail at the beginning of each week. They must then decide the set of trails to maintain for that week so that they can travel from any field to any other field. Tigers can only use trails which they are currently maintaining.

The tigers always want to minimize the total length of trail they must maintain. The tigers can choose to maintain any subset of the deer trails they know about, regardless of which trails were maintained the previous week. Deer trails (even when maintained) are never straight. Two trails that connect the same two fields might have different lengths. While two trails might cross, tigers are so focused; they refuse to switch trails except when they are in a field. At the beginning of each week, the tigers will describe the deer trail they discovered. Your program must then output the minimum total length of trail the tigers must maintain that week so that they can travel from any field to any other field, if there is such a set of trails.

Input

Input starts with an integer T (≤ 25), denoting the number of test cases.

Each case starts with two integers N (1 ≤ N ≤ 200) and WW is the number of weeks the program will cover (1 ≤ W ≤ 6000).

Each of the next W lines will contain three integers describing the trail the tigers found that week. The first two numbers denote the end points (filed numbers) and the third number denotes the length of the trail (1 to 10000). No trail has the same field as both of its end points.

Output

For each case, print the case number in a line. Then for every week, output a single line with the minimum total length of trail the tigers must maintain so that they can travel from any field to any other field. If no set of trails allows the tigers to travel from any field to any other field, output "-1".

Sample Input

Output for Sample Input

1

4 6

1 2 10

1 3 8

3 2 3

1 4 3

1 3 6

2 1 2

Case 1:

-1

-1

-1

14

12

8

 题目大意:n个点,m条边,每输入一条边就输出一遍最小生成树的权值,若没有生成树就输出-1

增量最小生成树,每一次加边之前先跑一遍kruskal找最小生成树,若已经有最小生成树,则新加入的边肯定会让其形成环,这时候开始进行删边操作,删去那个多余的边

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxm=6e3+7;
const int maxn=210;
struct Node
{
	int u,v;
	int val;
	bool operator<(const Node& tmp)const
	{
		return val<tmp.val;
	}
}edge[maxm];
int tree[maxn];
int cnt,n,m;
void init()
{
	for(int i=0;i<maxn;i++)
	{
		tree[i]=i;
	}
	return;
}
int find(int node)
{
	return tree[node]==node?node:tree[node]=find(tree[node]);
}
void kruskal()
{
	sort(edge,edge+cnt);
	int ans=0;
	int sum=0;
	int pos=-1;
	for(int i=0;i<cnt;i++)
	{
		int u=edge[i].u;
		int v=edge[i].v;
		int x=find(u);
		int y=find(v);
		if(x!=y)
		{
			tree[x]=y;
			ans++;
			sum+=edge[i].val;
		}
		else
		{
			pos=i;
			continue;
		}
	}
	if(pos!=-1)
	{
		edge[pos]=edge[cnt-1];
		cnt--;
	}
	if(ans!=n-1) printf("-1\n");
	else printf("%d\n",sum);
	return;
}
int main()
{
	//freopen("in.txt","r",stdin);
	int test;
	scanf("%d",&test);
	for(int cas=1;cas<=test;cas++)
	{
		cnt=0;
		scanf("%d%d",&n,&m);
		printf("Case %d:\n",cas);
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d",&edge[cnt].u,&edge[cnt].v,&edge[cnt].val);
			cnt++;
			init();
			kruskal();
		}
	}
	return 0;
}

1123 - Trail Maintenance

    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

Tigers in the Sunderbans wish to travel freely among the N fields (numbered from 1 to N), even though they are separated by trees. The tigers wish to maintain trails between pairs of fields so that they can travel from any field to any other field using the maintained trails. Tigers may travel along a maintained trail in either direction.

The tigers do not build trails. Instead, they maintain deer trails that they have discovered. On any week, they can choose to maintain any or all of the deer animal trails they know about. Always curious, the tigers discover one new deer trail at the beginning of each week. They must then decide the set of trails to maintain for that week so that they can travel from any field to any other field. Tigers can only use trails which they are currently maintaining.

The tigers always want to minimize the total length of trail they must maintain. The tigers can choose to maintain any subset of the deer trails they know about, regardless of which trails were maintained the previous week. Deer trails (even when maintained) are never straight. Two trails that connect the same two fields might have different lengths. While two trails might cross, tigers are so focused; they refuse to switch trails except when they are in a field. At the beginning of each week, the tigers will describe the deer trail they discovered. Your program must then output the minimum total length of trail the tigers must maintain that week so that they can travel from any field to any other field, if there is such a set of trails.

Input

Input starts with an integer T (≤ 25), denoting the number of test cases.

Each case starts with two integers N (1 ≤ N ≤ 200) and WW is the number of weeks the program will cover (1 ≤ W ≤ 6000).

Each of the next W lines will contain three integers describing the trail the tigers found that week. The first two numbers denote the end points (filed numbers) and the third number denotes the length of the trail (1 to 10000). No trail has the same field as both of its end points.

Output

For each case, print the case number in a line. Then for every week, output a single line with the minimum total length of trail the tigers must maintain so that they can travel from any field to any other field. If no set of trails allows the tigers to travel from any field to any other field, output "-1".

Sample Input

Output for Sample Input

1

4 6

1 2 10

1 3 8

3 2 3

1 4 3

1 3 6

2 1 2

Case 1:

-1

-1

-1

14

12

8

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/81053969