2013
11-10

# Picnic Planning

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 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 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.

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

Total miles driven: 183

//* @author
import java.util.*;
import java.io.*;
import java.util.concurrent.*;

public class Main {
private static class UFSets {
UFSets(int l) {
s = new int[l];
for(int i=0; i!=l; ++i)
s[i] = i;
}
void union(int x, int y) {
s[find(x)] = s[find(y)];
}

int find(int x) {
if(x>=s.length) while(true) ;
if(s[x]!=x) return s[x] = find(s[x]);
return x;
}

private int[] s;
}

private static class Node extends LinkedBlockingDeque< Edge>{
private static final long serialVersionUID = 1L;
void updatePath(Integer path) {
if(this.path == null || this.path.compareTo(path) > 0)
this.path = path;
}
static Node getNearestNodeFrom(Node e) {
++cnt;
return e.getNearesNode();
}

private Node getNearesNode() {
if(lab==cnt) return this;
lab = cnt;
Node ans = this;
for(Edge e: this) {
Node another = e.target(this).getNearesNode();
if(another.path == null) continue;
if(ans.path == null || ans.path.compareTo(another.path)>0) ans = another;
}
return ans;
}
void update() {
lab = ++cnt;
cut = null;
ans += path;
for(Edge e: this)
e.target(this).update(e);
}

private void update(Edge edge) {
if(lab==cnt) return;
cut = edge;
lab = cnt;
for(Edge e: this)
{
Node v = e.target(this);
if(e.compareTo(edge)>0) v.update(e);
else v.update(edge);
}
}

static int cnt = 0;
int lab = 0;
Integer path = null;
Edge cut;
}

private static class Edge implements Comparable< Edge>{
public int compareTo(Edge e) {
return this.l-e.l;
}
Edge(int cx, int cy, Node x, Node y, int l) {
this.cx = cx;
this.cy = cy;
this.x = x;
this.y = y;
this.l = l;
}
void activate() {
ix = x.iterator();
ix.next();
iy = y.iterator();
iy.next();
ans += l;
}
void disable() {
ix.remove();
iy.remove();
ans -= l;
}
Node target(Node v) {
if(x==v) return y;
return x;
}
final Node x, y;
Iterator< Edge> ix, iy;
final int cx, cy, l;
}

private static final Scanner in = new Scanner(System.in);
private static final Formatter out = new Formatter(System.out);
private static final int m = in.nextInt();
private static List< Node> nodes = new ArrayList< Node>();
private static List< Edge> edges = new ArrayList< Edge>();
private static int ans = 0;
static Map< String, Integer> hash = new HashMap< String, Integer>();
static {
hash.put("Park", 0);
}

static int getHash(String s) {
if(!hash.containsKey(s)) {
hash.put(s, hash.size());
}
return hash.get(s);
}

public static void main(String[] args) {
for(int i=0; i!=m; ++i) {
int x = getHash(in.next());
int y = getHash(in.next());
int l = in.nextInt();
if(y==0) {
if(x==0) continue;
y = x;
x = 0;
}
if(x==0) nodes.get(y).updatePath(l);
else edges.add(new Edge(x, y, nodes.get(x), nodes.get(y), l));
}
Collections.sort(edges);
UFSets s = new UFSets(nodes.size());
for(Edge e: edges) {
if(s.find(e.cx)==s.find(e.cy)) continue;
e.activate();
s.union(e.cx, e.cy);
}
int degree = in.nextInt();
for(int i=1; i!=nodes.size(); ++i) {
if(s.find(i)!=i) continue;
Node v = Node.getNearestNodeFrom(nodes.get(i));
v.update();
--degree;
}
while(degree-->0) {
Node v = null;
for(Node cand: nodes) {
if(cand.path == null) continue;
if(cand.cut == null) continue;
if(v == null || cand.path - cand.cut.l < v.path - v.cut.l) v = cand;
}
if(v==null || v.path - v.cut.l >= 0) break;
v.cut.disable();
v.update();
}
out.format("Total miles driven: %d\n", ans);
}
}

1. 样例输出和程序输出不吻合，修改一下样例输出吧。我用的是VC编译器，会提示我的i和j变量重复定义