hdu 5934 Bomb

来源:互联网 发布:福建网络服务公司 编辑:程序博客网 时间:2024/05/20 14:41
Problem Description
There are N bombs needing exploding.

Each bomb has three attributes: exploding radius ri, position (xi,yi) and lighting-cost ci which means you need to pay ci 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 T, which indicates the number of test cases.

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

In the following N lines, the ith line contains four intergers xi, yi, ri and ci, indicating the coordinate of ith bomb is (xi,yi), exploding radius is ri and lighting-cost is ci.

Limits
- 1≤T≤20
- 1≤N≤1000
- −108≤xi,yi,ri≤108
- 1≤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

分析:强连通 + 缩点。

#include<iostream>#include<stdio.h>#include<stack>#include<string.h>#include<algorithm>#include<vector>const int maxn = 1000;const int inf = 1e9;typedef long long ll;using namespace std;int n;vector<int>G[maxn+5];int pre[maxn+5],lowlink[maxn+5],sccno[maxn+5],dfs_clock,scc_cnt;stack<int>S;void dfs(int u){    pre[u] = lowlink[u] = ++dfs_clock;    S.push(u);    for(int i=0,l=G[u].size(); i<l; i++)    {        int v = G[u][i];        if(!pre[v])        {            dfs(v);            lowlink[u] = min(lowlink[u],lowlink[v]);        }        else        {            if(!sccno[v]) lowlink[u]=min(lowlink[u],pre[v]);        }    }    if(lowlink[u] == pre[u])    {        scc_cnt++;        for(;;)        {            int x = S.top();            S.pop();            sccno[x] = scc_cnt;            if(x==u) break;        }    }}void find_scc(int n){    dfs_clock = scc_cnt = 0;    memset(sccno,0,sizeof(sccno));    memset(pre,0,sizeof(pre));    for(int i=1; i<=n; i++) if(!pre[i]) dfs(i);}ll x[maxn+5],y[maxn+5],r[maxn+5];int w[maxn+5],c[maxn+5],cnt[maxn+5],ans;bool ok(int i,int j){    ll L = r[i];    ll d = (x[i]-x[j])*1ll*(x[i]-x[j])*1ll+(y[i]-y[j])*1ll*(y[i]-y[j])*1ll;    if(d<=L*L) return true;    return false;}int main(){    int T,Cas=1;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(int i=1; i<=n; i++)        {            scanf("%I64d %I64d %I64d %d",&x[i],&y[i],&r[i],&w[i]);            G[i].clear() , c[i] = 0;        }        for(int i=1; i<=n; i++)        {            for(int j=1; j<=n; j++)            {                if(ok(i,j)) G[i].push_back(j);            }        }        find_scc(n);        for(int i=1; i<=scc_cnt; i++)  c[i] = inf ,cnt[i] = 0;        for(int x=1; x<=n; x++)        {            int l = G[x].size();            c[sccno[x]] = min(c[sccno[x]],w[x]);            for(int j=0; j<l; j++)            {                int y = G[x][j];                if(sccno[x] == sccno[y]) continue;                cnt[sccno[y]]++;            }        }        ans = 0;        for(int i=1; i<=scc_cnt; i++) if(!cnt[i]) ans+=c[i];        printf("Case #%d: %d\n",Cas++,ans);    }    return 0;}


原创粉丝点击