Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
#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\n",++cas,ans); } //system("Pause"); return 0; }
原文:http://www.cnblogs.com/dyzll/p/6011449.html