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
3 2
1 2 1
2 3 2
4 3
1 2 1
1 3 2
1 4 3
 
Sample Output
-1
3
Hint
For the second sample input: our enemy may build line 2 to 3, 2 to 4, 3 to 4. If they build line 2 to 3, we will destroy line 1 to 4, cost 3. If they build line 2 to 4, we will destroy line 1 to 3, cost 2. If they build line 3 to 4, we will destroy line 1 to 2, cost 1. So, if we want to make sure that we can destroy successfully, the minimum cost is 3.
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  4007 4010 4003 4008 4009 
 

题目大意:

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

解题思路:

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

解题代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std; const int maxn=11000;
const int maxm=210000; struct edge{
int u,v,w,next;
edge(int u0=0,int v0=0,int w0=0){
u=u0;v=v0;w=w0;
}
friend bool operator <(edge x,edge y){
return x.w<y.w;
}
}e[maxm]; int n,m,cnt,index,head[maxn],dfn[maxn],low[maxn],color[maxn],nc;
vector <int> vec;
bool mark[maxn];
vector <vector <edge> > dfsmap; void addedge(int u,int v,int w){
e[cnt].u=u;e[cnt].v=v;e[cnt].w=w;e[cnt].next=head[u];head[u]=cnt++;
} void input(){
nc=0;
cnt=0;
index=1;
vec.clear();
for(int i=0;i<maxn;i++){
head[i]=-1;
dfn[i]=0;
mark[i]=false;
}
int u,v,w;
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
} void tarjan(int s,int father){
vec.push_back(s);
dfn[s]=low[s]=index++;
mark[s]=true;
bool flag=true;
for(int i=head[s];i!=-1;i=e[i].next){
int d=e[i].v;
if(d==father && flag){flag=false;continue;}
if(!dfn[d]){
tarjan(d,s);
low[s]=min(low[s],low[d]);
}else if(mark[d]){
low[s]=min(low[s],dfn[d]);
}
}
if(low[s]==dfn[s]){
int d;
nc++;
do{
d=vec.back();
vec.pop_back();
color[d]=nc;
mark[d]=false;
}while(d!=s);
}
} pair <int,int> dfs(int s,int father){
int first=(1<<30),second=(1<<30);
for(int i=0;i<dfsmap[s].size();i++){
int d=dfsmap[s][i].v,w=dfsmap[s][i].w;
if(d==father) continue;
pair <int,int> tmp=dfs(d,s);
if(w<tmp.first) tmp.first=w;
if(tmp.first<first){
second=min(tmp.second,first);
first=tmp.first;
}
else if(tmp.first<second) second=tmp.first;
}
//cout<<s<<"->"<<p1.first<<endl;
return make_pair(first,second);
} void solve(){
for(int i=1;i<=n;i++){
if(!dfn[i]) tarjan(i,-1);
}
dfsmap.clear();
dfsmap.resize(n+4);
edge mine=edge(0,0,(1<<30));
for(int i=0;i<cnt;i++){
int x=color[e[i].u],y=color[e[i].v];
if(x!=y){
dfsmap[x].push_back(edge(x,y,e[i].w));
//cout<<x<<"->"<<y<<":"<<e[i].w<<endl;
if(e[i].w<mine.w) mine=edge(x,y,e[i].w);
}
}
pair <int,int> p1=dfs(mine.u,mine.v);
pair <int,int> p2=dfs(mine.v,mine.u);
int ans=min(p1.second,p2.second);
if(ans>=(1<<30) ) cout<<"-1"<<endl;
else cout<<ans<<endl;
} int main(){
while(scanf("%d%d",&n,&m)!=EOF){
input();
solve();
}
return 0;
}

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. 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)

    原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factor ...

  2. Lua 数据库访问(转)

    本文主要为大家介绍 Lua 数据库的操作库:LuaSQL.他是开源的,支持的数据库有:ODBC, ADO, Oracle, MySQL, SQLite 和 PostgreSQL. 本文为大家介绍MyS ...

  3. 程序猿常识--OJ系统和ACM测试考试大全

    OJ它是Online Judge缩写系统,来在线检測程序源码的正确性. 著名的OJ有RQNOJ.URAL等. 国内著名的题库有北京大学题库.浙江大学题库等. 国外的题库包含乌拉尔大学.瓦拉杜利德大学题 ...

  4. POJ 2756 Autumn is a Genius 采用string大数减法

    标题意味着小神童.加减可以计算. 只是说这个小神童的学科知识,究竟有多神,自己给自己找. 最后,因为数据是非常非常巨大的,我听说关闭50k结束了50000数字总和,可以想见他神教. 这似乎也是考试题目 ...

  5. Java TCP/UDP网络通信编程

    本文转自:http://www.cnblogs.com/cdtarena/archive/2013/04/10/3012282.html 网络应用中基本上都是TCP(Transmission Cont ...

  6. android 如何将电话簿SDN数字和其他普通的数字混合在一起?

    最初的设计将默认SDN单独分出来,副标题"SDN". 下面的变化可以通过例如实现SDN并安排普通相同数量在一起,按字母顺序排列. DefaultContactListAdapter ...

  7. Ehcache详细解读(转)

    Ehcache 是现在最流行的纯Java开源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从Hibernate的缓存开始的.网上中文的EhCache材料 以简单介绍和配置方法居多,如果你有这方 ...

  8. 你所不了解的float(滥用float的怪异现象) (转)

    阅读目录 float设计初衷就是为了实现文字环绕效果 如何解决浮动造成的父容器塌陷? 兼容各浏览器清除浮动的通用方式 滥用浮动 运用浮动的一些特性 浮动与布局 浮动与单侧固定布局 浮动与智能自适应的流 ...

  9. sqlserver缓存程序-只能使用一次清除缓存计划

    plan cache非常大.将仅仅使用一次的缓存计划清除,而不用清除整个cache. declare @sid varbinary(64) declare cur01 cursor for selec ...

  10. MyEclipse调整项目的顺序

    MyEclipse该项目是按照字母顺序排列的项目名称,无法调整. 例,我现在做Photo工程项目,向下位置,非常不方便: 可是,它有一个将项目分组的功能"Working Sets" ...