当前位置:   article > 正文

1569. Networking the “Iset”

"ural 1569 networking the \"iset"

http://acm.timus.ru/problem.aspx?space=1&num=1569

枚举每一个点为起点 进行广搜 比较每次搜到的树  选择最优的一种结果

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<string>
  5. #include<map>
  6. #include<vector>
  7. #include<stack>
  8. #include<set>
  9. #include<map>
  10. #include<queue>
  11. #include<algorithm>
  12. #include<cmath>
  13. #define LL long long
  14. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  15. using namespace std;
  16. const int INF=0x3f3f3f3f;
  17. const int N=205;
  18. const int M=200005;
  19. int l[M],r[M];
  20. bool instal[M],tmp[M];
  21. int head[N],I;
  22. struct node
  23. {
  24. int j,next;
  25. int k;
  26. }edge[M];
  27. int dist[N];
  28. bool final[N];
  29. int D;
  30. int n,m;
  31. void add(int i,int j,int k)
  32. {
  33. edge[I].j=j;
  34. edge[I].k=k;
  35. edge[I].next=head[i];
  36. head[i]=I++;
  37. }
  38. void bfs(int x1)
  39. {
  40. memset(final,true,sizeof(final));
  41. memset(dist,-1,sizeof(dist));
  42. memset(tmp,false,sizeof(tmp));
  43. queue<int>qt;
  44. qt.push(x1);
  45. dist[x1]=0;
  46. while(!qt.empty())
  47. {
  48. int x=qt.front();
  49. qt.pop();
  50. for(int t=head[x];t!=-1;t=edge[t].next)
  51. {
  52. int j=edge[t].j;
  53. if(dist[j]==-1)
  54. {
  55. final[x]=false;
  56. dist[j]=dist[x]+1;
  57. //cout<<edge[t].k<<endl;
  58. tmp[edge[t].k]=true;
  59. qt.push(j);
  60. }
  61. }
  62. }
  63. int k1=-1,k2=-1;
  64. for(int i=1;i<=n;++i)
  65. if(final[i])
  66. {
  67. if(k2==-1||dist[i]>dist[k2])
  68. k2=i;
  69. if(k1==-1||dist[k2]>dist[k1])
  70. swap(k1,k2);
  71. }
  72. int d=0;
  73. if(k1!=-1)
  74. d+=dist[k1];
  75. if(k2!=-1)
  76. d+=dist[k2];
  77. if(D==-1||d<D)
  78. {//cout<<d<<" "<<D<<endl;
  79. D=d;
  80. for(int i=1;i<=m;++i)
  81. instal[i]=tmp[i];
  82. }
  83. }
  84. int main()
  85. {
  86. //freopen("data.in","r",stdin);
  87. while(cin>>n>>m)
  88. {
  89. memset(head,-1,sizeof(head));
  90. I=0;
  91. D=-1;
  92. for(int i=1;i<=m;++i)
  93. {
  94. cin>>l[i]>>r[i];
  95. add(l[i],r[i],i);
  96. add(r[i],l[i],i);
  97. }
  98. for(int i=1;i<=n;++i)
  99. bfs(i);
  100. for(int i=1;i<=m;++i)
  101. if(instal[i])
  102. cout<<l[i]<<" "<<r[i]<<endl;
  103. }
  104. return 0;
  105. }

  

转载于:https://www.cnblogs.com/liulangye/archive/2013/01/26/2878175.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/544645
推荐阅读
相关标签
  

闽ICP备14008679号