uva 10600 ACM Contest and Blackout(次小生成树)
2015-01-25 16:56
387 查看
Problem A
ACM CONTEST AND BLACKOUT
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 dothat, 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 thenext 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 author: Shamil Yagiyayev
Problem submitter: Dmytro Chernysh
Problem solution: Shamil Yagiyayev, Dmytro Chernysh, K M Hasan
/* 一次过,,幸福来的太强烈了,加油!!! 今年在学校的最后一题。。。。 Time:2015-1-25 16:55 */ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int INF=0x3f3f3f3f; const int MAX=105; bool vis[MAX],used[MAX][MAX]; int pre[MAX],dis[MAX],g[MAX][MAX],path[MAX][MAX]; int n,m; int Prim(){ for(int i=0;i<=n;i++){ dis[i]=g[1][i]; vis[i]=false; pre[i]=1; } int Mst=0; int minV,u;vis[1]=true; for(int i=1;i<=n;i++){ u=1,minV=INF; for(int j=1;j<=n;j++){ if(!vis[j]&&minV>dis[j]){ minV=dis[j]; u=j; } } if(minV==INF) return Mst; Mst+=dis[u];vis[u]=true; used[u][pre[u]]=used[pre[u]][u]=true; for(int j=1;j<=n;j++){ if(vis[j]&&j!=u){ path[u][j]=path[j][u]=max(path[pre[u]][j],dis[u]);//此处是pre[u]到j,犹豫了一下 } if(!vis[j]&&dis[j]>g[u][j]){ dis[j]=g[u][j]; pre[j]=u; } } } return Mst; } void Init(){ for(int i=1;i<=n;i++){ for(int j=i;j<=n;j++){ if(i==j)g[i][j]=0; else g[i][j]=g[j][i]=INF; path[i][j]=path[j][i]=0; used[i][j]=used[j][i]=false; } } } int main(){ int T; int u,v,w; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); Init(); for(int i=1;i<=m;i++){ scanf("%d%d%d",&u,&v,&w); if(g[u][v]>w) g[u][v]=g[v][u]=w; } int Mst=Prim(); int ans1,ans2=INF; ans1=Mst; for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ if(!used[i][j]){ ans2=min(ans2,Mst-path[i][j]+g[i][j]); } } } printf("%d %d\n",ans1,ans2); } return 0; }
相关文章推荐
- UVA 10600 ACM Contest and Blackout 次小生成树/裸
- UVA 10600 - ACM Contest and Blackout(次小生成树)
- UVA10600 ACM Contest and Blackout (次小生成树)
- UVA 10600 ACM Contest and Blackout 次小生成树/裸
- UVa 10600__ACM Contest and Blackout(次小生成树)
- UVA 10600 ACM Contest and Blackout(最小生成树and次小生成树)
- ACM Contest and Blackout UVA - 10600 (次小生成树)
- UVa 10600 ACM contest and Blackout( 次小生成树)
- UVA 10600 - ACM Contest and Blackout(最小生成树&次小生成树)
- UVa10600 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(次小生成树)