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

来源:互联网 发布:淘宝花呗套利 编辑:程序博客网 时间:2024/05/10 09:22

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 9999999999using namespace std;#define maxm 100000+500#define maxn 100000+500struct 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;}





0 0
原创粉丝点击