HDU4738 Caocao's Bridges(桥)

http://acm.hdu.edu.cn/showproblem.php?pid=4738

题意:给定一张无向图,求其中权值最小的一座桥,派最少的士兵去炸掉它!!

思路:直接用tarjan计算出桥并且取其中权值最小者。

此题坑点甚多,1、有可能桥本来就不联通,输出-1。2、桥最小者为0,输出1(至少排一个人去炸桥)。3、不要去重边,两个岛之间允许有多座桥,tarjan忽略返回边只忽略一次,加pre_num处理下。

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rev(i,a,b) for(int i=(a);i>=(b);i--)
#define clr(a,x) memset(a,x,sizeof a)
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;

const int eps=0.00000001;
const int maxn=1005;
const int maxm=maxn*maxn*2;

int first[maxn],low[maxn],dfn[maxn],fa[maxn],if_add[maxn];
int nex[maxm],u[maxm],v[maxm],w[maxm];
int n,m,ecnt,index,bridge;
bool vcut[maxn],ecut[maxm];

void tarjan(int s,int pre)
{
	dfn[s]=low[s]=++index;
	int son=0;
	int pre_num=0;
	for(int e=first[s];~e;e=nex[e])
    {
        if(v[e]==pre&&!pre_num)
        {
            pre_num++;
            continue;
        }
        if(!dfn[v[e]])
        {
            son++;
            tarjan(v[e],s);
            low[s]=min(low[s],low[v[e]]);
            if(low[v[e]]>dfn[s])
            {
                bridge++;
                ecut[e]=1;
                ecut[e^1]=1;
            }
            if(s!=pre&&low[v[e]]>=dfn[s])
            {
                vcut[s]=1;
                if_add[s]++;
            }
        }
        else low[s]=min(low[s],dfn[v[e]]);
    }
    if(s==pre&&son>1)vcut[s]=1,if_add[s]=son-1;
}
void add_(int a,int b,int c)
{
	u[ecnt]=a;
	v[ecnt]=b;
	w[ecnt]=c;
	ecut[ecnt]=0;
	nex[ecnt]=first[a];
	first[a]=ecnt++;
}
void solve()
{
    clr(dfn,0);
    clr(if_add,0);
    clr(vcut,0);
    index=bridge=0;
    for(int i=1;i<=n;i++)
        if(!dfn[i])tarjan(i,i);
}
int find_(int x)
{
    if(fa[x]==-1)return x;
    return fa[x]=find_(fa[x]);
}
int main()
{
    int a,b,c;
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        clr(first,-1);ecnt=0;
        clr(fa,-1);int num=n;

        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(a!=b)
            {
                add_(a,b,c),add_(b,a,c);
                int x=find_(a);
                int y=find_(b);
                if(x!=y)fa[x]=y,num--;
            }
        }
        if(num!=1)
        {
            puts("0");
            continue;
        }
        solve();
        int ans=inf;
        for(int i=0;i<ecnt;i+=2)
            if(ecut[i]&&w[i]<ans)ans=w[i];
        if(ans==inf)ans=-1;
        if(ans==0)ans++;
        printf("%d\n",ans);
    }
}

HDU4738 Caocao's Bridges(桥)

时间: 2024-12-15 15:05:50

HDU4738 Caocao's Bridges(桥)的相关文章

HDU4738 Caocao&#39;s Bridges 无向图的桥

一眼题:找所有的桥,然后求最小权值 但是有很多坑点 1:如果本来不联通 输出0,(这个坑我知道) 2:但是还有一个坑,就是当整个连通,最小桥的权值是0时,也必须派一个人去,wa了无数遍(还是太年轻) #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <string> #include

hdu-4738.Caocao&#39;s Bridges(图中权值最小的桥)

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10933    Accepted Submission(s): 3065 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi.

hdu4738 Caocao&#39;s Bridges

http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:曹操赤壁之战大败,于是卷土重来.为了避免水上作战,他在长江上建立了一些岛屿,这样他的士兵就可以在岛屿上攻击到 周瑜的军队.同时,为了更方便各个岛屿的支援与部署,他在一些岛屿之间又修建了一些桥,并且在桥上准备了一些守卫的士兵.周瑜 看到后当然要破坏这些岛屿的连接,然而诸葛亮只给他留下了一个bomb,他如果想要炸毁一座桥需要派出至少和守卫士兵数相等的敢死队 去.问最少需要派出多少人. 很佩服出题人

[HDU4738]Caocao&#39;s Bridges(求割边最小权值)

http://acm.hdu.edu.cn/showproblem.php?pid=4738 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=1e3+4,maxm=1e6+4; int dfn[maxn],low[maxn],head[maxn]; int n,m,cnt=0,rt,ans

【模板】【hdu4738】Caocao&#39;s Bridges——tarjan求桥

题目链接 题目大意: 曹操有N个岛,这些岛用M座桥连接起来,每座桥有士兵把守(也可能没有), 诸葛亮把所有炸弹都带走了,只留下一枚给周瑜(真狠). 周瑜想让这N个岛不连通,但需要派出不小于守桥士兵数的人去炸桥,因为只有一枚炸弹,因此只够炸掉一座桥. 分析: 很明显的求代价最小的桥,当然这道题有几个特殊的地方: 1.图本来就不联通,输出0: 2.无解(不存在桥),输出-1: 3.没人把守,但你还是得派一个人炸桥,输出1: 4.一般情况,输出最小代价. 剩下的就是模板了,不过需要注意的一点是,这道题

HDU 4738 Caocao&#39;s Bridges tarjan求桥

Caocao's Bridges 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 Chan

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

HDU 4738 Caocao&#39;s Bridges(求价值最小的桥)

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

Hdu 4738 Caocao&#39;s Bridges (连通图+桥)

题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这个边一定是一个桥,所以我们只需要求出来所有的桥,然后比较每个桥的花费,选取最小的那个就好. 看起来很简单的样子哦!但是这个题目有很多的细节: A:题目中有重边,以后写Tarjan还是清一色判断重边吧.(除非题目特别要求) B:m个桥有可能连通不了这n个桥,这个时候不需要花费. C:当最小花费桥的花费