链接
http://acm.hdu.edu.cn/showproblem.php?pid=5934
题解
缩点,每个入度为 0 0 0的 S C C 中 SCC中 SCC中取权值的 m i n min min加起来
代码
#include <bits/stdc++.h>
#define maxn 1010
#define maxe 1000010
#define linf (1ll<<60)
#define sqr(x) ((x)*(x))
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long ll;
ll N, x[maxn], y[maxn], c[maxn], r[maxn], indeg[maxn], mn[maxn];
struct Graph
{
int etot, head[maxn], to[maxe], next[maxe], w[maxe];
void clear(int N)
{
for(int i=1;i<=N;i++)head[i]=0;
etot=0;
}
void adde(int a, int b, int c){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
}G;
struct Trajan_SCC
{
int dfn[maxn], low[maxn], vis[maxn], scc[maxn], tim, scc_cnt;
stack<int> s;
void clear(int n)
{
for(int i=1;i<=n;i++)vis[i]=0;
tim=scc_cnt=0;
}
void dfs(Graph &G, int pos)
{
int p;
dfn[pos]=low[pos]=++tim;
s.emplace(pos);
vis[pos]=1;
for(p=G.head[pos];p;p=G.next[p])
{
if(vis[G.to[p]]==0)dfs(G,G.to[p]);
if(vis[G.to[p]]==1)low[pos]=min(low[pos],low[G.to[p]]);
}
if(dfn[pos]==low[pos])
{
int x;
scc_cnt++;
do
{
x=s.top(), s.pop();
scc[x]=scc_cnt;
vis[x]=2;
}
while(x!=pos);
}
}
void run(Graph &G, int n)
{
for(int i=1;i<=n;i++)if(!vis[i])dfs(G,i);
}
}tarjan;
int main()
{
ios::sync_with_stdio(false);
ll T, i, j, kase=0;
cin>>T;
while(T--)
{
cl(indeg);
memset(mn,0x3f,sizeof(mn));
G.clear(N);
tarjan.clear(N);
cin>>N;
for(i=1;i<=N;i++)cin>>x[i]>>y[i]>>r[i]>>c[i];
for(i=1;i<=N;i++)for(j=1;j<=N;j++)
if(i!=j and sqr(x[i]-x[j])+sqr(y[i]-y[j])<=sqr(r[i]))
G.adde(i,j,0);
tarjan.run(G,N);
for(i=1;i<=N;i++)mn[tarjan.scc[i]]=min(mn[tarjan.scc[i]],c[i]);
for(i=1;i<=N;i++)for(j=1;j<=N;j++)
if(tarjan.scc[i]!=tarjan.scc[j] and sqr(x[i]-x[j])+sqr(y[i]-y[j])<=sqr(r[i]))
indeg[tarjan.scc[j]]++;
ll ans=0;
for(i=1;i<=tarjan.scc_cnt;i++)if(indeg[i]==0)ans+=mn[i];
cout<<"Case #"<<++kase<<": "<<ans<<endl;
}
return 0;
}