hdu 4005 The war
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = 2e5+;
int n,m;
struct Edge
{
int u,v,cost;
int next;
}edge[M];
int head[maxn],edgenum;
void add(int u,int v,int cost)
{
Edge E={u,v,cost,head[u] };
edge[edgenum]=E;
head[u]=edgenum++; Edge E1={v,u,cost,head[v] };
edge[edgenum]=E1;
head[v]=edgenum++;
} int pre[maxn],low[maxn],dfs_clock,bcc_cnt,index;
int mark[maxn];
vector< vector<Edge> > dfsmap;
vector<int> vec;
int color[maxn];
int dfs(int u,int fa)
{
low[u]=pre[u]= ++dfs_clock;
int flag=;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (v==fa && flag) {flag=;continue; }
if (!pre[v])
{
dfs(v,u);
low[u]=min(low[u],low[v]);
}
else if (pre[v]<pre[u])
low[u]=min(low[u],pre[v]);
}
}
void tarjan(int u,int fa){
vec.push_back(u);
pre[u]=low[u]=index++;
mark[u]=true;
bool flag=true;
for(int i=head[u] ;i!=- ;i=edge[i].next){
int d=edge[i].v;
if(d==fa && flag){flag=false;continue;}
if(!pre[d]){
tarjan(d,u);
low[u]=min(low[u],low[d]);
}else {
low[u]=min(low[u],pre[d]);
}
}
if(low[u]==pre[u]){
int d;
bcc_cnt++;
do{
d=vec.back();
vec.pop_back();
color[d]=bcc_cnt;
mark[d]=false;
}while(d!=u);
}
}
void find_bcc()
{
memset(pre,,sizeof(pre));
memset(low,,sizeof(low));
memset(mark,false,sizeof(mark));
vec.clear();
dfs_clock=bcc_cnt=;
index=;
for (int i= ;i<=n ;i++)
if (!pre[i]) tarjan(i,-);
}
int mindistance;
pair<int,int> dfs2(int u,int fa)
{
int first=inf,second=inf;
for (int i= ;i<dfsmap[u].size() ;i++)
{
int v=dfsmap[u][i].v;
int w=dfsmap[u][i].cost;
if (v==fa) continue;
pair<int,int> tmp=dfs2(v,u);
if (tmp.first>w) swap(tmp.first,w);
//if (second>w) second=w;
if (tmp.first<first)
{
second=min(tmp.second,first);
first=tmp.first;
}
else if (tmp.first<second) second=tmp.first;
}
return make_pair(first,second);
}
int main()
{
int a,b,c;
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-,sizeof(head));
edgenum=;
for (int i= ;i<m ;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
find_bcc();
cout<<endl<<bcc_cnt<<endl;
for (int i= ;i<=n ;i++)
cout<<i<<" "<<low[i]<<" "<<color[i]<<endl;
cout<<endl;
dfsmap.resize(n+);
for (int i= ;i<n+ ;i++) dfsmap[i].clear();
int mindist=inf,uu=,vv=;
for (int i= ;i<edgenum ;i++)
{
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].cost;
int u1=color[u] ,v1=color[v] ;
if (u1 != v1)
{
Edge E={u1,v1,edge[i].cost };
dfsmap[u1].push_back(E);
if (edge[i].cost<mindist)
{
mindist=edge[i].cost;
uu=u1 ;vv=v1 ;
}
}
}
// for (int i=1 ;i<=n ;i++)
// {
// int u=low[i];
// for (int j=head[i] ;j!=-1 ;j=edge[j].next)
// {
// int v=low[edge[j].v ];
// if (u!=v)
// {
// Edge E={u,v,edge[j].cost };
// dfsmap[u].push_back(E);
// if (edge[j].cost<mindist)
// {
// mindist=edge[j].cost;
// uu=u ;vv=v ;
// }
// }
// }
// }
mindistance=inf;
pair<int,int> p1=dfs2(uu,vv);
pair<int,int> p2=dfs2(vv,uu);
//pair<int,int> p1=dfs3(uu,vv);
//pair<int,int> p2=dfs3(vv,uu);
mindistance=min(mindistance,min(p1.second,p2.second));
printf("%d\n",mindistance==inf ? - : mindistance);
}
return ;
}
后续:感谢大牛提出宝贵的意见。。。。
hdu 4005 The war的更多相关文章
- HDU 4005 The war(双连通好题)
HDU 4005 The war pid=4005" target="_blank" style="">题目链接 题意:给一个连通的无向图.每条 ...
- HDU 4005 The war Tarjan+dp
The war Problem Description In the war, the intelligence about the enemy is very important. Now, o ...
- HDU 4005 The war (图论-tarjan)
The war Problem Description In the war, the intelligence about the enemy is very important. Now, our ...
- HDU 4005 The war 双连通分量 缩点
题意: 有一个边带权的无向图,敌人可以任意在图中加一条边,然后你可以选择删除任意一条边使得图不连通,费用为被删除的边的权值. 求敌人在最优的情况下,使图不连通的最小费用. 分析: 首先求出边双连通分量 ...
- HDU 4005 The war(边双连通)
题意 给定一张 \(n\) 个点 \(m\) 条边的无向连通图,加入一条边,使得图中权值最小的桥权值最大,如果能使图中没有桥则输出 \(-1\). 思路 先对原图边双缩点,然后变成了一棵树.在 ...
- hdu 4005(边双连通)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 思路:首先考虑边双连通分量,如果我们将双连通分量中的边删除,显然我们无法得到非连通图,因此要缩点 ...
- hdu 4005 双联通 2011大连赛区网络赛E *****
题意: 有一幅图,现在要加一条边,加边之后要你删除一条边,使图不连通,费用为边的费用,要你求的是删除的边的最小值的最大值(每次都可以删除一条边,选最小的删除,这些最小中的最大就为答案) 首先要进行缩点 ...
- hdu 4005 边连通度与缩点
思路:先将图进行缩点,建成一颗树,那么如果这是一条单路径树(即最大点度不超过2),就不在能删的一条边,使得不连通.因为将其头尾相连,形成一个圈,那么删任意一条边,图都是连通的. 上面的是无解的情况,如 ...
- HDU 4070 Phage War
贪心,t 大的放到前面...因为感染所有cell需要的phage的总数是一定的,所以产生phage需要的时间是一定的,只需要考虑用来感染的时间,这样考虑的话,把 t 小的放后面的话,可以发现总时间的最 ...
随机推荐
- Silverlight 独立存储(IsolatedStorageFile)
1.在Web中添加天气服务引用地址 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 2.在Web中添加Wcf服务接口I ...
- C# 平时碰见的问题【1】
1. SqlBulkCopy 可以利用这个类实现快速大批量新增数据的效果, 但在使用过程中发现了一个问题: 无法将数据源中的DateTime类型转换成数据库中的int类型 看起来就是数据列不对应导致的 ...
- 生产WCF客户端类文件的命令格式
生产WCF客户端类文件的命令格式: svcutil.exe net.tcp://127.0.0.1:8732/ChromaMI.Remote.ConfigService/RemoteConfigSer ...
- vim之旅
本人是今年的毕业生, 大学很莫名的选择了一个电子商务专业. 由于专业没有实质性的东西可学,加上对电商不敢兴趣, 于是乎我有了大量的时间在宿舍里折腾电脑. 折腾了几年大三决定转业, 大四在还没找工作之前 ...
- Moses manual 中Basline System 2.3.4节用IRSTLM创建语言模型的命令有误
手册里写到: ~/irstlm/bin/compile-lm \ --text yes \ news-commentary-v8.fr-en.lm.en.gz \ news-commentary-v8 ...
- WPF中线性渐变画刷的一个小窍门
最近被项目里面控件的设计搞的死去活来的,大部分的设计都会需要使用进度条的功能,因为UI形状的变态,使用ProgressBar不能满足需求,没办法就自己想办法实现进度显示.折腾的多了发现一个很不错的方法 ...
- 利用JQ实现的,高仿 彩虹岛官网导航栏(学习HTML过程中的小记录)
利用JQ实现的,高仿 彩虹岛官网导航栏(学习HTML过程中的小记录) 作者:王可利(Star·星星) 总结: 今天学习的jQ类库的使用,代码重复的比较多需要完善.严格区分大小写,在 $(" ...
- 创建表 添加主键 添加列常用SQL语句
--删除主键 alter table 表名 drop constraint 主键名--添加主键alter table 表名 add constraint 主键名 primary key(字段名1,字段 ...
- poj 2046 Gap
题目连接 http://poj.org/problem?id=2046 Gap Description Let's play a card game called Gap. You have 28 c ...
- android bluetooth UUID蓝牙查询表
ServiceDiscoveryServerServiceClassID_UUID = '{00001000-0000-1000-8000-00805F9B34FB}' BrowseGroupDesc ...