The war

Problem Description
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.
 
Input
The input contains several cases. For each cases, the first line contains two positive integers n, m (1<=n<=10000, 0<=m<=100000) standing for the number of the enemy's war zones (numbered from 1 to n), and the number of lines that our enemy has already build.
Then m lines follow. For each line there are three positive integer a, b, c (1<=a, b<=n, 1<=c<=100000), meaning between war zone A and war zone B there is a communication line with the "cost of destroy " c.
 
Output
For each case, if the task can be finished output the minimum cost, or output ‐1.
 
Sample Input
  1. 3 2
  2. 1 2 1
  3. 2 3 2
  4. 4 3
  5. 1 2 1
  6. 1 3 2
  7. 1 4 3
 
Sample Output
  1. -1
  2. 3
  3. Hint
  4. For the second sample input: our enemy may build line 2 to 3, 2 to 4,
  5. 3 to 4. If they build line 2 to 3, we will destroy line 1 to 4, cost 3. If they
  6. build line 2 to 4, we will destroy line 1 to 3, cost 2. If they build line 3 to 4,
  7. we will destroy line 1 to 2, cost 1. So, if we want to make sure that we can
  8. destroy successfully, the minimum cost is 3.
  9.  
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  4007 4010 4003 4008 4009 
 

题目大意:

敌人有n个网站相连,如今你能够破坏一条边使得这些点不相连,可是敌人为了阻止你破坏,提前多建了一条边,问你最坏情况下的最小花费是多少?

解题思路:

參考别人博客:点击打开链接

解题代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. const int maxn=11000;
  9. const int maxm=210000;
  10.  
  11. struct edge{
  12. int u,v,w,next;
  13. edge(int u0=0,int v0=0,int w0=0){
  14. u=u0;v=v0;w=w0;
  15. }
  16. friend bool operator <(edge x,edge y){
  17. return x.w<y.w;
  18. }
  19. }e[maxm];
  20.  
  21. int n,m,cnt,index,head[maxn],dfn[maxn],low[maxn],color[maxn],nc;
  22. vector <int> vec;
  23. bool mark[maxn];
  24. vector <vector <edge> > dfsmap;
  25.  
  26. void addedge(int u,int v,int w){
  27. e[cnt].u=u;e[cnt].v=v;e[cnt].w=w;e[cnt].next=head[u];head[u]=cnt++;
  28. }
  29.  
  30. void input(){
  31. nc=0;
  32. cnt=0;
  33. index=1;
  34. vec.clear();
  35. for(int i=0;i<maxn;i++){
  36. head[i]=-1;
  37. dfn[i]=0;
  38. mark[i]=false;
  39. }
  40. int u,v,w;
  41. for(int i=0;i<m;i++){
  42. scanf("%d%d%d",&u,&v,&w);
  43. addedge(u,v,w);
  44. addedge(v,u,w);
  45. }
  46. }
  47.  
  48. void tarjan(int s,int father){
  49. vec.push_back(s);
  50. dfn[s]=low[s]=index++;
  51. mark[s]=true;
  52. bool flag=true;
  53. for(int i=head[s];i!=-1;i=e[i].next){
  54. int d=e[i].v;
  55. if(d==father && flag){flag=false;continue;}
  56. if(!dfn[d]){
  57. tarjan(d,s);
  58. low[s]=min(low[s],low[d]);
  59. }else if(mark[d]){
  60. low[s]=min(low[s],dfn[d]);
  61. }
  62. }
  63. if(low[s]==dfn[s]){
  64. int d;
  65. nc++;
  66. do{
  67. d=vec.back();
  68. vec.pop_back();
  69. color[d]=nc;
  70. mark[d]=false;
  71. }while(d!=s);
  72. }
  73. }
  74.  
  75. pair <int,int> dfs(int s,int father){
  76. int first=(1<<30),second=(1<<30);
  77. for(int i=0;i<dfsmap[s].size();i++){
  78. int d=dfsmap[s][i].v,w=dfsmap[s][i].w;
  79. if(d==father) continue;
  80. pair <int,int> tmp=dfs(d,s);
  81. if(w<tmp.first) tmp.first=w;
  82. if(tmp.first<first){
  83. second=min(tmp.second,first);
  84. first=tmp.first;
  85. }
  86. else if(tmp.first<second) second=tmp.first;
  87. }
  88. //cout<<s<<"->"<<p1.first<<endl;
  89. return make_pair(first,second);
  90. }
  91.  
  92. void solve(){
  93. for(int i=1;i<=n;i++){
  94. if(!dfn[i]) tarjan(i,-1);
  95. }
  96. dfsmap.clear();
  97. dfsmap.resize(n+4);
  98. edge mine=edge(0,0,(1<<30));
  99. for(int i=0;i<cnt;i++){
  100. int x=color[e[i].u],y=color[e[i].v];
  101. if(x!=y){
  102. dfsmap[x].push_back(edge(x,y,e[i].w));
  103. //cout<<x<<"->"<<y<<":"<<e[i].w<<endl;
  104. if(e[i].w<mine.w) mine=edge(x,y,e[i].w);
  105. }
  106. }
  107. pair <int,int> p1=dfs(mine.u,mine.v);
  108. pair <int,int> p2=dfs(mine.v,mine.u);
  109. int ans=min(p1.second,p2.second);
  110. if(ans>=(1<<30) ) cout<<"-1"<<endl;
  111. else cout<<ans<<endl;
  112. }
  113.  
  114. int main(){
  115. while(scanf("%d%d",&n,&m)!=EOF){
  116. input();
  117. solve();
  118. }
  119. return 0;
  120. }

