第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛 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 ).
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
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
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
- 第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛 Problem 1566 - C - Spanning Tree
- 第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛
- C - Spanning Tree 最小生成树 Kruskal (第四届华中区程序设计邀请赛暨武汉大学第十三届校赛)
- 第二届华中区程序设计邀请赛暨武汉大学第十一届校赛 网络预选赛 解题报告
- 第二届华中区程序设计邀请赛暨武汉大学第十一届校赛 网络预选赛 解题报告
- 第三届华中区程序设计邀请赛暨武汉大学第十二届校赛 网络预选赛 水题解题报告
- 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 网络预选赛
- 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 网络预选赛
- 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 Problem 1603 - Minimum Sum
- 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 Problem 1608 - Calculation
- 首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛网络预赛C题(HDOJ-4217)
- 第二届华中区程序设计邀请赛暨武汉大学第十一届校赛 现场赛 解题报告
- 首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛网络预赛A题(HDOJ-4215)
- 首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛网络预赛I题(HDOJ-4223)
- 首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛网络预赛G题(HDOJ-4221)
- 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 现场赛 WHU oj Problem 1613 - Count in Sama’s triangle 扩展欧几里得
- HDU4223&&NYOJ422 首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛
- 首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛训练总结【8/11】
- JavaMail发送邮件(含附件)的例子
- <STL> pair随笔
- 学习ionic的资源和案例
- OJ题——组合题
- 偏好保存(在Activity里很简洁)
- 第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛 Problem 1566 - C - Spanning Tree
- Ubuntu 14.04 64bit上安装有道词典Linux版本
- 质数因子
- 用Set类判断Map里key是否存在的办法
- 日志查找自动化学习(Fighting1.1)
- Java Thread yield()
- 面向对象(内部类练习)
- socket中的非阻塞设置
- JAVA集合操作的利器:CollectionUtils