Problem:
AYBABTU

There is a tree that has n nodes and n-1 edges.
There are military bases on t out of the n nodes.
We want to disconnect the bases as much as possible by destroying k edges.
The tree will be split into k+1 regions when we destroy k edges.
Given the purpose to disconnect the bases, we only consider to split in a way that each of these k+1 regions has at least one base.
When we destroy an edge, we must pay destroying cost.
Find the minimum destroying cost to split the tree.

Input

The input consists of multiple data sets.
Each data set has the following format.
The first line consists of three integers n, t, and k (1 \leq n \leq 10,000, 1 \leq t \leq n, 0 \leq k \leq t-1).
Each of the next n-1 lines consists of three integers representing an edge.
The first two integers represent node numbers connected by the edge.
A node number is a positive integer less than or equal to n.
The last one integer represents destroying cost.
Destroying cost is a non-negative integer less than or equal to 10,000.
The next t lines contain a distinct list of integers one in each line, and represent the list of nodes with bases.
The input ends with a line containing three zeros, which should not be processed.

Output

For each test case, print its case number and the minimum destroying cost to split the tree with the case number.

Sample Input

2 2 1
1 2 1
1
2
4 3 2
1 2 1
1 3 2
1 4 3
2
3
4
0 0 0

Output for the Sample Input

Case 1: 1
Case 2: 3

Solution in C++ (judge verdict: Runtime Error)

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

const int MAX_V = 100000;
typedef pair<int,int> P;
struct edge{ int to,cost; };

// グラフ (隣接リスト)
vector<edge> G[MAX_V];
// e = (u,v) , is_cost[e] := コスト
map<P,int> is_cost;
// base[u] == true のとき ノード u は ベース
bool base[MAX_V];
// (cost,(u,v))の組
vector< pair<int ,P> > a;
// used[u] == true のとき ノード u は探索済み
bool used[MAX_V];

int n, t, k;


// 初期化
void init(){
	a.clear();
	is_cost.clear();
	for(int i=0 ; i < MAX_V ; i++ ){
		base[i] = false;
		G[i].clear();
	}
}

// グラフに重みcostのエッジ(u,v)を追加
void add_edge(int u, int v, int cost){
	edge e;
	e.to = v;
	e.cost = cost;
	G[u].push_back( e );
	e.to = u;
	G[v].push_back( e );
}

// グラフのエッジ(u,v)を切断
void cut_edge(int u, int v){
	for(vector<edge>::iterator it = G[u].begin() ; it != G[u].end() ; ++it ){
		if( (*it).to == v ){
			G[u].erase( it );
			return ;
		}
	}
}


// ノード u の木にベースがあるかどうか
void is_base(int u, bool& flag){
	
	// ノード u がベースのとき
	if( base[u] || flag ){
		flag = true;
		return;
	}
	// ノード u が探索済みのとき
	if( used[u] ) return;
	used[u] = true;
	
	for(int i=0 ; i < G[u].size() ; i++ ){
		edge e = G[u][i];
		is_base( e.to , flag );
	}
}

int solve(){
	
	// 連結成分の数
	int p=1;
	// コストの総和
	int sum = 0;
	
	sort( a.begin() , a.end() );
	for(int i=0 ; i < a.size() ; i++ ){
		int cost = a[i].first;
		int u = a[i].second.first;
		int v = a[i].second.second;
		if( u > v ) swap(u,v);
		
		cut_edge( u , v );
		
		bool flag1 = false, flag2 = false;
		for(int j=0 ; j < MAX_V ; j++ ) used[j] = false;
		//is_base( u , flag1 );
		for(int j=0 ; j < MAX_V ; j++ ) used[j] = false;
		//is_base( v , flag2 );
		//flag1 = flag2 = true;
		
		// u,vの木の両方にベースを含むとき切断する
		if( flag1 && flag2 ){
			//sum += is_cost[P(u,v)];
			p++;
		}else{
			//add_edge( u , v , is_cost[P(u,v)] );
		}
		
		// 連結成分が K+1 個になったらループを抜ける
		if( p == k+1 ) break;
	}
	return sum;
}

int main(){
	for(int t_ = 1 ; cin >> n >> t >> k , (n || t || k) ; t_++ ){
		// 初期化
		init();
		
		for(int i = 0 ; i < n-1 ; i++ ){
			int u, v, cost;
			cin >> u >> v >> cost;
			u--; v--;
			if( u > v ) swap(u,v);
			
			is_cost[P(u,v)] = cost;
			add_edge( u , v , cost );
			pair<int,P> p( cost , P(u,v) );
			a.push_back( p );
		}
		for(int i = 0 ; i < t ; i++ ){
			int b;
			cin >> b;
			b--;
			base[b] = true;
		}
		
		int ans = solve();
		cout << "Case " << t_ << ": " << ans << endl;
	}
}