zoukankan      html  css  js  c++  java
  • Bomb

    Bomb

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    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
    - 1T20
    - 1N1000
    - 108xi,yi,ri108
    - 1ci104
     
    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
    分析:对入度为0的强连通分量求一个最小花费;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <unordered_map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    #define freopen freopen("in.txt","r",stdin)
    const int maxn=1e3+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,pre[maxn],link[maxn],sccno[maxn],du[maxn],dfs_clock,scc_cnt,cas;
    vi e[maxn],bel[maxn];
    stack<int>s;
    struct node
    {
        ll x,y,r,c;
        bool operator<(const node&p)const
        {
            return c<p.c;
        }
    }a[maxn];
    ll dist(int u,int v)
    {
        return (a[u].x-a[v].x)*(a[u].x-a[v].x)+(a[u].y-a[v].y)*(a[u].y-a[v].y);
    }
    void dfs(int u)
    {
        pre[u]=link[u]=++dfs_clock;
        s.push(u);
        for(int x:e[u])
        {
            if(!pre[x])
            {
                dfs(x);
                link[u]=min(link[u],link[x]);
            }
            else if(!sccno[x])
            {
                link[u]=min(link[u],pre[x]);
            }
        }
        if(link[u]==pre[u])
        {
            scc_cnt++;
            while(true)
            {
                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);
    }
    int main()
    {
        int i,j;
        scanf("%d",&t);
        while(t--)
        {
            memset(du,0,sizeof(du));
            scanf("%d",&n);
            rep(i,1,n)bel[i].clear(),e[i].clear();
            rep(i,1,n)scanf("%lld%lld%lld%lld",&a[i].x,&a[i].y,&a[i].r,&a[i].c);
            rep(i,1,n)rep(j,1,n)
            {
                if(i==j)continue;
                if(dist(i,j)<=a[i].r*a[i].r)e[i].pb(j);
            }
            find_scc(n);
            rep(i,1,n)bel[sccno[i]].pb(i);
            rep(i,1,n)rep(j,1,n)
            {
                if(i==j)continue;
                if(dist(i,j)<=a[i].r*a[i].r&&sccno[i]!=sccno[j])++du[sccno[j]];
            }
            ll ans=0;
            rep(i,1,scc_cnt)
            {
                if(!du[i])
                {
                    ll now=1e18;
                    for(int x:bel[i])now=min(now,a[x].c);
                    ans+=now;
                }
            }
            printf("Case #%d: %lld
    ",++cas,ans);
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    LeetCode 167. 两数之和 II
    LeetCode 97. 交错字符串
    LeetCode 35. 搜索插入位置
    LeetCode 120. 三角形最小路径和
    LeetCode 350. 两个数组的交集 II
    LeetCode 174. 地下城游戏
    LeetCode 315. 计算右侧小于当前元素的个数
    LeetCode 309. 最佳买卖股票时机含冷冻期
    面试题 17.13. 恢复空格
    去除字符串首尾空格
  • 原文地址:https://www.cnblogs.com/dyzll/p/6011449.html
Copyright © 2011-2022 走看看