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
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
Each case 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
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
For each test case, the 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
2 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 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 1
Sample Output
Total miles driven: 183 Total miles driven: 255
//复杂度:O(mlogm+E+k*(V+E)...应该是吧 #include<iostream> #include<cstdio> #include<string.h> #include<cstring> #include<vector> #include<set> #include<queue> #include<stack> #include<algorithm> #include<string> #include<math.h> #include<map> using namespace std; #define LL long long const int maxn = 100+10; int n , m , k; struct Edge { Edge() { } Edge(int uu,int vv,int ww) : u(uu) , v(vv) , w(ww) { } int u , v; int w; bool operator < (const Edge&e) const { return w < e.w; } }; vector<Edge> edge; vector<Edge> G; int father[maxn]; int weight[maxn]; int best[maxn]; map<string,int> person; int p[maxn]; bool vis[maxn]; int find(int x) { if (p[x]==x) return x; return p[x] = find(p[x]); } void input() { person.clear(); person["Park"] = n = 1; edge.clear(); G.clear(); int u , v , w; for (int i = 0 ; i < m ; ++i) { string s1 , s2; cin>>s1>>s2; if (person.count(s1)==0) person[s1] = ++n; if (person.count(s2)==0) person[s2] = ++n; u = person[s1] , v = person[s2]; scanf("%d",&w); edge.push_back(Edge(u,v,w)); if (u==1) G.push_back(Edge(u,v,w)); if (v==1) G.push_back(Edge(v,u,w)); } scanf("%d",&k); } void addEdge(const Edge&e) { if (e.u!=1 && father[e.u]==e.u) { father[e.u] = e.v; weight[e.u] = e.w; } else if (father[e.v]==e.v) { father[e.v] = e.u; weight[e.v] = e.w; } else { vector<int> v; int x = e.v; v.push_back(x); while (father[x]!=x) { x = father[x]; v.push_back(x); } for (int i = v.size()-1 ; i > 0 ; --i) { father[v[i]] = v[i-1]; weight[v[i]] = weight[v[i-1]]; } father[e.v] = e.u; weight[e.v] = e.w; } } void RemoveAdd(const Edge& e) { int x = e.u==1 ? e.v : e.u; vector<int> v; while (true) { v.push_back(x); if (weight[x]==best[x]) break; x = father[x]; } for (int i = v.size()-1 ; i > 0 ; --i) { father[v[i]] = v[i-1]; weight[v[i]] = weight[v[i-1]]; } x = e.u==1 ? e.v : e.u; weight[x] = e.w; father[x] = 1; } void dp(int x) { if (father[x]==1 || best[x]!=-1) return; dp(father[x]); best[x] = max(best[father[x]],weight[x]); } void solve() { int ans = 0; sort(edge.begin(),edge.end()); for (int i = 1 ; i <= n ; ++i) p[i] = father[i] = i; for (int i = 0 ; i < m ; ++i) { int u = edge[i].u; int v = edge[i].v; if (u==1 || v==1) continue; u = find(u) , v = find(v); if (u==v) continue; ans += edge[i].w; p[u] = v; addEdge(edge[i]); } sort(G.begin(),G.end()); memset(vis,0,sizeof(vis)); int deg = 0; for (int i = 0 ; i < G.size() ; ++i) { Edge & e = G[i]; if (e.v==1 || vis[find(e.v)]) continue; vis[find(e.v)] = true; ++deg; addEdge(e); ans += e.w; } int sumcost = ans; k = min(k,(int)G.size()); while (deg < k) { int sel = -1; memset(best,-1,sizeof(best)); for (int i = 0 ; i < G.size() ; ++i) { Edge & e = G[i]; if (father[e.v]==1) continue; dp(e.v); if (sel==-1) { sel = i; continue; } if (G[sel].w-best[G[sel].v] > e.w-best[e.v]) sel = i; } sumcost += G[sel].w-best[G[sel].v]; ans = min(ans,sumcost); RemoveAdd(G[sel]); ++deg; } printf("Total miles driven: %d\n",ans); } int main() { int T; cin>>T; while (T--) { scanf("%d",&m); input(); solve(); if (T) printf("\n"); } }