HDU - 5934(73/600)

本文介绍了一种基于图论的算法解决炸弹连锁爆炸问题的方法。通过构建图模型并利用Tarjan算法寻找强连通分量,进而求得引爆所有炸弹所需的最小成本。详细解释了算法流程,包括输入输出格式及示例。

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

here are NN bombs needing exploding.

Each bomb has three attributes: exploding radius riri, position (xi,yi)(xi,yi) and lighting-cost cici which means you need to pay cici cost making it explode.

If a un-lighting bomb is in or on the border the exploding area of another exploding one, the un-lighting bomb also will explode.

Now you know the attributes of all bombs, please use the minimum cost to explode all bombs.
Input
First line contains an integer TT, which indicates the number of test cases.

Every test case begins with an integers NN, which indicates the numbers of bombs.

In the following NN lines, the ith line contains four intergers xixi, yiyi, riri and cici, indicating the coordinate of ith bomb is (xi,yi)(xi,yi), exploding radius is riri and lighting-cost is cici.

Limits
- 1≤T≤201≤T≤20
- 1≤N≤10001≤N≤1000
- −108≤xi,yi,ri≤108−108≤xi,yi,ri≤108
- 1≤ci≤1041≤ci≤104
Output
For every test case, you should output ‘Case #x: y’, where x indicates the case number and counts from 1 and y is the minimum cost.
Sample Input
1
5
0 0 1 5
1 1 1 6
0 1 1 7
3 0 2 10
5 0 1 4
Sample Output
Case #1: 15

tarjan以后找根

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long 
int liantong[1001], dfn[1001], low[1001], clj = 0, jc = 0;
stack<int>zz;
int inf=0x3f3f3f3f3f3f3f3f;
int T,n,x[1001],y[1001],r[1001],c[1001],u=0,chu[1001],dan[1001];
vector<int>tu[1001];
void tarjan(int xianzai)
{
    dfn[xianzai] = low[xianzai] = ++clj;
    zz.push(xianzai);
    for (int a = 0;a<tu[xianzai].size();a++)
    {
        int dd = tu[xianzai][a];
        if (!dfn[dd])
        {
            tarjan(dd);
            low[xianzai] = min(low[xianzai], low[dd]);
        }
        else if (!liantong[dd])low[xianzai] = min(low[xianzai], low[dd]);
    }
    if (low[xianzai] == dfn[xianzai])
    {
        jc++;
        for (;;)
        {
            int ww = zz.top();
            zz.pop();
            liantong[ww] = jc;
            if (ww == xianzai)break;
        }
    }
}
main()
{
    cin>>T;
    while(T--)
    {
        cin>>n;
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(liantong,0,sizeof(liantong));
        memset(r,0,sizeof(r));
        memset(c,0,sizeof(c));
        memset(chu,0,sizeof(chu));
        memset(dan,0x3f,sizeof(dan));
        clj=jc=0;
        while(!zz.empty())zz.pop();
        for(int a=1;a<=n;a++)tu[a].clear();
        for(int a=1;a<=n;a++)scanf("%lld%lld%lld%lld",&x[a],&y[a],&r[a],&c[a]);
        for(int a=1;a<=n;a++)
        {
            for(int b=1;b<=n;b++)
            {
                if(b==a)continue;
                if((x[b]-x[a])*(x[b]-x[a])+(y[a]-y[b])*(y[a]-y[b])>r[a]*r[a])continue;
                tu[a].push_back(b);
            }
        }
        for(int a=1;a<=n;a++)
        {
            if(liantong[a])continue;
            tarjan(a);
        }
        for(int a=1;a<=n;a++)
        {
            for(int b=0;b<tu[a].size();b++)
            {
                int w=tu[a][b];
                if(liantong[a]==liantong[w])continue;
                chu[liantong[w]]++;
            }
        }
        for(int a=1;a<=n;a++)
        {
            if(chu[liantong[a]])continue;
            dan[liantong[a]]=min(dan[liantong[a]],c[a]);
        //  cout<<liantong[a]<<endl;
        }
        /*for(int a=1;a<=jc;a++)
        {
            cout<<chu[a]<<endl;
        }
        return 0;*/
        int dandan=0;
        for(int a=1;a<=jc;a++)
        {
            if(dan[a]==inf)continue;
            if(chu[a])continue;
            dandan+=dan[a];
        }
        printf("Case #%lld: %lld\n",++u,dandan);
    }
}
### 关于HDU - 6609 的题目解析 由于当前未提供具体关于 HDU - 6609 题目的详细描述,以下是基于一般算法竞赛题型可能涉及的内容进行推测和解答。 #### 可能的题目背景 假设该题目属于动态规划类问题(类似于多重背包问题),其核心在于优化资源分配或路径选择。此类问题通常会给出一组物品及其属性(如重量、价值等)以及约束条件(如容量限制)。目标是最优地选取某些物品使得满足特定的目标函数[^2]。 #### 动态转移方程设计 如果此题确实是一个变种的背包问题,则可以采用如下状态定义方法: 设 `dp[i][j]` 表示前 i 种物品,在某种条件下达到 j 值时的最大收益或者最小代价。对于每一种新加入考虑范围内的物体 k ,更新规则可能是这样的形式: ```python for i in range(n): for s in range(V, w[k]-1, -1): dp[s] = max(dp[s], dp[s-w[k]] + v[k]) ``` 这里需要注意边界情况处理以及初始化设置合理值来保证计算准确性。 另外还有一种可能性就是它涉及到组合数学方面知识或者是图论最短路等相关知识点。如果是后者的话那么就需要构建相应的邻接表表示图形结构并通过Dijkstra/Bellman-Ford/Floyd-Warshall等经典算法求解两点间距离等问题了[^4]。 最后按照输出格式要求打印结果字符串"Case #X: Y"[^3]。 #### 示例代码片段 下面展示了一个简单的伪代码框架用于解决上述提到类型的DP问题: ```python def solve(): t=int(input()) res=[] cas=1 while(t>0): n,k=list(map(int,input().split())) # Initialize your data structures here ans=find_min_unhappiness() # Implement function find_min_unhappiness() res.append(f'Case #{cas}: {round(ans)}') cas+=1 t-=1 print("\n".join(res)) solve() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值