码迷,mamicode.com
首页 > 其他好文 > 详细

第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛 Problem 1566 - C - Spanning Tree

时间:2015-04-22 09:40:45      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

Description
You are given a graph with N nodes and M edges. 
Then every time you are required to add an additional edge with weight Wi connecting the node Ai and Bi in the graph, and then calculate the sum of the edges’ weight of the Minimum Spanning Tree of the current graph. You’ll be asked to complete the mission for Q times.
The Minimum Spanning Tree of a graph is defined to be a set of N - 1 edges which connects all the N nodes of the graph and the sum of all the edges is as small as possible.
It‘s guaranteed that the graph is connected.
Input
First line of each case contains three numbers N , M and Q.(1?≤??N,Q ≤?1000, 1?≤??M?≤?100000 ,)

The following M lines contains three numbers Ai , Bi and Wi.( 1?≤??Ai, Bi?≤?1000, 1?≤??Wi≤?100000).

The last Q lines of this case contains three numbers Ai , Bi and Wi. ( 1 <= Ai, Bi <= 1000, 1?≤??Wi≤?100000 ). 
Output
Output the answer in a single line for each case.
Sample Input
3 3 3
2 1 8
3 1 4
1 2 6
1 2 4
2 3 1
1 1 4
3 3 3
2 1 7
3 2 8
3 3 6
1 3 3
2 2 3
2 2 3
Sample Output
8
5
5
10
10
10

用STL的vector容器写,直接内部排序了。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define PI acos(-1.0)
#define M 1000005  //10^6
#define eps 1e-8
#define LL long long
#define moo 1000000007
#define INF 9999999999
using namespace std;
#define maxm 100000+500
#define maxn 100000+500

struct Edge
{
    int u,v,w;
    bool operator <(const Edge &rhs) const
    {
        return w<rhs.w;
    }
    void read()
    {
        scanf("%d %d %d",&u,&v,&w);
    }

}edge[maxm];

vector<Edge> res;

int fa[maxm];

int findfa(int x)
{
    if(x==fa[x])
    return x;
    return fa[x]=findfa(fa[x]);
}

bool same(int a,int b)
{
    return findfa(a)==findfa(b);
}

void merge(int a,int b)
{

    int xx=findfa(a);
    int yy=findfa(b);
    if(xx>yy)
    fa[xx]=yy;
    else
    fa[yy]=xx;

}

int kruscal(vector<Edge> &e,int n)
{
    for(int i=1;i<=n;i++)
    fa[i]=i;

    vector<Edge>res;

    sort(e.begin(),e.end());
    int ans=0;

    for(int i=0;i<e.size();i++)
    {
        if(same(e[i].u,e[i].v))
        continue;

        merge(e[i].u,e[i].v);
        ans+=e[i].w;
        res.push_back(e[i]);
    }
    e=res;
    return ans;

}

int main()
{
    int m,n,q;
    while(~scanf("%d %d %d",&n,&m,&q))
    {

        for(int i=0;i<m;i++)
        edge[i].read();

        edge[m++].read();
        sort(edge,edge+m);
        res.clear();

        for(int i=1;i<=n;i++)
        fa[i]=i;

        int ans=0;

        for(int i=0;i<m;i++)
        {
            if(same(edge[i].u,edge[i].v))
            continue;

            merge(edge[i].u,edge[i].v);
            ans+=edge[i].w;
            res.push_back(edge[i]);
        }

        q--;
        printf("%d\n",ans);

        while(q--)
        {
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            res.push_back((Edge){u,v,w});
            printf("%d\n",kruscal(res,n));
        }

    }

    return 0;
}






第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛 Problem 1566 - C - Spanning Tree

标签:

原文地址:http://blog.csdn.net/wust_zjx/article/details/45177229

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!