题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005

In the war, the intelligence about the enemy is very important. Now, our troop has mastered the situation of the enemy's war zones, and known that these war zones can communicate to each other directly or indirectly through the network. We also know the enemy is going to build a new communication line to strengthen their communication network. Our task is to destroy their communication network, so that some of their war zones can't communicate. Each line has its "cost of destroy". If we want to destroy a line, we must spend the "cost of destroy" of this line. We want to finish this task using the least cost, but our enemy is very clever. Now, we know the network they have already built, but we know nothing about the new line which our enemy is going to build. In this condition, your task is to find the minimum cost that no matter where our enemy builds the new line, you can destroy it using the fixed money. Please give the minimum cost. For efficiency, we can only destroy one communication line. 
 
题意:n个点m条边,无向图,每条边有一个值,如果我们删除这条边,我们需要花费的金额等同这条边上的值。最开始整个图是连通的,现在会加上一条未知的边进去形成新图,我们只能删除一条边,保证新图不连通,求需要的最小金额是多少。
 
解法:
无向图求连通分量。我们首先应该知道,因为加入的边未知,所以我们所求的最小金额就是无论加入的边在图中哪里,我们都可以删除一条边来使它不连通。so我们首先对图求解连通分量,然后缩点,形成一棵树。这时候,我们可以想到,在加入一条未知的边之后会形成一个环,显然最小值的边不是我们的答案,因为这条边有可能就在这个环中,这时候我们就删除不了边了(其他边的值都比这个值大)。
我们仔细想想可以发现:根据题意构造最优路径,一条路径通过以结点u为根的子树时,一定会经过以结点u为根的子树中边权最小的那条边,那么这时候我们除去这条路径后剩下的边权中的最小值就是我们要求的答案了。所以,我们的算法思想就是:递归求解每个结点为根的子树中的最小边和次小边,我们必须保证这里最小边和次小边不可能在一条路径上,然后求次小边中的最小值就是答案了。
 
总结:
连通分量的一道很好的题,想了一天也没有好的解法方法,最后看了别人的代码和讲解才有一些理解和感悟,特别是在保证最小边和次小边不能有机会在一条路径上的解决方法上很巧妙。
 
最后,有两个疑问一直想不通,现在还一直在苦想中。。。
1:图中有重边,这时候我们要么全部保留,要么留下一条边权最大或最小的。这道题,我们需要全部保留,但为什么不是只留下一条边?
2:这道题进行缩点的时候,我用low[]数组判断RE了,好像不是可以用low来判断是否在一个连通块中吗?
图论题做得少,努力补充过程中。。。。图论很有意思,加油!
 
