URAL 1416. Confidential

1416. Confidential

Time limit: 2.0 second Memory limit: 64 MB
Zaphod Beeblebrox — President of the Imperial Galactic Government. And by chance he is an owner of enterprises that trade in secondhand pens. This is a complicated highly protable and highly competitive business. If you want to stay a leader you are to minimize your expenses all the time. And the presedent's high post helps in those aairs. But he is to keep this business in secret. As a president Zaphod has access to the top secret and important information an exact value of power loss in the hyperspace transition between the planets. Of course, this information is very useful to his company. Zaphod is to choose the minimal possible set of trans-planet passages so that he could pass from any planet to any other one via those passages and their total cost would be minimal. The task won't be complicated if Zaphod was not to keep in secret that he helps his company with the secret information. Thus, Zaphod decided to find not the cheapest passages set but the next one. As a real businessman he wants to estimate the value of his conspiracy expenses.

Input

The first input line contains two integers:   N  (2 ≤   N  ≤ 500) is a number of planets in the Galaxy and   M  is an amount of possible transitions. The next   M  lines contain three integers   ai,   bi  the numbers of the planets that are connected with some passage (1 ≤   ai,   bi    N), and   wi  (0 ≤   wi  ≤ 1000) is the transition cost. If an   A  to   B  transition is possible then a   B  to   A  transition is possible, too. The cost of those transitions are equal. There is not more than one passage between any two planets. One can reach any planet from any other planet via some chain of these passages.

Output

You should find two different sets of transitions with the minimal possible cost and output theirs costs. Print the minimal possible cost first. If any of those sets of transitions does not exist denote it's cost by −1.

Samples

input output
4 6
1 2 2
2 3 2
3 4 2
4 1 2
1 3 1
2 4 1
Cost: 4
Cost: 4
3 2
1 2 2
2 3 2
Cost: 4
Cost: -1
Problem Author: Den Raskovalov Problem Source: The Ural State University Championship, October 29,
***************************************************************************************
kruskal for the Minimum spanning tree and the next smallest;
And when the check collection, non-recursive form, otherwise a timeout
***************************************************************************************
  . 1 #include <the iostream>
   2 #include < String >
   . 3 #include <CString>
   . 4 #include <Queue>
   . 5 #include <cstdio>
   . 6 #include <algorithm>
   . 7 #include <the cctype>
   . 8  the using  namespace STD;
   . 9  const  int = INF . 1 << 30 ;
 10  struct   Node
 . 11  {
 12 is       int U, V, W;
 13 is       int Used; // flag whether the change in the minimum spanning tree 
14       int del; //Tag is to take the edge 
15   } E [ 260.01 thousand ];
 16   int FA [ 1001 ];
 . 17   int n-, m, I, J, K;
 18 is   BOOL   gs_first;
 . 19   int Find ( int X) // nonrecursive 
20    {
 21        int S;
 22 is        for (S = X; FA [S]> = 0 ; S = FA [S]);
 23 is        the while (S =! X)
 24         {
 25             int T = FA [X];
 26 is             FA [X] = S;
 27            x=t;
 28        }
 29        return s;
 30   }
 31   void  Unon(int x,int y)
 32    {
 33        int x1=find(x);
 34        int y1=find(y);
 35        if(x1!=y1)
 36         fa[x1]=y1;
 37    }
 38    bool  cmp(node a,node b)
 39       {
 40           return a.w<b.w;
 41       }
 42    void  initset()
 43     {
 44         for(int it=1;it<=n;it++)
 45          fa[it]=-1;
 46     }
 47    void  init()
 48     {
 49         cin>>n>>m;
 50         initset();
 51         for(i=0;i<m;i++)
 52         {
 53             cin>>e[i].u>>e[i].v>>e[i].w;
 54             e[i].del=0;
 55             e[i].used=0;
 56         }
 57         sort(e,e+m,cmp);
 58     }
 59     int kruskar()
 60      {
 61          int num=0;
 62          int sum=0;
 63          for(int it=0;it<m;it++)
 64           {
 65               if(e[it].del==1)
 66                continue;
 67               int u=e[it].u;
 68               int v=e[it].v;
 69               if(find(u)!=find(v))
 70                {
 71                  num++;
 72                  sum+=e[it].w;
 73                  Unon(u,v);
 74                  if(gs_first)
 75                   e[it].used=1;
 76                }
 77 
 78                if(num>=n-1)
 79                 break;
 80           }
 81           if(num<n-1)
 82            return -1;
 83           return sum;
 84      }
 85      int main()
 86      {
 87          init();
 88          gs_first=true;
 89          int w1=kruskar();
 90          gs_first=false;
 91          initset();
 92          int w2,min=inf;
 93          for(i=0;i<m;i++)
 94           {
 95              if(e[i].used==1) // Enumeration seek times smaller spanning 
96                {
 97                    E [I] = .del . 1 ;
 98                    initset ();
 99                    w2 of = kruskar ();
 100                    IF ! (W2 of <w2 of && min = - . 1 )
 101                     min = w2 of;
 102                    E [I] .del = 0 ;
 103                }
 104            }
 105            COUT << " Cost: " << W1 << endl;
 106            IF (min == INF)
 107            cout<<"Cost: "<<"-1"<<endl;
108           else
109            cout<<"Cost: "<<min<<endl;
110            return 0;
111      }
View Code

adhere to! ! ! ! ! ! ! ! ! !

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3298367.html

Guess you like

Origin blog.csdn.net/weixin_34072637/article/details/93727321