uva 10600 - ACM Contest and Blackout(次小生成树)
来源:互联网 发布:淘宝省钱app 编辑:程序博客网 时间:2024/05/22 10:41
In order to prepare the “The First National ACM School Contest”(in 20??) the major of the city decided to provide all the schools with a reliable source of power. (The major is really afraid of blackoutsJ). So, in order to do that, power station “Future” and one school (doesn’t matter which one) must be connected; in addition, some schools must be connected as well.
You may assume that a school has a reliable source of power if it’s connected directly to “Future”, or to any other school that has a reliable source of power. You are given the cost of connection between some schools. The major has decided to pick out two the cheapest connection plans – the cost of the connection is equal to the sum of the connections between the schools. Your task is to help the major – find the cost of the two cheapest connection plans.
Input
The Input starts with the number of test cases, T (1£T£15) on a line. Then T test cases follow. The first line of every test case contains two numbers, which are separated by a space, N (3£N£100) the number of schools in the city, and M the number of possible connections among them. Next M lines contain three numbers Ai, Bi, Ci , where Ci is the cost of the connection (1£Ci£300) between schools Ai and Bi. The schools are numbered with integers in the range 1 to N.
Output
For every test case print only one line of output. This line should contain two numbers separated by a single space - the cost of two the cheapest connection plans. Let S1 be the cheapest cost and S2 the next cheapest cost. It’s important, that S1=S2 if and only if there are two cheapest plans, otherwise S1£S2. You can assume that it is always possible to find the costs S1 and S2..
Sample Input
Sample Output
2
5 8
1 3 75
3 4 51
2 4 19
3 2 95
2 5 42
5 4 31
1 2 9
3 5 66
9 14
1 2 4
1 8 8
2 8 11
3 2 8
8 9 7
8 7 1
7 9 6
9 3 2
3 4 7
3 6 4
7 6 2
4 6 14
4 5 9
5 6 10
110 121
37 37
Problem source: Ukrainian National Olympiad in Informatics 2001
Problem author: Shamil Yagiyayev
Problem submitter: Dmytro Chernysh
Problem solution: Shamil Yagiyayev, Dmytro Chernysh, K M Hasan
#include<cstdio>#include<cmath>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int maxn = 100 + 10;const int INF = 1000000000;struct Edge { int x, y; double d; bool operator < (const Edge& rhs) const { return d < rhs.d; }};struct MST{ int n, m;//点数和边数 Edge e[maxn*maxn];//储存所有的边 int pa[maxn];//用于并查集,父指针 vector<int> G[maxn];//用于Dfs,储存生成树中每个点相邻的点 vector<int> C[maxn];//用于Dfs,储存相应的边的权 vector<int> nodes;//用于Dfs,储存已经遍历过的节点 int maxcost[maxn][maxn];//储存最小生成树中,u、v唯一路径上的最大权值 int used[maxn*maxn]; //这里是把无根树,转为了有根树,具体做法: //在初始调用时Dfs(0,-1,0),然后后面节点拓展儿子时,不允许向回走 void dfs(int u, int fa, int facost){ //先对当前点同所有已访问过的节点,进行更新 for(int i = 0; i < nodes.size(); i++) { int x = nodes[i]; maxcost[u][x] = maxcost[x][u] = max(maxcost[x][fa], facost); } nodes.push_back(u); //再递归下一个点 for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if(v != fa) dfs(v, u, C[u][i]); } } void init(int n){ this -> n = n; m = 0; memset(maxcost,0,sizeof(maxcost)); memset(used,0,sizeof(used)); nodes.clear(); for(int i = 0; i < n; i++) { pa[i] = i; G[i].clear(); C[i].clear(); } } void AddEdge(int x,int y,int dist){ e[m++] = (Edge){x,y,dist}; } int findset(int x) { return pa[x] != x ? pa[x] = findset(pa[x]) : x; } int solve() { sort(e, e+m); int cnt = 0; int ans = 0; for(int i = 0; i < m; i++) { int x = e[i].x, y = e[i].y, u = findset(x), v = findset(y); int d = e[i].d; if(u != v) { used[i] = 1; pa[u] = v; G[x].push_back(y); C[x].push_back(d); G[y].push_back(x); C[y].push_back(d); ans += d; if(++cnt == n-1) break; } } return ans; }};MST solver;int main(){ int t,n,m; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); solver.init(n); for(int i = 0;i < m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); solver.AddEdge(a-1,b-1,c); } int ans1 = solver.solve(); int ans2 = INF; solver.dfs(0,-1,0); for(int i = 0;i < m;i++){ if(!solver.used[i]){ int x = solver.e[i].x; int y = solver.e[i].y; int d = solver.e[i].d; ans2 = min(ans2,ans1+d-solver.maxcost[x][y]); } } printf("%d %d\n",ans1,ans2); } return 0;}
- UVA 10600 - ACM Contest and Blackout 次小生成树
- 【UVa】10600 ACM Contest and Blackout 次小生成树
- uva 10600 ACM Contest and Blackout(次小生成树)
- UVA 10600 ACM Contest and Blackout (次小生成树)
- UVA - 10600 ACM Contest and Blackout(次小生成树)
- Uva 10600 ACM Contest and Blackout(次小生成树)
- UVA 10600 - ACM Contest and Blackout(最小生成树&次小生成树)
- UVa 10600 ACM contest and Blackout( 次小生成树)
- uva 10600 - ACM Contest and Blackout(次小生成树)
- UVA 10600 - ACM Contest and Blackout (次小生成树)
- UVA 10600 - ACM Contest and Blackout(次小生成树)
- UVA 10600 ACM Contest and Blackout(次小生成树)
- uva 10600 ACM Contest and Blackout (次小生成树)
- uva 10600 - ACM Contest and Blackout(次小生成树)
- UVA 10600 ACM Contest and Blackout(最小生成树and次小生成树)
- UVa 10600__ACM Contest and Blackout(次小生成树)
- UVA 10600 ACM Contest and Blackout 次小生成树/裸
- UVA 10600 ACM Contest and Blackout (次小生成树 Kruskal 封装)
- 一步一步教你在win8安装SQL2000(原创)
- Hbase写路径
- Big Switch开源SDN软件,廉价交换机时代到来
- Qt 线程基础(QThread、QtConcurrent等)
- TIME_WAIT状态下对接收到的数据包如何处理
- uva 10600 - ACM Contest and Blackout(次小生成树)
- -jar参数运行应用时classpath的设置方法
- JAVA Applet 工作流设计器
- Linux -- Shell
- class VideoCapture之:: VideoCapture
- redmine 集成 git
- linux顶半部和底半部中断机制
- Linux 下找不到ifconfig命令
- ubuntu12.04安装samba