hdoj 4738 Caocao's Bridges【双连通分量求桥】
Caocao's Bridges
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3000 Accepted Submission(s):
953
battle of Chibi. But he wouldn't give up. Caocao's army still was not good at
water battles, so he came up with another idea. He built many islands in the
Changjiang river, and based on those islands, Caocao's army could easily attack
Zhou Yu's troop. Caocao also built bridges connecting islands. If all islands
were connected by bridges, Caocao's army could be deployed very conveniently
among those islands. Zhou Yu couldn't stand with that, so he wanted to destroy
some Caocao's bridges so one or more islands would be seperated from other
islands. But Zhou Yu had only one bomb which was left by Zhuge Liang, so he
could only destroy one bridge. Zhou Yu must send someone carrying the bomb to
destroy the bridge. There might be guards on bridges. The soldier number of the
bombing team couldn't be less than the guard number of a bridge, or the mission
would fail. Please figure out as least how many soldiers Zhou Yu have to sent to
complete the island seperating mission.
In each
test case:
The first line contains two integers, N and M, meaning that
there are N islands and M bridges. All the islands are numbered from 1 to N. ( 2
<= N <= 1000, 0 < M <= N2 )
Next M lines describes
M bridges. Each line contains three integers U,V and W, meaning that there is a
bridge connecting island U and island V, and there are W guards on that bridge.
( U ≠ V and 0 <= W <= 10,000 )
The input ends with N = 0 and M =
0.
Zhou Yu had to send to complete the mission. If Zhou Yu couldn't succeed any
way, print -1 instead.
#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
#include<vector>
#define MAX 1010
#define MAXM 1000100
#define INF 0x7ffffff
using namespace std;
int n,m,mark;//mark记录图是否联通
int head[MAX],ans;
int low[MAX],dfn[MAX];
int dfsclock,dcccnt;
struct node
{
int beg,end,val,next;
int cnt;//记录桥是否存在
}edge[MAXM];
void init()
{
ans=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[ans].beg=u;
edge[ans].end=v;
edge[ans].val=w;
edge[ans].cnt=0;//初始化为0表示没有桥
edge[ans].next=head[u];
head[u]=ans++;
}
void getmap()
{
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
}
void tarjan(int u,int fa)
{
int v;
low[u]=dfn[u]=++dfsclock;
int flag=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].end;
if(flag&&v==fa)//判断重边
{
flag=0;
continue;
}
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v])//是桥
edge[i].cnt=edge[i^1].cnt=1; //标记这条边是桥edge[i^1].cnt意思是这条边的反向边
}
else
low[u]=min(low[u],dfn[v]);
}
}
void find()
{
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
dfsclock=0;
tarjan(1,-1);
mark=1;
for(int i=1;i<=n;i++)//遍历所有点
{
if(!dfn[i])//如果点没被搜索到证明这个图不连通
{
mark=0;
return ;
}
}
}
void solve()
{
if(!mark)//图不连通 不需要派人 直接输出0
printf("0\n");
else
{
int ant=INF;
for(int i=0;i<ans;i++)
{
if(edge[i].cnt)
ant=min(ant,edge[i].val);//寻找人数最少把守的桥
}
if(ant==INF)//如果不能阻断各岛屿之间的联系
ant=-1;
if(ant==0)//如果桥上的人数为0 派一个人
ant=1;
printf("%d\n",ant);
}
}
int main()
{
while(scanf("%d%d",&n,&m),n|m)
{
init();
getmap();
find();
solve();
}
return 0;
}
hdoj 4738 Caocao's Bridges【双连通分量求桥】的更多相关文章
- 2013杭州网赛 1001 hdu 4738 Caocao's Bridges(双连通分量割边/桥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥 ...
- hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- HDU 4738 Caocao's Bridges(Tarjan求桥+重边判断)
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...
- 【HDU4612】 双连通分量求桥
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 题目大意:给你一个无向图,问你加一条边后最少还剩下多少多少割边. 解题思路:好水的一道模板题.先 ...
- hdoj 3849 By Recognizing These Guys, We Find Social Networks Useful【双连通分量求桥&&输出桥&&字符串处理】
By Recognizing These Guys, We Find Social Networks Useful Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 4738 Caocao's Bridges taijan (求割边,神坑)
神坑题.这题的坑点有1.判断连通,2.有重边,3.至少要有一个人背*** 因为有重边,tarjan的时候不能用子结点和父节点来判断是不是树边的二次访问,所以我的采用用前向星存边编号的奇偶性关系,用^1 ...
- 【HDU 4738 Caocao's Bridges】BCC 找桥
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:给定一个n个节点m条边的无向图(可能不连通.有重边),每条边有一个权值.判断其连通性,若双 ...
- Hdu 4738 Caocao's Bridges (连通图+桥)
题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这 ...
随机推荐
- sed工具使用
sed命令使用形式 1.sed命令从管道中读取数据处理 command | sed ' edit command' 通过管道把一个命令的标准输出读入到sed的标准输入,sed就起到了过滤作用 2.se ...
- php hook 之简单例子
<?php// 应用单例模式// 建立相应的 plugins 文件夹,并建立 .php 文件放在里面class plugin{ public $actions; public $fi ...
- 『奇葩问题集锦』function * (next){ 执行报错 SyntaxError: Unexpected token *
这是因为 app.use(function * (){ 语句中有一个 * ,这种方式被称为generator functions ,一般写作function *(){...} 的形式,在此类func ...
- ThinkPHP下使用Ueditor
在做课程设计的时候想到用百度的Ueditor,可在配置的时候出现了一些问题 Ueditor感觉不是很难,以前有个人定制的,现在取消了这项服务,但是我们可以自己进行配置 下载地址:http://uedi ...
- zzuli oj 1145 有问题的里程表 2
Description 某辆汽车有一个里程表,该里程表可以显示一个整数,为该车走过的公里数.然而这个里程表有个毛病:它总是从3变到5,而跳过数字4,里程表所有位(个位. 十位.百位等)上的数字都是如此 ...
- cursor_sharing
CURSOR_SHARING Property Description Parameter type String Syntax CURSOR_SHARING = { EXACT | FORCE } ...
- linux下测试磁盘的读写IO速度【转】
Reference1:http://server.chinabyte.com/495/12361995.shtmlReference2:https://www.deleak.com/blog/2011 ...
- 在vmware 6.5+ubuntu12.04上安装VMware tools出现问题的分析
笔者已经写了一篇关于安装"VMware Tools",以实现文件共享的文章,那篇文章对于你实现共享操作是足够了, 所以,倘若你赶时间不如直接去在虚拟机的linux中利用VMware ...
- MINA源码阅读之ACP
Processor在XXAcceptor以及XXConnector中所扮演的只能就是:作为Acceptor以及Connetor所创建的Session的Processor: IoAcceptor作为他所 ...
- iOS开发 - NSBundle, NSDevice, NSLocale
iOS的APP的应用开发的过程中,有时为了bug跟踪或者获取用反馈的需要自动收集用户设备.系统信息.应用信息等等,这些信息方便开发者诊断问题,当然这些信息是用户的非隐私信息,是通过开发api可以获取到 ...