Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"log"
	"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))
		log.Printf("%+v\n", 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>

using namespace std;

struct item
{
	int id;
	int price;
};

struct recipe
{
	int id;
	int size;
	int price;
	int ingre[100];
};

item I[100];
recipe R[100];
string T[100];

int solve(int id, int n, int m);

int main(int argc, char** argv)
{
	
	string name, o, q;
	int n, m, p, k;
	
	while( 1 )
	{
		cin >> n;
		if(n==0) break;
		for(int i=0; i<n; i++)
		{
			cin >> T[i];
			I[i].id = i;
			cin >> I[i].price;
		}
		
		cin >> m;
		for(int i=0; i<m; i++)
		{
			R[i].price = 0;
			cin >> name;
			for(int j=0; j<n; j++)
			{
				if(T[j]==name)
				{
					R[i].id = j;
				}
			}
			cin >> R[i].size;
			
			for(int j=0; j<R[i].size; j++)
			{
				cin >> name;
				for(int k=0; k<n; k++)
				{
					if(T[k]==name)
					{
						R[i].ingre[j] = k;
						R[i].price += I[k].price;
					}
				}
			}
		}
		
		cin >> name;
		for(int i=0; i<n; i++)
		{
			if(name==T[i])
			{
				cout << solve(i, n, m) << endl;
			}
		}
	}
	
	return 0;
}

int solve(int id, int n, int m)
{
	int money = 0;
	bool is_proc = false;
	
	for(int j=0; j<m; j++)
	{
		if(R[j].id == id)
		{
			if(I[id].price >= R[j].price)
			{
				is_proc = true;
				money = 0;
				for(int k=0; k<R[j].size; k++)
				{
					money += solve(R[j].ingre[k], n, m);
				}
			}
		}
	}
	
	if(is_proc) return money;
	else return I[id].price;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
	while(true){
		int N;
		scanf("%d",&N);
		if(N==0)return 0;
		int price[100];
		map<string,int>Map;
		for(int i=0;i<N;i++){
			string S;
			cin >> S;
			Map[S]=i;
			int P;
			scanf("%d",&P);
			price[i]=P;
		}
		int M;
		scanf("%d",&M);
		int gou[100][100];
		int K[100];
		for(int i=0;i<N;i++){
			K[i]=-1;
		}
		for(int i=0;i<M;i++){
			string S;
			cin >> S;
			scanf("%d",&K[Map[S]]);
			for(int j=0;j<K[Map[S]];j++){
				string S1;
				cin >> S1;
				gou[Map[S]][j]=Map[S1];
			}
		}
		while(true){
			bool F=false;
			for(int i=0;i<N;i++){
				if(K[i]==-1)continue;
				int sum=0;
				for(int j=0;j<K[i];j++){
					sum+=price[gou[i][j]];
				}
				if(sum<price[i]){
					F=true;
					price[i]=sum;
				}
			}
			if(!F)break;
		}
		string S;
		cin >> S;
		printf("%d\n",price[Map[S]]);
	}
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <algorithm>
int n;

struct item{
    string name;
    int cost;
    vector<string> used;
    int size;
} ;

item list[100];
int memo[100];

int getCost(int v)
{
    if(memo[v]!=-1)return memo[v];
    int i, j;
    int ans;
    
    if (list[v].size == 0){
        return (list[v].cost);
    }
    else {
        ans = 0;
        for (i = 0; i < list[v].size; i++){
            for (j = 0; j < n; j++){
                if (list[v].used[i]== list[j].name){
                    ans += getCost(j);
                }
            }
        }
        list[v].cost = min(list[v].cost,ans);
    }
    return (list[vertex].cost);
}

int main(void)
{
    int i, j, k;
    int m, kk;
    string key, temp;
    int look, sum;
    
    while (1){
        scanf("%d", &n);
        
        if (n == 0){
            break;
        }
        for(i=0;i<n;i++)memo[i]=-1;
        for (i = 0; i < n; i++){
            cin >> list[i].name >> list[i].cost;
            list[i].size = 0;
        }
        
        cin >> m;
        for (i = 0; i < m; i++){
            cin >> key;
            for (j = 0; j < n; j++){
                if (key==list[j].name){
                    look = j;
                }
            }
            cin >> kk;
            list[look].size = kk;
            for (j = 0; j < kk; j++){
                cin >> temp;
                for (k = 0; k < n; k++){
                    if (temp==list[k].name){
                        list[look].used[j]=temp;
                    }
                }
            }
        }
        cin >> key;
        for (j = 0; j < n; j++){
            if (strcmp(key, list[j].name) == 0){
                look = j;
            }
        }
        
        cout << getCost(look);
        
    }
    
    return (0);
}

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

map<string,int> data;
map<string,map<string,int> >tree;

int cal(string str){
	int r=data[str];
	int sum=-1;
	map<string,int>::iterator itr=tree[str].begin();
	for(;itr!=tree[str].end();itr++){
		sum+=cal(itr->first);
	}
	if(sum!=-1)return min(sum+1,r);
	else return r; 
}

int main(){
	int n;
	
	while(cin>>n && n){
		tree.clear();
		string str;
		int cost;
		for(int i=0;i<n;i++){
			cin>>str>>cost;
			data[str]=cost;
		}
		int m;
		cin>>m;
		for(int i=0;i<m;i++){
			string in;
			int num;
			cin>>in>>num;
			for(int j=0;j<num;j++){
				string tmp;
				cin>>tmp;
				tree[in][tmp]=1;
			}
		}
		
		string ma;
		cin>>ma;
		
		cout<<cal(ma)<<endl;
		
		
	}
	
}

C++: #include<iostream>
#include<map>
#include<string>
using namespace std;int main(){int n,m,p;string s,t;for(;cin>>n,n;){map<string,int>P;for(;n--;cin>>P[s])cin>>s;for(cin>>n;n--;){cin>>s>>m;for(p=0;m--;p+=P[t])cin>>t;P[s]=min(P[s],p);}cin>>s;cout<<P[s]<<endl;}}

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

int main(){
	int n, m, k, cost;
	string item;

	while( cin >> n , n ){
		map<string,int> Table;
		for(int i=0 ; i<n ; ++i ){
			cin >> item >> cost;
			Table[ item ] = cost;
		}
		cin >> m;
		for(int i=0 ; i<m ; ++i ){
			vector<string> vc;
			cin >> item >> k;

			for(int j=0 ; j<k ; ++j ){
				string s;
				cin >> s;
				vc.push_back( s );
			}
			int sum = 0;
			for(int j=0 ; j<(int)vc.size() ; ++j ){
				sum += Table[ vc[j] ];
			}
			Table[ item ] = min( sum , Table[ item ] );
		}
		string obj;
		cin >> obj;
		cout << Table[ obj ] << endl;
	}
}