hdu5934 Bomb

本文详细解析了HDU 5934题目,通过缩点算法处理有向图中的强连通分量(SCC),并计算每个入度为0的SCC中的最小权值之和。代码实现使用了C++,包含Tarjan算法进行SCC分解。

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

链接

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

题解

缩点,每个入度为 0 0 0 S C C 中 SCC中 SCC取权值的 m i n min min加起来

代码

#include <bits/stdc++.h>
#define maxn 1010
#define maxe 1000010
#define linf (1ll<<60)
#define sqr(x) ((x)*(x))
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long ll;
ll N, x[maxn], y[maxn], c[maxn], r[maxn], indeg[maxn], mn[maxn];
struct Graph
{
    int etot, head[maxn], to[maxe], next[maxe], w[maxe];
    void clear(int N)
    {
        for(int i=1;i<=N;i++)head[i]=0;
        etot=0;
    }
    void adde(int a, int b, int c){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
}G;
struct Trajan_SCC
{
    int dfn[maxn], low[maxn], vis[maxn], scc[maxn], tim, scc_cnt;
    stack<int> s;
    void clear(int n)
    {
        for(int i=1;i<=n;i++)vis[i]=0;
        tim=scc_cnt=0;
    }
    void dfs(Graph &G, int pos)
    {
        int p;
        dfn[pos]=low[pos]=++tim;
        s.emplace(pos);
        vis[pos]=1;
        for(p=G.head[pos];p;p=G.next[p])
        {
            if(vis[G.to[p]]==0)dfs(G,G.to[p]);
            if(vis[G.to[p]]==1)low[pos]=min(low[pos],low[G.to[p]]);
        }
        if(dfn[pos]==low[pos])
        {
            int x;
            scc_cnt++;
            do
            {
                x=s.top(), s.pop();
                scc[x]=scc_cnt;
                vis[x]=2;
            }
            while(x!=pos);
        }
    }
    void run(Graph &G, int n)
    {
        for(int i=1;i<=n;i++)if(!vis[i])dfs(G,i);
    }
}tarjan;
int main()
{
    ios::sync_with_stdio(false);
    ll T, i, j, kase=0;
    cin>>T;
    while(T--)
    {
        cl(indeg);
        memset(mn,0x3f,sizeof(mn));
        G.clear(N);
        tarjan.clear(N);
        cin>>N;
        for(i=1;i<=N;i++)cin>>x[i]>>y[i]>>r[i]>>c[i];
        for(i=1;i<=N;i++)for(j=1;j<=N;j++)
            if(i!=j and sqr(x[i]-x[j])+sqr(y[i]-y[j])<=sqr(r[i]))
                G.adde(i,j,0);
        tarjan.run(G,N);
        for(i=1;i<=N;i++)mn[tarjan.scc[i]]=min(mn[tarjan.scc[i]],c[i]);
        for(i=1;i<=N;i++)for(j=1;j<=N;j++)
            if(tarjan.scc[i]!=tarjan.scc[j] and sqr(x[i]-x[j])+sqr(y[i]-y[j])<=sqr(r[i]))
                indeg[tarjan.scc[j]]++;
        ll ans=0;
        for(i=1;i<=tarjan.scc_cnt;i++)if(indeg[i]==0)ans+=mn[i];
        cout<<"Case #"<<++kase<<": "<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值