Caocao's Bridges

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3992    Accepted Submission(s): 1250

Problem Description
Caocao was defeated by Zhuge Liang and Zhou Yu in the 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.
 
Input
There are no more than 12 test cases.

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.

 
Output
For each test case, print the minimum soldier number Zhou Yu had to send to complete the mission. If Zhou Yu couldn't succeed any way, print -1 instead.
 
Sample Input
3 3
1 2 7
2 3 4
3 1 4
 
3 2
1 2 7
2 3 4
 
0 0
 
Sample Output
-1
4

题目链接:HDU 4738

题目不难但有三个坑点:1、两点之间可能有重边;2、初始状态的图已经不是连通的了;3、某个桥就算没人守也至少派要一个人去炸桥

这题的重边可以有两种处理方式,很容易想到简单方法就是多用一个邻接矩阵来记录两点之间守卫人数,在加边的时候就把重边判断掉使其权值变成INF,这样就算有桥也是拿INF这个权值去更新答案

第二种就是给每一条边一个id,然后判断是否往回走就不是用v==pre而是id==E[i].id,这样一来若有重边则肯定存在另外至少一个不同id的边可以进行dfs把pre(指向u的后向边)的low更新掉,导致low[v]不会大于dfn[u],就构不成桥的条件了,解决了重边的问题。

例如这样的数据

2 2

1 2 2

1 2 2

首先是dfs从1->2,把1的dfn更新,发现2没访问过,开始从2递归即2->1,遍历发现2->1这条边和1->2这走过来的边id一样,continue,又发现一条2->1的但id不一样的,但是此时的dfn[1]已经有值且1在stack中,则$low[2]=min(low[2],dfn[1])=1$,此时$low[2]$便不会大于$dfn[1]$

感觉第二种方法比较好用且适用性好,学习一个

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1007;
struct edge
{
int to;
int pre;
int id;
int w;
};
edge E[N*N];
int head[N],tot;
//int soldier[N][N];//第一种方法所需的的邻接矩阵
int low[N],dfn[N],ts,top,st[N],ins[N];
int least; void init()
{
CLR(head,-1);
tot=0;
//CLR(soldier,INF);
CLR(low,0);
CLR(dfn,0);
ts=top=0;
CLR(ins,0);
least=INF;
}
inline void add(int s,int t,int w,int id)
{
E[tot].to=t;
E[tot].id=id;
E[tot].w=w;
E[tot].pre=head[s];
head[s]=tot++;
}
void Tarjan(int u,int id)
{
low[u]=dfn[u]=++ts;
ins[u]=1;
st[top++]=u;
int v;
for (int i=head[u]; ~i; i=E[i].pre)
{
v=E[i].to;
if(id==E[i].id)
continue;
if(!dfn[v])
{
Tarjan(v,E[i].id);
low[u]=min<int>(low[v],low[u]);
if(low[v]>dfn[u])
{
int need=E[i].w;
if(need<least)
least=need;
}
}
else if(ins[v])
low[u]=min<int>(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
do
{
v=st[--top];
ins[v]=0;
}while (u!=v);
}
}
int main(void)
{
int n,m,a,b,w,i;
while (~scanf("%d%d",&n,&m)&&(n||m))
{
init();
for (i=0; i<m; ++i)
{
scanf("%d%d%d",&a,&b,&w);
add(a,b,w,i);
add(b,a,w,i);
}
int k=0;
for (i=1; i<=n; ++i)
{
if(!dfn[i])
{
Tarjan(i,-1);
++k;
}
}
if(k>1)//连通图数量
least=0;
else if(least==0)//至少派一个人去
least=1;
else if(least==INF)//
least=-1;
printf("%d\n",least);
}
return 0;
}

HDU 4738 Caocao's Bridges(Tarjan求桥+重边判断)的更多相关文章

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

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

  2. Hdu 4738 Caocao's Bridges (连通图+桥)

    题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这 ...

  3. HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】

     Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  4. HDU 4738 Caocao's Bridges ——(找桥,求联通块)

    题意:给你一个无向图,给你一个炸弹去炸掉一条边,使得整个图不再联通,你需要派人去安置炸弹,且派去的人至少要比这条边上的人多.问至少要派去多少个,如果没法完成,就输出-1. 分析:如果这个图是已经是多个 ...

  5. HDU 4738 Caocao's Bridges(Tarjan)

    题目链接 #include <iostream> #include <cstring> #include <cstdio> #include <queue&g ...

  6. kuangbin专题 专题九 连通图 HDU 4738 Caocao's Bridges

    题目链接:https://vjudge.net/problem/HDU-4738 题目:tarjan求桥,坑点: 题目说是分岛任务...如果所有岛之间没有完全连通,就不需要执行任务了...答案直接是0 ...

  7. hdu 4738 Caocao's Bridges(割边)

    题目链接 用tarjan求桥上的最小权值 #include<bits/stdc++.h> #define ll long long int using namespace std; inl ...

  8. HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. 2013杭州网赛 1001 hdu 4738 Caocao's Bridges(双连通分量割边/桥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥 ...

随机推荐

  1. Ubuntu安装steam游戏平台的解决方案

    steam是一个游戏平台,上面提供了很多收费和免费的游戏,在安装的过程中遇到了一些问题,所以把自己遇到的问题及解决方案分享出来供大家参考. 第一步:安装steam平台 sudo apt-get ins ...

  2. 教官的游戏(codevs 2793)

    题目描述 Description 有N个学生刚吃完饭,准备出食堂. 国防学校有个规矩:必须2人一排或3人一列离开. 两个教官A,B轮流取2或3人,谁先取完谁就赢得游戏.(A先取) 若两人都用最优策略, ...

  3. Genesis自动登录方法(免输入用户名和密码)

    第一步:点击“我的电脑”右键属性在“高级”里面的“环境变量”里面把“系统变量”照下图所示新建(XP和WIN7的环境变量设置方法类似): 变量名:FRONTLINE_NO_LOGIN_SCREEN 变量 ...

  4. Java中栈结构的自我实现

    package com.pinjia.shop.common.collection; /** * Created by wangwei on 2017/1/3. */ public class MyL ...

  5. Linux中信号量处理

    参考文章: http://blog.csdn.net/qinxiongxu/article/details/7830537/ 信号量一. 什么是信号量信号量的使用主要是用来保护共享资源,使得资源在一个 ...

  6. [译] UML中的关系之Dependency

    在UML中,依赖关系表示Client依赖于另一个元素,叫做Supplier. 通常来说,依赖关系不需要特殊的名字. 依赖的类别 抽象 abstraction, derive, refine, trac ...

  7. c++ 复习内容

    . ]) { sizeof(str)=? } :// 函数行参仅是一个指针 . Typedef struct s* tPs; tPs p3,p4;//相等 struct s*p3,struct s*p ...

  8. js选中当前菜单后高亮显示的导航条

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 【shiro】一、基础概念

    来源:http://blog.csdn.net/swingpyzf/article/details/46342023/ &&&& http://jinnianshilo ...

  10. Python实践:模块自动重载

    一.概述 二.思路 三.实现 四.测试 1.开启自动重载(终端1) 2.修改模块(终端2) 3.查看实时输出(终端1) 五.参考源码 一.概述 开发Web程序时,通常会采用本地服务器进行调试,但如果代 ...