hdu-4738(tarjan割边)

题意:给你n个点,m条边,边有权值,问你最小的花费使图不连通;

解题思路:就是求边权最小的割边,但这道题有坑点:

1、有重边(桥的两个点有重边时,你去掉一条边并没什么d用);

2、当权值为0的时候,我们也需要放一个人(被这个坑死了0.0);

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define maxn 1100
#define inf 0x3f3f3f3f
using namespace std;
struct Edge
{
    int next;
    int to;
    int w;
}edge[maxn*maxn*2];
int head[maxn*maxn];
int low[maxn*maxn];
int dfn[maxn*maxn];
int n,m;
int flag;
int minn;
int cnt,step;
int bridge;
void init()
{
    memset(head,-1,sizeof(head));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    cnt=0;step=0;bridge=0;flag=0;minn=inf;
}
void add(int u,int v,int w)
{
    edge[cnt].next=head[u];
    edge[cnt].to=v;
    edge[cnt].w=w;
    head[u]=cnt++;
}
void tarjan(int u,int fa)
{
    dfn[u]=low[u]=++step;
    int num=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(fa==v&&num==0)
        {
            num++;
            continue;
        }
       // cout<<v<<endl;
        if(!dfn[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(low[v]>dfn[u])
            {
                bridge++;
                minn=min(minn,edge[i].w);
            }
        }
        else
            low[u]=min(low[u],dfn[v]);
    }
}
void solve()
{
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i])
        {
            ans++;
            tarjan(i,i);
        }
    }
    if(ans>=2)
    {
        printf("0\n");
        return;
    }
    if(bridge==0)
        printf("-1\n");
    else
    {
        if(minn==0)
            printf("1\n");
        else
        printf("%d\n",minn);
    }
}
int main()
{
    int x,y,w;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        if(n==0&&m==0)
            break;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&x,&y,&w);
            add(x,y,w);
            add(y,x,w);
        }
        solve();
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/huangdao/p/9017436.html

时间: 2024-10-10 10:40:16

hdu-4738(tarjan割边)的相关文章

HDU 4738 Caocao&#39;s Bridges(割边)

乍一看一个模板题,仔细一看还是模板题,但是三个坑.1,不是连通图,放0个.2 守卫为0,放1个. 3注意重边. #include<iostream> #include<cstdio> #include<vector> #include<queue> #include<algorithm> #include<stack> #include<cstring> using namespace std; #define maxn

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(找割边)

HDU 4738 Caocao's Bridges 题目链接 注意几个坑,可能重边,至少要派一个人去炸,没有连通的时候就不用炸了 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 1005; const int INF = 0x3f3f3f3f; int pre[N], low[N

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 tarjan

Caocao's Bridges Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=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 still was not good

(连通图 Tarjan)Caocao&#39;s Bridges --HDU --4738

链接: http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:曹操有很多岛屿,然后呢需要建造一些桥梁将所有的岛屿链接起来,周瑜要做的是就是不让曹操将所有岛屿连接起来,每个座桥有人在守卫, 周瑜只能炸一座桥,并且他派人去炸桥只能派的人数必须 大于等于守桥的人数.输出最小的炸桥人数, 要是没有答案就输出 -1 代码: #include <iostream> #include <cstdio> #include <cmath>

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:当最小花费桥的花费

HDU 4738 Caocao&#39;s Bridges

Caocao's Bridges Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 473864-bit integer IO format: %I64d      Java class name: Main Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wo

HDU 4738 Caocao&#39;s Bridges ——(找桥,求联通块)

题意:给你一个无向图,给你一个炸弹去炸掉一条边,使得整个图不再联通,你需要派人去安置炸弹,且派去的人至少要比这条边上的人多.问至少要派去多少个,如果没法完成,就输出-1. 分析:如果这个图是已经是多个联通块了,那么一个人都不用去,如果不是,那么只要找出这个无向图上的桥并且哨兵数量最少的那座把它炸了就行(输出这条边上的哨兵数量即可).直接tarjan就可以写. 注意点:1.可能有重边,所以用手写邻接表的方式存图:2.如果一座桥上没有哨兵,那么你也得至少派去一个人去安置炸弹(因为炸弹不会自己飞过去啊