Source code for submission s1261

spider.cpp

  1. #include <cstdio>
  2. #include <string>
  3. #include <set>
  4. #include <vector>
  5. #include <queue>
  6. #include <utility>
  7. #include <map>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. class mycmp{
  13. public:
  14. bool operator()( const pair<int,int> & lhs, const pair<int,int> & rhs ) const{
  15. return (lhs.first-rhs.first)>0;
  16. }
  17. };
  18.  
  19. int main(void){
  20. int nodeNO, linkNO;
  21. while (scanf("%d %d", &nodeNO, &linkNO)==2){
  22. int len =0;
  23. set<int> Set;
  24. set<int> Set2;
  25. multimap<int, pair<int,int> > mmp;
  26. priority_queue<pair<int,int>, vector<pair<int,int> >, mycmp> pq; ////
  27. pair<int,pair<int,int> > max = make_pair(0, make_pair(0,0));
  28.  
  29. for (int i=0; i < linkNO; i++){
  30. int u,v,l;
  31. scanf("%d %d %d",&u, &v,&l);
  32. pair<int,pair<int,int> > tmp = make_pair(l, make_pair(u,v));
  33. Set2.insert(u);
  34. Set2.insert(v);
  35. if (l>max.first){
  36. if (max.first>0)
  37. {
  38. mmp.insert(make_pair(max.second.second,make_pair(max.first,max.second.first)));
  39. mmp.insert(make_pair(max.second.first,make_pair(max.first,max.second.second)));
  40. }
  41. max = tmp;
  42. }else {
  43. mmp.insert(make_pair(v,make_pair(l,u)));
  44. mmp.insert(make_pair(u,make_pair(l,v)));
  45. }
  46. }
  47. Set.insert(max.second.first);
  48. Set.insert(max.second.second);
  49. len -= max.first;
  50. multimap<int,pair<int,int> >::iterator it;
  51. while ((it = mmp.find(max.second.first))!=mmp.end()){
  52. // printf("it+=<%d,%d> \n", it->second.first, it->second.second);
  53. pq.push(it->second);
  54. mmp.erase(it);
  55. }
  56.  
  57. while ((it = mmp.find(max.second.second))!=mmp.end()){
  58. // printf("it*=<%d,%d> \n", it->second.first, it->second.second);
  59. pq.push(it->second);
  60. mmp.erase(it);
  61. }
  62.  
  63. while (!pq.empty()){
  64. pair<int,int> tm = pq.top();
  65. pq.pop();
  66. // printf("tm=<%d,%d> \n",tm.first, tm.second);
  67. if (Set.find(tm.second) == Set.end()){
  68. // printf("len = %d ; new len = %d; tm = %d\n", len , len +tm.first, tm.first);
  69. len += tm.first;
  70. Set.insert(tm.second);
  71. while ((it = mmp.find(tm.second))!=mmp.end()){
  72. // printf("it=<%d,%d> \n", it->second.first, it->second.second);
  73.  
  74. pq.push(it->second);
  75. mmp.erase(it);
  76. }
  77. }
  78. }
  79. if (!mmp.empty() || ((int)Set2.size())< nodeNO)
  80. printf("disconnected\n");
  81. else
  82. printf("%d\n",len);
  83. }
  84.  
  85. return 0;
  86. }
  87.