lightOJ1123 --Trail Maintenance(最小生成树+删边)

LightOJ1123 :Trail Maintenance

时间限制:2000MS    内存限制:32768KByte   64位IO格式:%lld & %llu
描述

Tigers in the Sunderbans wish to travel freely among the N fields (numbered from1 toN), 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 starts with an integer T (≤ 25), denoting the number of test cases.

Each case starts with two integers N (1 ≤ N ≤ 200) and W. W 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.

输出

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".

样例输入

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

 

 

 

一看题意显然是最小生成树,用克鲁斯卡尔暴力判断果断TLE。。。。。

听了大神的想法,才知道自己逗了

边数过多,复杂度是o(n*m),不TLE才怪

像这类边数多、节点少的题目应该在每一次生成最小生成树时将不要的边(构成环的边和权值过大的边)删去,这样复杂度会降到o(n*n)

代码:

 

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
typedef long long ll;
int pre[205];
struct edge
{
    int l,r,v;
    friend bool operator <(const edge &a, const edge &b)
    {
        return a.v<b.v;
    }
}a[10005];
int find(int x)
{
    if(pre[x]==x)return x;
    return pre[x]=find(pre[x]);
}
int main()
{
    int t,fir=1,n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        printf("Case %d:\n",fir++);
        int cnt=0;
        for(int i=0;i<m;i++)
        {
            for(int j=1;j<=n;j++)
                pre[j]=j;
            scanf("%d %d %d",&a[cnt].l,&a[cnt].r,&a[cnt].v);
            cnt++;
            sort(a,a+cnt);
            int ans=0,wa=0;
            for(int j=0;j<cnt;j++)
            {
                int x=find(a[j].l);
                int y=find(a[j].r);
                if(x==y)
                {
                    a[j].l=a[cnt-1].l;
                    a[j].r=a[cnt-1].r;
                    a[j].v=a[cnt-1].v;
                    cnt--;
                    j--;
                    continue;
                }
                else
                {
                    wa++;
                    pre[y]=x;
                    ans+=a[j].v;
                }
            }
            if(wa==n-1)printf("%d\n",ans);
            else printf("-1\n");
            if(cnt>n-1)cnt=n-1;
        }
    }
    return 0;
}


 

你可能感兴趣的:(最小生成树)