tarjan 视频讲解

/**
* 题目链接:https://vjudge.net/problem/HDU-5934
* 题意:给你n个炸弹,引爆每个炸弹会有一定的花费。每个炸弹给出坐标x,y,半径r,引爆花费;
* 引爆一个炸弹会把范围内的炸弹引爆,连锁反应。 现在想把所有炸弹引爆的最小花费。
*
* 解题思路:强连通缩点。根据a能够引爆b,可以在建一条a到b的单向边。如果是一个强连通(这一部分的图,
* 任意两点都可以相互到达)那么就把这个强连通分量变成一个点,值最分量的最小值。这样图就变成有向无环图了。
* 考虑到每个点的花费都是大于0的,所以引爆开始点最划算,即为入度为0的点。
*
* 前置技能 tarjan 缩点。
*/ #include <bits/stdc++.h>
using namespace std; const int maxn=1000+10;
const int INF=2e9+1e8;
vector<int>E[maxn];
struct Point
{
int x,y,r,cost;
}boom[maxn];
bool judge(Point a,Point b)
{
if( 1ll*(a.x-b.x)*(a.x-b.x)+1ll*(a.y-b.y)*(a.y-b.y)<=1ll*a.r*a.r ) return true;
return false;
}
int dfn[maxn],low[maxn],id,vis[maxn],ans,deg[maxn];
int num[maxn],cnt,cost[maxn];//对点进行重新编号,(数组num),按照联通分量进行编号
stack<int>S;
void init()
{
id=cnt=0;
memset(deg,0,sizeof(deg));
memset(num,0,sizeof(num));
memset(vis,0,sizeof(vis));
memset(dfn,0,sizeof(dfn));
}
void tarjan(int x)
{
low[x]=dfn[x]=++id;
S.push(x);
vis[x]=1;
for(int i=0;i<(int)E[x].size();i++)
{
int to=E[x][i];
if(!dfn[to])
{
tarjan(to);
low[x]=min(low[x],low[to]);
}
else if(vis[to]) low[x]=min(low[x],dfn[to]);
}
if(low[x]==dfn[x])
{
int mincost=INF,in=0;
cnt++;
while(1)
{
int now=S.top();
S.pop();
vis[now]=0;
num[now]=cnt;
mincost=min(mincost,boom[now].cost);
if(now==x) break;
}
cost[cnt]=mincost;
}
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
init();
for(int i=1;i<=n;i++)
{
E[i].clear();
scanf("%d%d%d%d",&boom[i].x,&boom[i].y,&boom[i].r,&boom[i].cost);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(judge(boom[i],boom[j])) E[i].push_back(j);
}
}
for(int i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int i=1;i<=n;i++)
{
for(int j=0;j<(int)E[i].size();j++)
{
int to=E[i][j];
if(num[i]!=num[to]) deg[num[to]]++;
}
}
ans=0;
for(int i=1;i<=cnt;i++) if(deg[i]==0)
ans+=cost[i];
printf("Case #%d: %d\n",cas++,ans);
}
return 0;
}

HDU - 5934的更多相关文章

  1. HDU 5934 Bomb(炸弹)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  2. HDU 5934:Bomb(强连通缩点)

    http://acm.hdu.edu.cn/showproblem.php?pid=5934 题意:有N个炸弹,每个炸弹有一个坐标,一个爆炸范围和一个爆炸花费,如果一个炸弹的爆炸范围内有另外的炸弹,那 ...

  3. HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. hdu 5934 Bomb

    Bomb Problem Description There are N bombs needing exploding.Each bomb has three attributes: explodi ...

  5. HDU 5934 强联通分量

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. Bomb HDU - 5934 (Tarjan)

    #include<map> #include<set> #include<ctime> #include<cmath> #include<stac ...

  7. HDU 5934 Bomb(tarjan/SCC缩点)题解

    思路:建一个有向图,指向能引爆对象,把强连通分量缩成一点,只要点燃图中入度为0的点即可.因为入度为0没人能引爆,不为0可以由别人引爆. 思路很简单,但是早上写的一直错,改了半天了,推倒重来才过了... ...

  8. HDU 5934 (强连同分量+缩点)

    题意: 给出n个炸弹的信息 :坐标x , 坐标y , 爆炸半径 , 成本: 如果一个炸弹被引爆那这个范围的都爆炸 , 问最小的成本是多少? 题意:首先先来个n^2 暴力出某个炸弹爆炸波及的其他炸弹,用 ...

  9. 【(最小权点基)tarjan强连通分量缩点+tarjan模板】HDU 5934 Bomb

    [AC] #include<bits/stdc++.h> using namespace std; typedef long long ll; int n; ; ; const int i ...

随机推荐

  1. 基于react-native android的新闻app的开发

    使用平台:android 代码获取地址:https://github.com/wuwanyu/ReactNative-Android-MovieDemo 项目展示: 结构图: SpalashScree ...

  2. Java static关键字特点

    一.特点 1.随着类的加载而加载2.优先于对象存在3.被类的所有对象共享4.可以通过类名调用 二.调用特征 静态方法: 成员变量:只能访问静态变量 成员方法:只能访问静态成员方法 非静态方法: 成员变 ...

  3. TFS中工作项的定制- 字段功能定义

    参考,翻译此页面All FIELD XML Elements Reference(http://msdn.microsoft.com/en-us/library/ms194953.aspx) 对于每一 ...

  4. output value . Sigmoid neurons are similar to perceptrons, but modified so that small changes in their weights and bias cause only a small change in their output.

    http://neuralnetworksanddeeplearning.com/chap1.html . Sigmoid neurons are similar to perceptrons, bu ...

  5. Card Collector(期望+min-max容斥)

    Card Collector(期望+min-max容斥) Card Collector woc居然在毫不知情的情况下写出一个min-max容斥 题意 买一包方便面有几率附赠一张卡,有\(n\)种卡,每 ...

  6. [转载]Hibernate如何提升数据库查询的性能

    目录(?)[-] 数据库查询性能的提升也是涉及到开发中的各个阶段在开发中选用正确的查询方法无疑是最基础也最简单的 SQL语句的优化 使用正确的查询方法 使用正确的抓取策略 Hibernate的性能优化 ...

  7. Nodejs课堂笔记-第二课 package.json的作用

    本文由Vikings(http://www.cnblogs.com/vikings-blog/) 原创,转载请标明.谢谢! 上节课,我们打造了一下IDE工具-web storm的显示界面.至少现在回到 ...

  8. R语言图形base系统(一)

           一般R作图有三大绘图系统:base系统.ggplot2绘图系统.lattice绘图系统.        本篇主要介绍base系统绘图时的图形参数.一般用plot()函数来完成.在R中,若 ...

  9. maven 手动加载第三方jar、zip包

    使用maven搭建工程时,难免要加载大量的第三方的jar包.zip包比较少用,而maven的官网提供的jar往往不能满足需求,这时需要我们手动加载到我们本地或nexus私服的仓库中. 1.加载jar包 ...

  10. Spring MVC 通过反射将数据导出到excel

    直接上代码 // 导出excel方法 @RequestMapping("exportExcel") public void exportExcel(HttpServletReque ...