Description
During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China -- they were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the king of the kingdom Qin. Through 9 years of wars, he finally conquered all six other kingdoms and became the first emperor of a unified China in 221 BC. That was Qin dynasty -- the first imperial dynasty of China(not to be confused with the Qing Dynasty, the last dynasty of China). So Ying Zheng named himself "Qin Shi Huang" because "Shi Huang" means "the first emperor " in Chinese.Qin Shi Huang undertook gigantic projects, including the first version of the Great Wall of China, the now famous city-sized mausoleum guarded by a life-sized Terracotta Army, and a massive national road system. There is a story about the road system:
There were n cities in China and Qin Shi Huang wanted them all be connected by n - 1 roads, in order that he could go to every city from the capital city Xianyang. Although Qin Shi Huang was a tyrant, he wanted the total length of all roads to be minimum,so that the road system may not cost too many people's life. A daoshi (some kind of monk) named Xu Fu told Qin Shi Huang that he could build a road by magic and that magic road would cost no money and no labor. But Xu Fu could only build ONE magic road for Qin Shi Huang. So Qin Shi Huang had to decide where to build the magic road. Qin Shi Huang wanted the total length of all none magic roads to be as small as possible, but Xu Fu wanted the magic road to benefit as many people as possible -- So Qin Shi Huang decided that the value of A/B (the ratio of A to B) must be the maximum, which A is the total population of the two cites connected by the magic road, and B is the total length of none magic roads.
Would you help Qin Shi Huang?
A city can be considered as a point, and a road can be considered as a line segment connecting two points.
Solution
枚举加特效的一条边(u,v),然后通过预处理实现O(1)得到(u,v)上最大边。
白书例题,类似次小生成树的运用。
Code
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 using namespace std; 6 const int maxn=1005; 7 8 int f[maxn][maxn],p[maxn]; 9 int x[maxn],y[maxn],c[maxn]; 10 int find(int x){return p[x]==x?x:p[x]=find(p[x]);} 11 struct edge{ 12 int u,v,w; 13 bool operator<(const edge&a) 14 const {return w<a.w;} 15 }g[maxn*maxn]; 16 int head[maxn],e[maxn*2],w[maxn*2],nxt[maxn*2],k; 17 void adde(int u,int v,int g){ 18 e[++k]=v;w[k]=g;nxt[k]=head[u];head[u]=k; 19 e[++k]=u;w[k]=g;nxt[k]=head[v];head[v]=k; 20 } 21 int dist(int a,int b){ 22 return (x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]); 23 } 24 int n,m; 25 26 int q[maxn],clock; 27 void dfs(int p,int u){ 28 q[++clock]=u; 29 for(int i=head[u];i;i=nxt[i]){ 30 int v=e[i]; 31 if(v==p) continue; 32 for(int j=1;j<=clock;j++) 33 f[v][q[j]]=f[q[j]][v]=max(f[u][q[j]],w[i]); 34 dfs(u,v); 35 } 36 } 37 38 void clear(){ 39 m=k=clock=0; 40 memset(head,0,sizeof(head)); 41 memset(e,0,sizeof(e)); 42 memset(w,0,sizeof(w)); 43 memset(nxt,0,sizeof(nxt)); 44 memset(f,0,sizeof(f)); 45 } 46 47 int main(){ 48 int T; 49 scanf("%d",&T); 50 while(T--){ 51 clear(); 52 scanf("%d",&n); 53 for(int i=1;i<=n;i++) 54 scanf("%d%d%d",&x[i],&y[i],&c[i]),p[i]=i; 55 56 for(int i=1;i<=n;i++) 57 for(int j=i+1;j<=n;j++){ 58 m++; 59 g[m].u=i,g[m].v=j; 60 g[m].w=dist(i,j); 61 } 62 sort(g+1,g+m+1); 63 64 double sum=0; 65 for(int i=1;i<=m;i++){ 66 int x=find(g[i].u),y=find(g[i].v); 67 if(x!=y){ 68 adde(g[i].u,g[i].v,g[i].w); 69 sum+=sqrt(g[i].w); 70 p[x]=y; 71 } 72 if(k==2*(n-1)) break; 73 } 74 75 dfs(0,1); 76 77 double ans=0; 78 for(int u=1;u<=n;u++) 79 for(int v=u+1;v<=n;v++){ 80 double ansx=sum; 81 ansx-=sqrt(f[u][v]); 82 ansx=(c[u]+c[v])*1.0/ansx; 83 ans=max(ans,ansx); 84 } 85 printf("%.2lf\n",ans); 86 } 87 return 0; 88 }