hdu 4005(双连通)

本文探讨了一种在敌方通信网络中寻找最佳攻击线路的方法,确保无论敌方如何加强网络,都能够有效地进行破坏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The war

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 656    Accepted Submission(s): 115


Problem Description
In the war, the intelligence about the enemy is very important. Now, our troop has mastered the situation of the enemy's war zones, and known that these war zones can communicate to each other directly or indirectly through the network. We also know the enemy is going to build a new communication line to strengthen their communication network. Our task is to destroy their communication network, so that some of their war zones can't communicate. Each line has its "cost of destroy". If we want to destroy a line, we must spend the "cost of destroy" of this line. We want to finish this task using the least cost, but our enemy is very clever. Now, we know the network they have already built, but we know nothing about the new line which our enemy is going to build. In this condition, your task is to find the minimum cost that no matter where our enemy builds the new line, you can destroy it using the fixed money. Please give the minimum cost. For efficiency, we can only destroy one communication line.
 

Input
The input contains several cases. For each cases, the first line contains two positive integers n, m (1<=n<=10000, 0<=m<=100000) standing for the number of the enemy's war zones (numbered from 1 to n), and the number of lines that our enemy has already build. Then m lines follow. For each line there are three positive integer a, b, c (1<=a, b<=n, 1<=c<=100000), meaning between war zone A and war zone B there is a communication line with the "cost of destroy " c.
 

Output
For each case, if the task can be finished output the minimum cost, or output ‐1.
 

Sample Input
  
3 2 1 2 1 2 3 2 4 3 1 2 1 1 3 2 1 4 3
 

Sample Output
  
-1 3
Hint
For the second sample input: our enemy may build line 2 to 3, 2 to 4, 3 to 4. If they build line 2 to 3, we will destroy line 1 to 4, cost 3. If they build line 2 to 4, we will destroy line 1 to 3, cost 2. If they build line 3 to 4, we will destroy line 1 to 2, cost 1. So, if we want to make sure that we can destroy successfully, the minimum cost is 3.
 

Source
 

Recommend
lcy

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4005

分析:这题是大连赛区的第5题,可惜当时不会双连通阿~~~

首先将所有双连通分量缩成一些点,剩下的点就是一棵无根树,题目要求出一个最小权值使得任意加一条边后都能去掉一条小于这个权值的边,使得图不连通,最坏情况下肯定是把最小边的两端连起来,由于只要连树叶点就行,于是我们就以最小边的两个端点为根,找两棵树中的各一条路径,使得路径包含的边尽量小,其实也就是所有节点与其子节点的边的次小值中的最小值就是答案。。。晕

代码:

#include<cstdio>
#define min(a,b) (a<b?a:b)
using namespace std;
const int mm=222222;
const int mn=11111;
const int oo=1000000000;
int t[mm],c[mm],p[mm];
int h[mn],q[mn],low[mn],dfn[mn],id[mn],w[mn][3];
int i,j,s,e,k,n,m,cnt,top,et,idx,ans;
inline void add(int u,int v,int s)
{
    t[e]=v,c[e]=s,p[e]=h[u],h[u]=e++;
    t[e]=u,c[e]=s,p[e]=h[v],h[v]=e++;
}
void dfs(int u,int fa)
{
    dfn[u]=low[u]=++idx;
    q[top++]=u;
    for(int i=h[u],v;i>=0;i=p[i])
        if(!dfn[v=t[i]])
        {
            dfs(v,u);
            low[u]=min(low[u],low[v]);
            if(dfn[u]<low[v])
            {
                id[v]=++cnt,w[++et][0]=u,w[et][1]=v,w[et][2]=c[i];
                while(q[--top]!=v)id[q[top]]=cnt;
            }
        }
        else if(v!=fa)low[u]=min(low[u],dfn[v]);
}
void find(int u)
{
    int i,v;
    low[u]=oo;
    for(i=h[u];i>=0;i=p[i])
        if(!q[v=t[i]])
        {
            q[v]=1,find(v);
            low[v]=min(low[v],c[i]);
            if(low[v]<low[u])ans=min(ans,low[u]),low[u]=low[v];
            else ans=min(ans,low[v]);
        }
}
int tarjan()
{
    int i,j,k,mi;
    for(cnt=idx=top=i=et=0;i<=n;++i)dfn[i]=0;
    for(i=1;i<=n;++i)
        if(!dfn[i])
        {
            dfs(i,i);
            ++cnt;
            while(top--)id[q[top]]=cnt;
        }
    for(i=e=0;i<=cnt;++i)h[i]=-1,q[i]=0;
    for(mi=k=1;k<=et;++k)
    {
        add(id[w[k][0]],id[w[k][1]],w[k][2]);
        if(w[k][2]<w[mi][2])mi=k;
    }
    q[i=id[w[mi][0]]]=q[j=id[w[mi][1]]]=1;
    ans=oo;
    find(i),find(j);
    return (ans<oo)?ans:-1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=-1)
    {
        for(i=e=0;i<=n;++i)h[i]=-1;
        while(m--)scanf("%d%d%d",&i,&j,&s),add(i,j,s);
        printf("%d\n",tarjan());
    }
    return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值