其他分享
首页 > 其他分享> > URAL 1416. Confidential

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 aibi the numbers of the planets that are connected with some passage (1 ≤ aibi ≤ 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

inputoutput
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算法求最小和次小生成树; 并查集时,用非递归形式,否则超时 ***************************************************************************************
  1 #include<iostream>
  2 #include<string>
  3 #include<cstring>
  4 #include<queue>
  5 #include<cstdio>
  6 #include<algorithm>
  7 #include<cctype>
  8 using namespace std;
  9 const int inf=1<<30;
 10 struct  node
 11  {
 12      int u,v,w;
 13      int used;//标记改变是否在最小生成树中
 14      int del;//标记是否取该边
 15  }e[260010];
 16  int fa[1001];
 17  int n,m,i,j,k;
 18  bool  gs_first;
 19  int find(int x)//非递归的
 20   {
 21       int s;
 22       for(s=x;fa[s]>=0;s=fa[s]);
 23       while(s!=x)
 24        {
 25            int t=fa[x];
 26            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)//枚举求次小生成树
 96               {
 97                   e[i].del=1;
 98                   initset();
 99                   w2=kruskar();
100                   if(w2<min&&w2!=-1)
101                    min=w2;
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

坚持!!!!!!!!!!

转载于:https://www.cnblogs.com/sdau--codeants/p/3298367.html

标签:Confidential,int,possible,cost,Zaphod,URAL,include,any,1416
来源: https://blog.csdn.net/weixin_34072637/article/details/93727321