HDU 4005 The war (图论-tarjan)的更多相关文章

  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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 In the war, the intelligence about the enemy is ...

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

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

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

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

  6. HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. hdu 4738 Caocao's Bridges (tarjan求桥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:给一些点,用一些边把这些点相连,每一条边上有一个权值.现在要你破坏任意一个边(要付出相 ...

  8. HDU 3969 Hawk-and-Chicken(dfs+tarjan缩点优化,网上最详细解析!!!)

    Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. hdu 4005(边双连通)

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

随机推荐

  1. Oracle trunc()函数

    Oracle trunc()函数的用法   --Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysd ...

  2. 【C语言探索之旅】 第三部分第二课:SDL开发游戏之创建窗口和画布

    内容简介 1.第三部分第二课: SDL开发游戏之创建窗口和画布 2.第三部分第三课预告: SDL开发游戏之显示图像 第三部分第二课:SDL开发游戏之创建窗口和画布 在上一课中,我们对SDL这个开源库做 ...

  3. 【C语言探索之旅】 第一部分第四课第一章:变量的世界之内存那档事

    内容简介 1.课程大纲 2.第一部分第四课第一章:变量的世界之内存那档事 3.第一部分第四课第二章预告:变量的世界之声明变量 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答 ...

  4. Sublime Text 3 搭建Go开发环境(Windows)

    一.安装GO 如果已经环境已经配置好,这一步省略.... 1.下载并安装go sdk 2.配置环境变量 (1). 新建 变量名:GOBIN 变量值 :F:\Go\bin (2). 新建 变量名:GOA ...

  5. BZOJ 1096 ZJOI2007 仓库建设 边坡优化

    标题效果:特定n植物,其中一些建筑仓库,有一点使,假设没有仓库仓库向右仓库.最低消费要求 非常easy边坡优化--在此之前刷坡优化的情况下,即使这道题怎么错过 订购f[i]作为i点建设化妆i花费所有安 ...

  6. C和指针 (pointers on C)——第十一章:动态内存分配(下)习题

    1.编写calloc,内部用malloc. void *calloc (size_t n, size_t size) { char * memory; memory =(char*) malloc(n ...

  7. SQL Server中tempdb的管理

    原文:SQL Server中tempdb的管理 资料来自: http://blogs.msdn.com/b/sqlserverstorageengine/archive/tags/tempdb/ ht ...

  8. 物理卷操作命令:pvcreate,pvscan,pvdisplay.卷组操作命令:vgcreate,vgdisplay. (转)

    新硬盘创建LVM系统过程. 物理卷操作命令:pvcreate,pvscan,pvdisplay. 卷组操作命令:vgcreate,vgdisplay. 逻辑卷操作命令:lvcreate,lvdispl ...

  9. 基于最简单的FFmpeg采样读取内存读写:内存玩家

    ===================================================== 基于最简单的FFmpeg样品系列读写内存列表: 最简单的基于FFmpeg的内存读写的样例:内 ...

  10. 王立平--result += &quot;{&quot;;

    result += "{"; 等于:result=result+"{" 字符串连接 x+=1====x=x+1 版权声明:本文博客原创文章,博客,未经同意,不得 ...