HDU 4738 Caocao'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 1005
int head[maxn],dfn[maxn],low[maxn],guard[maxn][maxn],tot,n;
int vis[maxn],all,index,bri[maxn*maxn];
struct Edge
{
    int to,nxt,g;
} edge[maxn*maxn*2];
queue<int> que;
void bfs(int u)
{
    vis[u] = 1;
    que.push(u);
    while(!que.empty())
    {
        int now = que.front();
        que.pop();
        for(int i = head[now]; i != -1; i = edge[i].nxt)
        {
            int v = edge[i].to;
            if(!vis[v]) {que.push(v);vis[v] = 1;}
        }
    }
}
bool judge()
{
    memset(vis,0,sizeof(vis));
    int sum = 0;
    while(!que.empty()) que.pop();
    for(int i = 1; i <= n; i++)
    {
        if(!vis[i])
        {
            sum++;
            bfs(i);
        }
    }
    if(sum==1) return true;
    return false;
}
void addedge(int u,int v,int w)
{
    edge[tot].to = v;
    edge[tot].g = w;
    edge[tot].nxt = head[u];
    head[u] = tot++;
}
void init()
{
    all = index = 0;
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
}
void tarjan(int u,int fa)
{
    low[u] = dfn[u] = ++index;
    for(int i = head[u];i != -1;i = edge[i].nxt){
        int v = edge[i].to;
        if(!dfn[v]){
            tarjan(v,u);
            low[u] = min(low[u],low[v]);
            if(low[v] > dfn[u] && guard[u][v]!=-2&&guard[v][u]!=-2){
                bri[all++] = edge[i].g;
            }
        }
        else if(v != fa) low[u] = min(low[u],dfn[v]);
    }
}
int main()
{
    int m,a,b,w;
    while(~scanf("%d%d",&n,&m))
    {
        if(n+m == 0) break;
        memset(head,-1,sizeof(head));
        tot = 0;
        memset(guard,-1,sizeof(guard));
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d",&a,&b,&w);
            if(guard[a][b]==-1 && guard[b][a]==-1)
            {
                addedge(a,b,w);
                addedge(b,a,w);
                guard[a][b] = guard[b][a] = w;
            }
            else {
                guard[a][b] = guard[b][a] = -2;
            }
        }
        if(!judge())
        {
            puts("0");
            continue;
        }
        init();
        tarjan(1,-1);
        if(all == 0){
            puts("-1");
            continue;
        }
        sort(bri,bri+all);
        if(bri[0] == 0) puts("1");
        else printf("%d\n",bri[0]);
    }
}

HDU 4738 Caocao's Bridges(割边)

时间: 2024-10-18 00:22:29

HDU 4738 Caocao's Bridges(割边)的相关文章

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 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 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

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.如果一座桥上没有哨兵,那么你也得至少派去一个人去安置炸弹(因为炸弹不会自己飞过去啊

hdu 4738 Caocao&#39;s Bridges(桥的最小权值+去重)

http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:曹操有一些岛屿被桥连接,每座都有士兵把守,周瑜想把这些岛屿分成两部分,但他只能炸毁一条桥,问最少需要派几个士兵去;如果不能完成输出-1 1:如果这些岛屿不连通,则不需要派人前去 2:如果桥的守卫是0的话也得派一人去炸毁 3:如果不能完成输出-1 4:输出最少需派的人数 #include<stdio.h> #include<string.h> #include<math.h&