【转】poj1639+最小k度限制生成树
没看懂。。。留着慢慢研究。。。。
Description
The Contortion Brothers are a famous set of circus clowns, known worldwide for their incredible ability to cram an unlimited number of themselves into even the smallest vehicle. During the off-season, the brothers like to get together for an Annual Contortionists Meeting at a local park. However, the brothers are not only tight with regard to cramped quarters, but with money as well, so they try to find the way to get everyone to the party which minimizes the number of miles put on everyone's cars (thus saving gas, wear and tear, etc.). To this end they are willing to cram themselves into as few cars as necessary to minimize the total number of miles put on all their cars together. This often results in many brothers driving to one brother's house, leaving all but one car there and piling into the remaining one. There is a constraint at the park, however: the parking lot at the picnic site can only hold a limited number of cars, so that must be factored into the overall miserly calculation. Also, due to an entrance fee to the park, once any brother's car arrives at the park it is there to stay; he will not drop off his passengers and then leave to pick up other brothers. Now for your average circus clan, solving this problem is a challenge, so it is left to you to write a program to solve their milage minimization problem.
Input
Input will consist of one problem instance. The first line will contain a single integer n indicating the number of highway connections between brothers or between brothers and the park. The next n lines will contain one connection per line, of the form name1 name2 dist, where name1 and name2 are either the names of two brothers or the word Park and a brother's name (in either order), and dist is the integer distance between them. These roads will all be 2-way roads, and dist will always be positive.The maximum number of brothers will be 20 and the maximumlength of any name will be 10 characters.Following these n lines will be one final line containing an integer s which specifies the number of cars which can fit in the parking lot of the picnic site. You may assume that there is a path from every brother's house to the park and that a solution exists for each problem instance.
Output
Output should consist of one line of the form Total miles driven: xxx where xxx is the total number of miles driven by all the brothers' cars.
Sample Input
10 Alphonzo Bernardo 32 Alphonzo Park 57 Alphonzo Eduardo 43 Bernardo Park 19 Bernardo Clemenzi 82 Clemenzi Park 65 Clemenzi Herb 90 Clemenzi Eduardo 109 Park Herb 24 Herb Eduardo 79 3
Sample Output
Total miles driven: 183
Source
黑书上的例题,所以题意就不啰嗦了,具体模型是求一个无向图的最小生成树,其中有一个点的度有限制(假设为 k)。
要求最小 k 度生成树,我们可以按照下面的步骤来做:
设有度限制的点为 V0 ,V0称为根节点
1,把所有与 V0 相连的边删去,图会分成多个子图(假设为 m 个,显然的,如果 m > k,那么问题无解),让他们分别求最小生成树;然后用最小的代价将 m 个最小生成树和 V0 连起来,那我们就得到了一棵关于 V0 的最小 m 度生成树。
2,在 m 度生成树中找一个点和 V0 相连(设这条边的权值为 a),会生成一个环,为了满足最小生成树的要求,我们必须删掉一条边(设这条边的权值为 b),以使总权值尽量小,那么就要求 a 尽量的小,b 尽量的大。
完成一次 2 的操作后得到的是 m+1 度最小生成树,以此类推,直到得到最小 k 度生成树。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<map> 5 #include<climits> 6 #include<queue> 7 8 using namespace std; 9 10 const int N=30; 11 12 struct node{ 13 int v,cap; 14 node(){} 15 node(int _v,int _cap):v(_v),cap(_cap){} 16 bool operator < (const node &a) const{ 17 return a.cap<cap; 18 } 19 }; 20 21 map<string,int> mp; 22 int g[N][N],dis[N],clo[N],pre[N],fst[N],max_side[N]; 23 int n,m,k; 24 25 int Prim(int src,int id){ 26 priority_queue<node> q; 27 while(!q.empty()) 28 q.pop(); 29 dis[src]=0; 30 q.push(node(src,0)); 31 int ans=0; 32 while(!q.empty()){ 33 node cur=q.top(); 34 q.pop(); 35 int u=cur.v; 36 if(!clo[u]){ 37 clo[u]=id; 38 ans+=dis[u]; 39 for(int i=1;i<n;i++) 40 if(!clo[i] && g[u][i]!=0 && dis[i]>g[u][i]){ //满足松弛条件 41 pre[i]=u; 42 dis[i]=g[u][i]; 43 q.push(node(i,dis[i])); 44 } 45 } 46 } 47 return ans; 48 } 49 50 void update(int cur,int last,int maxside){ //也是一个dfs过程,直到搜回到起点,同时完成了max_side[]更新 51 max_side[cur]=maxside>g[cur][last]?maxside:g[cur][last]; 52 for(int i=1;i<n;i++) 53 if(i!=last && g[cur][i]!=0 && (pre[cur]==i || pre[i]==cur)) 54 update(i,cur,max_side[cur]); 55 } 56 57 void Solve(){ 58 int i,res,cnt; 59 for(i=0;i<n;i++){ 60 dis[i]=INT_MAX; 61 clo[i]=pre[i]=fst[i]=0; 62 } 63 res=0,cnt=1; //除去根节点后,图中的连通子图个数,即最小生成树个数 64 for(i=1;i<n;i++) 65 if(!clo[i]) 66 res+=Prim(i,cnt++); 67 for(i=1;i<n;i++){ //找到每个生成树和 Park 最近的点使之和 Park 相连 68 int id=clo[i]; 69 if(g[0][i]!=0 && (!fst[id] || g[0][i]<g[0][fst[id]])) 70 fst[id]=i; 71 } 72 for(i=1;i<cnt;i++){ //把m个生成树上和根节点相连的边加入res,得到关于Park的最小m度生成树 73 res+=g[0][fst[i]]; 74 g[0][fst[i]]=g[fst[i]][0]=0; //之所以用邻接阵就是因为删除边很方便 75 update(fst[i],0,0); 76 } 77 78 /* 79 添删操作:将根节点和生成树中一个点相连,会产生一个环,将这个环上(除刚添的那条边外)权值最大 80 的边删去.由于每次操作都会给总权值带来影响 d=max_side[tmp]-mat[0][tmp],我们需要得到最小生 81 成树,所以我们就要求 d 尽量大 82 */ 83 84 k=k-cnt+1; //接下来重复操作,直到度数满足条件 85 while(k--){ 86 int tmp=0; 87 for(i=1;i<n;i++) //找 d 值最大的点(就是说完成添删操作后可以使总边权减小的值最大) 88 if(g[0][i]!=0 && (tmp==0 || max_side[tmp]-g[0][tmp]<max_side[i]-g[0][i])) 89 tmp=i; 90 if(max_side[tmp]<=g[0][tmp]) //总权值无法再减小 91 break; 92 res=res-max_side[tmp]+g[0][tmp]; 93 g[0][tmp]=g[tmp][0]=0; 94 int p=0; 95 for(i=tmp;pre[i]!=0;i=pre[i]) 96 if(p==0 || g[p][pre[p]]<g[i][pre[i]]) 97 p=i; 98 pre[p]=0; 99 update(tmp,0,0); 100 } 101 printf("Total miles driven: %d\n",res); 102 } 103 104 int main(){ 105 106 //freopen("input.txt","r",stdin); 107 108 char s1[20],s2[20]; 109 int cap; 110 while(~scanf("%d",&m)){ 111 mp["Park"]=0; 112 n=1; 113 memset(g,0,sizeof(g)); 114 while(m--){ 115 scanf("%s %s %d",s1,s2,&cap); 116 if(!mp.count(s1)) 117 mp[s1]=n++; 118 if(!mp.count(s2)) 119 mp[s2]=n++; 120 int u=mp[s1],v=mp[s2]; 121 if(!g[u][v] || g[u][v]>cap) 122 g[u][v]=g[v][u]=cap; 123 } 124 scanf("%d",&k); 125 Solve(); 126 } 127 return 0; 128 }
PS:这道题并不是要求 k 度的最小生成树,而是要求根节点的度在不超过 k 值的情况下,该图的最小生成树。也就是说,不一定要求到 k 度生成树,只要图的总权值不能继续减小我们就可以停下来了。