贴上整个代码,一些调试的和多余的函数代码没有删除,仔细理解应该没问题。
 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<vector>
  8. #define inf 0x7fffffff
  9. using namespace std;
  10. const int maxn=+;
  11. const int M = 2e5+;
  12. int n,m;
  13. struct Edge
  14. {
  15. int u,v,cost;
  16. int next;
  17. }edge[M];
  18. int head[maxn],edgenum;
  19. void add(int u,int v,int cost)
  20. {
  21. Edge E={u,v,cost,head[u] };
  22. edge[edgenum]=E;
  23. head[u]=edgenum++;
  24.  
  25. Edge E1={v,u,cost,head[v] };
  26. edge[edgenum]=E1;
  27. head[v]=edgenum++;
  28. }
  29.  
  30. int pre[maxn],low[maxn],dfs_clock,bcc_cnt,index;
  31. int mark[maxn];
  32. vector< vector<Edge> > dfsmap;
  33. vector<int> vec;
  34. int color[maxn];
  35. int dfs(int u,int fa)
  36. {
  37. low[u]=pre[u]= ++dfs_clock;
  38. int flag=;
  39. for (int i=head[u] ;i!=- ;i=edge[i].next)
  40. {
  41. int v=edge[i].v;
  42. if (v==fa && flag) {flag=;continue; }
  43. if (!pre[v])
  44. {
  45. dfs(v,u);
  46. low[u]=min(low[u],low[v]);
  47. }
  48. else if (pre[v]<pre[u])
  49. low[u]=min(low[u],pre[v]);
  50. }
  51. }
  52. void tarjan(int u,int fa){
  53. vec.push_back(u);
  54. pre[u]=low[u]=index++;
  55. mark[u]=true;
  56. bool flag=true;
  57. for(int i=head[u] ;i!=- ;i=edge[i].next){
  58. int d=edge[i].v;
  59. if(d==fa && flag){flag=false;continue;}
  60. if(!pre[d]){
  61. tarjan(d,u);
  62. low[u]=min(low[u],low[d]);
  63. }else {
  64. low[u]=min(low[u],pre[d]);
  65. }
  66. }
  67. if(low[u]==pre[u]){
  68. int d;
  69. bcc_cnt++;
  70. do{
  71. d=vec.back();
  72. vec.pop_back();
  73. color[d]=bcc_cnt;
  74. mark[d]=false;
  75. }while(d!=u);
  76. }
  77. }
  78. void find_bcc()
  79. {
  80. memset(pre,,sizeof(pre));
  81. memset(low,,sizeof(low));
  82. memset(mark,false,sizeof(mark));
  83. vec.clear();
  84. dfs_clock=bcc_cnt=;
  85. index=;
  86. for (int i= ;i<=n ;i++)
  87. if (!pre[i]) tarjan(i,-);
  88. }
  89. int mindistance;
  90. pair<int,int> dfs2(int u,int fa)
  91. {
  92. int first=inf,second=inf;
  93. for (int i= ;i<dfsmap[u].size() ;i++)
  94. {
  95. int v=dfsmap[u][i].v;
  96. int w=dfsmap[u][i].cost;
  97. if (v==fa) continue;
  98. pair<int,int> tmp=dfs2(v,u);
  99. if (tmp.first>w) swap(tmp.first,w);
  100. //if (second>w) second=w;
  101. if (tmp.first<first)
  102. {
  103. second=min(tmp.second,first);
  104. first=tmp.first;
  105. }
  106. else if (tmp.first<second) second=tmp.first;
  107. }
  108. return make_pair(first,second);
  109. }
  110. int main()
  111. {
  112. int a,b,c;
  113. while (scanf("%d%d",&n,&m)!=EOF)
  114. {
  115. memset(head,-,sizeof(head));
  116. edgenum=;
  117. for (int i= ;i<m ;i++)
  118. {
  119. scanf("%d%d%d",&a,&b,&c);
  120. add(a,b,c);
  121. }
  122. find_bcc();
  123. cout<<endl<<bcc_cnt<<endl;
  124. for (int i= ;i<=n ;i++)
  125. cout<<i<<" "<<low[i]<<" "<<color[i]<<endl;
  126. cout<<endl;
  127. dfsmap.resize(n+);
  128. for (int i= ;i<n+ ;i++) dfsmap[i].clear();
  129. int mindist=inf,uu=,vv=;
  130. for (int i= ;i<edgenum ;i++)
  131. {
  132. int u=edge[i].u;
  133. int v=edge[i].v;
  134. int w=edge[i].cost;
  135. int u1=color[u] ,v1=color[v] ;
  136. if (u1 != v1)
  137. {
  138. Edge E={u1,v1,edge[i].cost };
  139. dfsmap[u1].push_back(E);
  140. if (edge[i].cost<mindist)
  141. {
  142. mindist=edge[i].cost;
  143. uu=u1 ;vv=v1 ;
  144. }
  145. }
  146. }
  147. // for (int i=1 ;i<=n ;i++)
  148. // {
  149. // int u=low[i];
  150. // for (int j=head[i] ;j!=-1 ;j=edge[j].next)
  151. // {
  152. // int v=low[edge[j].v ];
  153. // if (u!=v)
  154. // {
  155. // Edge E={u,v,edge[j].cost };
  156. // dfsmap[u].push_back(E);
  157. // if (edge[j].cost<mindist)
  158. // {
  159. // mindist=edge[j].cost;
  160. // uu=u ;vv=v ;
  161. // }
  162. // }
  163. // }
  164. // }
  165. mindistance=inf;
  166. pair<int,int> p1=dfs2(uu,vv);
  167. pair<int,int> p2=dfs2(vv,uu);
  168. //pair<int,int> p1=dfs3(uu,vv);
  169. //pair<int,int> p2=dfs3(vv,uu);
  170. mindistance=min(mindistance,min(p1.second,p2.second));
  171. printf("%d\n",mindistance==inf ? - : mindistance);
  172. }
  173. return ;
  174. }

后续:感谢大牛提出宝贵的意见。。。。

