Picnic Planning POJ

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

题意:park最多连接k次,求一个最小不超过k度的生成树
思路:先将park点排除,构造生成树(可能是森林,s个联通块),再将park点加入,使之前的联通块联通
然后park点还剩k-s条边可以外连,这时候当我们再次任意连接其外的任何一点,都会形成一个环,那么就应该去除该环内
权值最大的边,利用dfs扫描各个联通块,记录从1到该点的路径中最大的一条的编号和权值。之后就进行删除操作,就此重复
  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<cmath>
  5 #include<queue>
  6 #include<map>
  7 #include<iostream>
  8 using namespace std;
  9 
 10 map<string,int>mp;
 11 struct Node
 12 {
 13     int x,y,val;
 14 }node[250],dist[40];
 15 int fa[40];
 16 struct E
 17 {
 18     int y,next,val;
 19 }edge[450];
 20 int n,cnt,tot,head[40],k,tot2;
 21 bool maps[40][40];
 22 void add(int x,int y,int val)
 23 {
 24     edge[++tot].y=y;
 25     edge[tot].val=val;
 26     edge[tot].next=head[x];
 27     head[x]=tot;
 28 }
 29 
 30 bool cmp(Node a,Node b)
 31 {
 32     return a.val < b.val;
 33 }
 34 
 35 int Find(int x)
 36 {
 37     return fa[x]==x?x:fa[x]=Find(fa[x]);
 38 }
 39 
 40 void dfs(int s,int pre)
 41 {
 42     for(int i=head[s];i;i=edge[i].next)
 43     {
 44         int to = edge[i].y;
 45         if(maps[s][to] && !dist[to].val)
 46         {
 47             if(edge[i].val < dist[s].val)dist[to] = dist[s];
 48             else
 49             {
 50                 dist[to].val = edge[i].val;
 51                 dist[to].y = to;
 52                 dist[to].x=s;
 53             }
 54             dfs(to,s);
 55         }
 56     }
 57 }
 58 int main()
 59 {
 60     cin>>n;
 61     memset(maps,0,sizeof(maps));
 62     mp["Park"] = 1;
 63     tot = tot2 = 0;
 64     cnt = 1;
 65     for(int i=1;i<=n;i++)
 66     {
 67         string name1,name2;
 68         int val;
 69         cin>>name1>>name2>>val;
 70         if(!mp[name1])mp[name1] = ++cnt;
 71         if(!mp[name2])mp[name2] = ++cnt;
 72         add(mp[name1],mp[name2],val);
 73         node[++tot2].x=mp[name1];
 74         node[tot2].y=mp[name2];
 75         node[tot2].val=val;
 76         add(mp[name2],mp[name1],val);
 77     }
 78     for(int i=1;i<=cnt;i++)fa[i]=i;
 79     sort(node+1,node+1+tot2,cmp);
 80     scanf("%d",&k);
 81     int ans=0;
 82     for(int i=1;i<=tot2;i++)
 83     {
 84         int x=node[i].x;
 85         int y=node[i].y;
 86         if(x == 1 || y == 1)continue;
 87         int fx=Find(x);
 88         int fy=Find(y);
 89         if(fx != fy)
 90         {
 91             maps[x][y] = maps[y][x] = 1;
 92             fa[fx]=fy;
 93             ans += node[i].val;
 94         }
 95     }
 96     for(int i=1;i<=tot2;i++)
 97     {
 98         int x=node[i].x;
 99         int y=node[i].y;
100         if(x != 1 && y != 1)continue;
101         int fx=Find(x);
102         int fy=Find(y);
103         if(fx!=fy)
104         {
105             maps[x][y] = maps[y][x] = 1;
106             fa[fx]=fy;
107             ans+=node[i].val;
108             k--;
109         }
110     }
111     while(k--)
112     {
113         memset(dist,0,sizeof(dist));
114         dfs(1,0);
115         int minn = 0x3f3f3f3f;
116         int id=0;
117         for(int i=head[1];i;i=edge[i].next)
118         {
119             int to = edge[i].y;
120             if(maps[1][to])continue;
121             if(minn > edge[i].val - dist[to].val)
122             {
123                 minn = edge[i].val - dist[to].val;
124                 id = i;
125             }
126         }
127         if(minn >=0)break;
128         int to = edge[id].y;
129         maps[1][to] = maps[to][1] = 1;
130         maps[dist[to].x][dist[to].y] = maps[dist[to].y][dist[to].x] = 0;
131         ans += minn;
132     }
133     printf("Total miles driven: %d
",ans);
134 }
View Code



原文地址:https://www.cnblogs.com/iwannabe/p/10748248.html