Bomb HDU

来源:互联网 发布:软件项目总监职责 编辑:程序博客网 时间:2024/05/17 21:06

There are N 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 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 cici, indicating the coordinate of ith bomb is (xi,yi), exploding radius is ri and lighting-cost is cici.

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

粗略看一下还以为是dp,慢慢反应过来有点图论的意思,先一个点引爆如何在范围内都可以引爆,相当于有了一个有向箭头,指向了各个被引爆的点,但是反之不亦然,所有就是一个有向图,假设建完图了,其实发现就是求连通分量即可,取这个连通分量的最小花费的点就好,显然就是tarjan算法然后缩点,最后把所有只要是出度为0的点的花费加起来即可,tarjan的固定模式,算是简单题

#include<cstdio>#include<iostream>#include<queue>#include<cstring>#include<vector>#include<algorithm>#include<cmath>#include<map>#include<stack>#define N 1005#define INF 1000000;using namespace std;int n;int x[N],y[N],r[N],c[N];vector<int>graph[N];long long dis(int p1,int p2){    return (long long)(x[p1]-x[p2])*(x[p1]-x[p2])+(long long)(y[p1]-y[p2])*(y[p1]-y[p2]);}stack<int> st;bool inS[N];int DFN[N],low[N];int belong[N];int val[N];int countt,index1;int inD[N];tarjan(int x){    DFN[x]=low[x]=index1++;    inS[x]=true;    st.push(x);    for(int i=0;i<graph[x].size();i++)    {        int v=graph[x][i];        if(!DFN[v])        {            tarjan(v);            low[x]=min(low[x],low[v]);        }        else            if(inS[v])                low[x]=min(low[x],DFN[v]);    }    if(low[x]==DFN[x])    {        val[countt]=INF;        while(st.top()!=x)        {            int v=st.top();            st.pop();            inS[v]=false;            belong[v]=countt;            val[countt]=min(val[countt],c[v]);//去最小的花费作为缩点以后的权值        }        int v=st.top();        st.pop();        inS[v]=false;        belong[v]=countt;        val[countt]=min(val[countt],c[v]);        countt++;    }}int work(){    for(int i=1;i<=n;i++)        for(int j=0;j<graph[i].size();j++)        {            int v=graph[i][j];            if(belong[v]!=belong[i])              inD[belong[v]]++;        }    int ans=0;    for(int i=0;i<countt;i++)        if(!inD[i])        ans+=val[i];    return ans;}void init(){    for(int i=1;i<=n;i++)        graph[i].clear();    index1=1;    countt=0;    memset(inD,0,sizeof(inD));    memset(DFN,0,sizeof(DFN));}int main(){    int t;    scanf("%d",&t);    int ca=1;    while(t--)    {        scanf("%d",&n);        init();        for(int i=1;i<=n;i++)        {            scanf("%d%d%d%d",x+i,y+i,r+i,c+i);            for(int j=1;j<i;j++)            {                long long d=dis(i,j);                if((long long)r[j]*r[j]>=d)                    graph[j].push_back(i);                if((long long)r[i]*r[i]>=d)                    graph[i].push_back(j);            }//直接n方建图        }        for(int i=1;i<=n;i++)            if(!DFN[i])                tarjan(i);        printf("Case #%d: %d\n",ca++,work());    }    return 0;}
原创粉丝点击