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 小的放后面的话,可以发现总时间的最 ...
随机推荐
- ElasticSearch 模板文件配置
首先是推荐一下参考资料 中文资料:http://kibana.logstash.es/content/elasticsearch/index.html 官方文档:https://www.elastic ...
- 学习c编程的第三天
#include<stdio.h> int add(int,int); int main(){ int x=add(1,2); printf("%d",x); retu ...
- 当数据0跟if判断冲突的时候
我是很无奈的,以后都要2,3,4,5这样去标志状态: 分配状态:<select name="is_send" > <option selected="s ...
- 分享:PHP获取MAC地址的实现代码
原文地址:http://www.jbxue.com/article/12635.html发布:thatboy 来源:Net [大 中 小] 分享一例php取得机器mac地址的代码,学习下p ...
- jquery实现radio按纽全不选和checkbox全选的实例
用jquery实现以下两个这个功能: 1.对所有单选按纽中radio全不选 单选按纽:<input type="radio" name="f1">A ...
- C# 多线程操作样例
using System; using System.Threading; //引用多线程 namespace ThreadTest { public class Alpha { public voi ...
- ios 总结
1 ocoa Touch Layer{ App Extensions https://developer.apple.com/library/ios/documentation/General/Con ...
- 《大话设计模式》ruby版代码:策略模式
需求: 商场收银软件,根据客户购买物品的单价和数量,计算费用,会有促销活动,打八折,满三百减一百之类的. 一,使用工厂模式. # -*- encoding: utf-8 -*- #现金收费抽象类 cl ...
- android开发系列之代码整洁之道
说起代码整洁之道,想必大家想到更多的是那本经典重构书籍.没错,记得当时自己读那本书的时候,一边结合项目实战,一边结合书中的讲解,确实学到了很多东西,对我自己的编码风格影响极深.随着时间的流逝,书中很多 ...
- Ubuntu 修改用户密码与启动root账号
passwd sban 修改当前帐号 sudo passwd root 修改root帐号 修改/etc/ssh/sshd_config,改: PermitRootLogin without-passw ...