Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
		"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		having := map[string]Entry{}
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			name := buf[0]
			price, _ := strconv.Atoi(buf[1])
			having[name] = Entry{price, []string{}}
		}
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			name_, price_ := buf_[0], INF
			if _, ok := having[name_]; ok {
				price_ = having[name_].price
			}
			having[name_] = Entry{price_, buf_[2:]}
		}
		scanner.Scan()
		fmt.Println(calculation(scanner.Text(), having))
	}
}

type Entry struct {
	price int
	elements []string
}

// メモ再帰的
func calculation(target string, having map[string]Entry) int {
	ans := INF
	if len(having[target].elements) > 0 {
		ans = 0
		for _, item := range having[target].elements {
			ans += calculation(item, having)
		}
	}
	return min(ans, having[target].price)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

long long int updateValue( int n, vector< long long int >& valueList, vector< vector< int > >& recipeList, vector< bool >& updatedList )
{
	if( updatedList[ n ] ){
		return valueList[ n ];
	}
	updatedList[ n ] = true;
	vector< int > recipe = recipeList[ n ];
	if( recipe.empty() ){
		return valueList[ n ];
	}
	long long int value = 0;
	for( int i = 0; i < (int)recipe.size(); ++i ){
		int id = recipe[ i ];
		value += updateValue( id, valueList, recipeList, updatedList );
	}
	if( value < valueList[ n ] ){
		valueList[ n ] = value;
	}
	return valueList[ n ];
}

int main()
{
	int n;
	while( cin >> n && n ){
		int id = 0;
		map< string, int > nameList;
		vector< long long int > valueList( n, 0 );
		for( int i = 0; i < n; ++i ){
			string s;
			int v;
			cin >> s >> v;
			int d = id++;
			nameList.insert( make_pair( s, d ) );
			valueList[ d ] = v;
		}
		int m;
		vector< vector< int > > recipeList( n, vector< int >() );
		cin >> m;
		for( int i = 0; i < m; ++i ){
			string o;
			int k;
			cin >> o >> k;
			int count = nameList.count( o );
			if( count == 0 ){
				continue;
			}
			int productId = nameList[ o ];
			for( int j = 0; j < k; ++j ){
				string q;
				cin >> q;
				count = nameList.count( q );
				if( count == 0 ){
					recipeList[ productId ] = vector< int >();
					break;
				}
				int materialId = nameList[ q ];
				recipeList[ productId ].push_back( materialId );
			}
		}
		vector< bool > updatedList( n, false );
		for( int i = 0; i < n; ++i ){
			updateValue( i, valueList, recipeList, updatedList );
		}

		string ansName;
		cin >> ansName;
		cout << valueList[ nameList[ ansName] ] << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int main()
{
	while(1)
	{
		vector<string> menu[105];
		vector<string> vec;
		int n,m;
		cin >> n;
		if(n==0)break;
		map<string,int> ma;
		for(int i=0;i<n;i++)
		{
			string name;
			int cost;
			cin >> name >> cost;
			ma[name]=cost;
		}
		cin >> m;
		for(int i=0;i<m;i++)
		{
			string s;int k;
			cin >> s;
			vec.pb(s);
			cin >> k;
			for(int j=0;j<k;j++)
			{
				string ss;
				cin >> ss;
				menu[i].pb(ss);
			}
		}
		bool update=true;
		while(update)
		{
			update=false;
			for(int i=0;i<m;i++)
			{
				int sum=0;
				for(int j=0;j<menu[i].size();j++)
				{
					sum+=ma[menu[i][j]];
				}
				if(ma[vec[i]]>sum)
				{
					ma[vec[i]]=sum;
					update=true;
				}
			}
		}
		string out;
		cin >> out;
		cout << ma[out] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <map>
#define MAX_N 100
#define MAX_M 100
#define MAX_K 100

using namespace std;

typedef struct _Recipe{
	string NewItemName;
	int SourceItemNumber;
	vector<string> SourceItemName;
}Recipe;

typedef struct _Item{
	string ItemName;
	int Price;
}Item;


int main(void){
	int n, m, min_cost[MAX_N];
	string require;
	Item itm[MAX_N];
	Recipe rcp[MAX_M];
	map<string,int> ItemName_To_Index;
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		for(int i = 0;i < n;i++){
			cin >> itm[i].ItemName >> itm[i].Price;
			min_cost[i] = itm[i].Price;
			ItemName_To_Index.insert(map<string,int>::value_type(itm[i].ItemName,i));
			//cout << "価格 " << itm[i].Price << endl;
		}
		cin >> m;
		for(int i = 0;i < m;i++){
			cin >> rcp[i].NewItemName >> rcp[i].SourceItemNumber;

			for(int j = 0;j < rcp[i].SourceItemNumber;j++){
				string tmp;
				cin >> tmp;
				rcp[i].SourceItemName.push_back(tmp);
			}

		}
		cin >> require;



		for(int i = 0;i < m;i++){
			for(int j = 0;j < m;j++){
				int sum = 0;
				for(int k = 0;k < rcp[j].SourceItemNumber;k++){
					sum += min_cost[ItemName_To_Index[rcp[j].SourceItemName[k]]];
				}
				min_cost[ItemName_To_Index[rcp[j].NewItemName]] = min(sum,min_cost[ItemName_To_Index[rcp[j].NewItemName]]);
			}
		}

		cout << min_cost[ItemName_To_Index[require]] << endl;
		for(int i = 0;i < m;i++){
			rcp[i].SourceItemName.clear();
		}
		ItemName_To_Index.clear();
		/*for(int i = 0;i < n;i++){
			cout << itm[i].ItemName << " 最安値 " << min_cost[i] << endl;
		}*/
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){

  int n;
  while( cin >> n, n ) {
    map<string, int> d;
    for(int i=0; i<n; i++) {
      string s;
      int p;
      cin >> s >> p;
      d[s] = p;
    }

    int m;
    cin >> m;
    for(int i=0; i<m; i++) {
      string s;
      int k, cost = 0;
      cin >> s >> k;
      for(int i=0; i<k; i++) {
	string q;
	cin >> q;
	cost += d[q];
      }

      if( d[s] > cost ) {
	d[s] = cost;
      }
    }

    string s;
    cin >> s;
    cout << d[s] << endl;
  }
}

C++: #include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define min(a, b) (a > b ? b : a)

struct Item {
    int price;
    std::vector<std::string> recipe;
};

int main() {
    std::map<std::string, Item> dict;
    int n;
    while (std::cin >> n && n) {
        std::string s, t;
        int p;
        rep(i, n) {
            std::cin >> s >> p;
            dict[s].price = p;
        }

        int k, l;
        std::cin >> k;
        rep(i, k) {
            std::cin >> s >> l;

            std::getline(std::cin, t);
            std::stringstream ss(t);
            std::copy(std::istream_iterator<std::string>(ss), {},
                      std::back_inserter(dict[s].recipe));
        }

        std::function<int(std::string)> min_cost = [&](std::string name) {
            int res = dict[name].price;
            if (dict[name].recipe.size() == 0) return res;

            int rec = 0;
            for (auto i : dict[name].recipe) rec += min_cost(i);
            return min(res, rec);
        };

        std::cin >> s;
        std::cout << min_cost(s) << std::endl;
        dict.clear();
    }
}