UVA 10600 ACM Contest and Blackout(次小树,3级)

F - ACM Contest and Blackout
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Appoint description: 

Description

Download as PDF

 

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<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<algorithm>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mp=109;
const int oo=0x3f3f3f3f;
class Edge
{
 public:int u,v,next,w;
 Edge(){}
 Edge(int _u,int _v,int _w)
 {u=_u;v=_v;w=_w;}
};
class Graph_tree
{
public:
  int head[mp],edge;
  Edge e[mp*mp*2];
  int g[mp][mp];
  void clear()
  {
    clr(head,-1);edge=0;
  }
  void add(int u,int v,int w)
  {
    e[edge].v=v;e[edge].w=w;e[edge].next=head[u];head[u]=edge++;
  }
  int dis[mp],pre[mp],m,n;
  int maxcost[mp][mp];
  bool vis[mp];
  int prim()
  { int u,v;
    FOR(i,0,n)dis[i]=oo,vis[i]=0,pre[i]=-1;
    vis[1]=1;
    for(int i=head[1];~i;i=e[i].next)
    { v=e[i].v;
      if(!vis[v]&&dis[v]>e[i].w)
      {dis[v]=e[i].w;pre[v]=1;
      }
    }
    int MST=0,MAX,best;dis[1]=0;
    FOR(i,0,n)FOR(j,0,n)maxcost[i][j]=0;
    FOR(i,2,n)
    {
      MAX=oo;best=-1;
      FOR(j,1,n)
      if(!vis[j]&&dis[j]<MAX)
        MAX=dis[j],best=j;
      MST+=MAX;vis[best]=1;
//      if(best==8){puts("+++");cout<<maxcost[7][1]<<endl;}
      //for(int j=head[best];~j;j=e[j].next)
      FOR(j,1,n)
      {
        v=j;
        if(vis[v]&&v!=best)///树上最大边
        {
          if(maxcost[v][ pre[best] ]>dis[best])
            maxcost[v][best]=maxcost[best][v]=maxcost[v][ pre[best] ];
          else maxcost[v][best]=maxcost[best][v]=dis[best];
//          printf("max %d %d %d\n",v,best,maxcost[v][best]);
        }
      }
      for(int j=head[best];~j;j=e[j].next)
      {
        v=e[j].v;
        if(!vis[v]&&dis[v]>e[j].w)
          dis[v]=e[j].w,pre[v]=best;
      }
    }
    return MST;
  }
  void getans()
  { int u,v;
    int _MST=oo,MST=prim();
    FOR(i,1,n)
    for(int j=head[i];~j;j=e[j].next)
    {
      v=e[j].v;
      if(pre[v]==i||pre[i]==v){continue;}
//      printf("->%d %d %d\n",i,v,e[j].w);continue;}
//      printf("%d to %d\n",e[j].w,maxcost[v][i]);
      _MST=min(_MST,MST+e[j].w-maxcost[v][i]);
    }
//    cout<<maxcost[1][8]<<endl;
    printf("%d %d\n",MST,_MST);
  }
}sp;
int main()
{
  int cas,n,m,a,b,c;
  while(~scanf("%d",&cas))
  {
    while(cas--)
    { sp.clear();
      scanf("%d%d",&n,&m);
      sp.n=n;sp.m=m;
      FOR(i,1,m)
      {
        scanf("%d%d%d",&a,&b,&c);
        sp.add(a,b,c);sp.add(b,a,c);
        sp.g[a][b]=sp.g[b][a]=c;
      }
      sp.getans();
    }
  }
  return 0;
}




posted @ 2013-10-16 15:21  剑不飞  阅读(219)  评论(0编辑  收藏  举报