hdu 4005 The war的更多相关文章

  1. HDU 4005 The war(双连通好题)

    HDU 4005 The war pid=4005" target="_blank" style="">题目链接 题意:给一个连通的无向图.每条 ...

  2. HDU 4005 The war Tarjan+dp

    The war Problem Description   In the war, the intelligence about the enemy is very important. Now, o ...

  3. HDU 4005 The war (图论-tarjan)

    The war Problem Description In the war, the intelligence about the enemy is very important. Now, our ...

  4. HDU 4005 The war 双连通分量 缩点

    题意: 有一个边带权的无向图,敌人可以任意在图中加一条边,然后你可以选择删除任意一条边使得图不连通,费用为被删除的边的权值. 求敌人在最优的情况下,使图不连通的最小费用. 分析: 首先求出边双连通分量 ...

  5. HDU 4005 The war(边双连通)

    题意 ​ 给定一张 \(n\) 个点 \(m\) 条边的无向连通图,加入一条边,使得图中权值最小的桥权值最大,如果能使图中没有桥则输出 \(-1\). 思路 ​ 先对原图边双缩点,然后变成了一棵树.在 ...

  6. hdu 4005(边双连通)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 思路:首先考虑边双连通分量,如果我们将双连通分量中的边删除,显然我们无法得到非连通图,因此要缩点 ...

  7. hdu 4005 双联通 2011大连赛区网络赛E *****

    题意: 有一幅图,现在要加一条边,加边之后要你删除一条边,使图不连通,费用为边的费用,要你求的是删除的边的最小值的最大值(每次都可以删除一条边,选最小的删除,这些最小中的最大就为答案) 首先要进行缩点 ...

  8. hdu 4005 边连通度与缩点

    思路:先将图进行缩点,建成一颗树,那么如果这是一条单路径树(即最大点度不超过2),就不在能删的一条边,使得不连通.因为将其头尾相连,形成一个圈,那么删任意一条边,图都是连通的. 上面的是无解的情况,如 ...

  9. HDU 4070 Phage War

    贪心,t 大的放到前面...因为感染所有cell需要的phage的总数是一定的,所以产生phage需要的时间是一定的,只需要考虑用来感染的时间,这样考虑的话,把 t 小的放后面的话,可以发现总时间的最 ...

随机推荐

  1. 关于ASP.net TextBox控件的失去焦点后触发其它事件

    编写人:CC阿爸 2015-2-02 今天在这里,我想与大家一起分享如何处理的ASP.net TextBox控件的失去焦点后触发其它事件的问题,在此做个小结,以供参考.有兴趣的同学,可以一同探讨与学习 ...

  2. LevelDB系列之SSTable(Sorted Strings Table)文件

    SSTable是Bigtable中至关重要的一块,对于LevelDb来说也是如此,对LevelDb的SSTable实现细节的了解也有助于了解Bigtable中一些实现细节. 本节内容主要讲述SSTab ...

  3. 将数组之中的省份市区地区ID改成对用中文字符

    数据表数据源的省市区联动: 原始数据: //获取所有学校信息 $school=D('school'); $info=$school->getList(); 数据如下: 1 => array ...

  4. 图片来自腾讯,未经授权不可引用,js解决方法

    问题记录,解决后来更新 js伪造Referer, 外链图片让用户浏览时,不发送 referer 字段给原网站的方法 A 网站引用了 B 站的 图片 <img src="b_url&qu ...

  5. PHP 下载文件时自动添加bom头的方法

    首先弄清楚,什么是bom头?在Windows下用记事本之类的程序将文本文件保存为UTF-8格式时,记事本会在文件头前面加上几个不可见的字符(EF BB BF),就是所谓的BOM(Byte order ...

  6. Laravel 5 基础(九)- 表单

    首先让我们修改路由,能够增加一个文章的发布. Route::get('articles/create', 'ArticlesController@create'); 然后修改控制器 public fu ...

  7. 清理sql2000查询分析器登录名记录

    注册表 HKEY_CURRENT_USER/Software/Microsoft/Microsoft  SQL  Server/80/Tools/Client/PrefServers

  8. 解决Genymotion下载设备失败的方法(Connection Timeout)

    一直下载不下来,报错. 解决办法: 打开 C:\Users\用户名\AppData\Local\Genymobile目录 打开genymotion.log文件,在里面最下面几行,找到如下日志 [Deb ...

  9. Unicode字符以16进制表示

    int(x [,base ])         将x转换为一个整数 long(x [,base ])        将x转换为一个长整数 float(x )               将x转换到一个 ...

  10. xcode plugin

    http://alcatraz.io/ https://github.com/macoscope/CodePilot prepo  curl -fsSL https://raw.githubuserc ...