Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	distance_table := [][]int{
		{0, 6, 13, 18, 23, 43, 58},
		{6, 0, 7, 12, 17, 37, 52},
		{13, 7, 0, 5, 10, 30, 45},
		{18, 12, 5, 0, 5, 25, 40},
		{23, 17, 10, 5, 0, 20, 35},
		{43, 37, 30, 25, 20, 0, 15},
		{58, 52, 45, 40, 35, 15, 0},
	}
	toll_table := [][]int{
		{0, 300, 500, 600, 700, 1350, 1650},
		{300, 0, 350, 450, 600, 1150, 1500},
		{500, 350, 0, 250, 400, 1000, 1350},
		{600, 450, 250, 0, 250, 850, 1300},
		{700, 600, 400, 250, 0, 600, 1150},
		{1350, 1150, 1000, 850, 600, 0, 500},
		{1650, 1500, 1350, 1300, 1150, 500, 0},
	}
	for scanner.Scan() {
		d, _ := strconv.Atoi(scanner.Text())
		if d == 0 {
			break
		}
		d--
		scanner.Scan()
		dbuf := strings.Split(scanner.Text(), " ")
		dh, _ := strconv.Atoi(dbuf[0])
		dm, _ := strconv.Atoi(dbuf[1])
		dtime := 100*dh+dm
		scanner.Scan()
		a, _ := strconv.Atoi(scanner.Text())
		a--
		scanner.Scan()
		abuf := strings.Split(scanner.Text(), " ")
		ah, _ := strconv.Atoi(abuf[0])
		am, _ := strconv.Atoi(abuf[1])
		atime := 100*ah+am
		toll := toll_table[d][a]
		if distance_table[d][a] <= 40 &&
			1730 <= dtime && dtime <= 1930 ||
			1730 <= atime && atime <= 1930 {
				toll = ((toll/2-1)/50+1)*50
		}
		fmt.Println(toll)
	}
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp make_pair
#define ERASE(v,i) (v).erase(remove(all(v),i),(v).end())
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define debug(x) cerr<<#x<<" = "<<(x)<<endl;
#define LINE cerr<<"LINE: "<<__LINE__<<endl;
inline int toInt(string s){int v;istringstream i(s);i>>v;return v;}
template<class T> inline string toString(T x){ostringstream o;o<<x;return o.str();}
template<class T> void pv(T a,T b){for(T it=a;it!=b;++it)cerr<<*it<<" ";cerr<<endl;}
const int INF = 1000000000;
const double PI = acos(-1.0), EPS = 1e-10;

int years[] = {9, 19, 29, 39, 49, 59, INF};
const int CNT = 7;

inline int stamp(int h, int m) {
	return 60 * h + m;
}

inline int wari(int p) {
	int np = p;
	np /= 2;
	if(np % 50 != 0) np = (np - (np % 50)) + 50;
	return np;
}

// cost[a][b] (b > a)
int cost[7][7] = {
	{0, 300, 500, 600, 700, 1350, 1650},
	{0, 0, 350, 450, 600, 1150, 1500},
	{0, 0, 0, 250, 400, 1000, 1350},
	{0, 0, 0, 0, 250, 850, 1300},
	{0, 0, 0, 0, 0, 600, 1150},
	{0, 0, 0, 0, 0, 0, 500},
	{0, 0, 0, 0, 0, 0, 0}
};

// dist[a][b] (a > b)
int dist[7][7] = {
	{0, 0, 0, 0, 0, 0, 0},
	{6, 0, 0, 0, 0, 0, 0},
	{13, 7, 0, 0, 0, 0},
	{18, 12, 5, 0, 0, 0, 0},
	{23, 17, 10, 5, 0, 0, 0},
	{43, 37, 30, 25, 20, 0, 0},
	{58, 52, 45, 40, 35, 15, 0}
};

int main() {
#if 0
	for(int p; cin >> p;)
		cout << wari(p) << endl;
#endif

#if 1
	for(;;) {

		int from, to, from_t, to_t;
		int h, m;

		cin >> from;

		if(from == 0) break;

		cin >> h >> m;
		from_t = stamp(h, m);

		cin >> to;

		cin >> h >> m;
		to_t = stamp(h, m);

		from--, to--;

		int C = cost[min(from, to)][max(from, to)];
		int D = dist[max(from, to)][min(from, to)];

		if(D <= 40 && stamp(17, 30) <= stamp(h, m) && stamp(h, m) <= stamp(19, 30)) {
			C = wari(C);
		}
		cout << C << endl;
	}
#endif

	return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int main() {
	vector< vector<P> > data(8, vector<P>(8, P(0, 0)));
	data[1][2] = data[2][1] = P(6, 300);
	data[1][3] = data[3][1] = P(13, 500);
	data[1][4] = data[4][1] = P(18, 600);
	data[1][5] = data[5][1] = P(23, 700);
	data[1][6] = data[6][1] = P(43, 1350);
	data[1][7] = data[7][1] = P(58, 1650);
	data[2][3] = data[3][2] = P(7, 350);
	data[2][4] = data[4][2] = P(12, 450);
	data[2][5] = data[5][2] = P(17, 600);
	data[2][6] = data[6][2] = P(37, 1150);
	data[2][7] = data[7][2] = P(52, 1500);
	data[3][4] = data[4][3] = P(5, 250);
	data[3][5] = data[5][3] = P(10, 400);
	data[3][6] = data[6][3] = P(30, 1000);
	data[3][7] = data[7][3] = P(45, 1350);
	data[4][5] = data[5][4] = P(5, 250);
	data[4][6] = data[6][4] = P(25, 850);
	data[4][7] = data[7][4] = P(40, 1300);
	data[5][6] = data[6][5] = P(20, 600);
	data[5][7] = data[7][5] = P(35, 1150);
	data[6][7] = data[7][6] = P(15, 500);
	int s, g;
	while (cin >> s) {
		if (s == 0) {
			break;
		}
		int sh, sm;
		cin >> sh >> sm;
		int gh, gm;
		cin >> g;
		cin >> gh >> gm;
		int mm = data[s][g].second;
		bool hantei = true;
		if (sh == 18) {
			if (mm % 100 == 50) {
				mm += 50;
			}
			mm /= 2;
			hantei = false;
		} else if (sh == 17) {
			if (sm >= 30) {
				if (mm % 100 == 50) {
					mm += 50;
				}
				mm /= 2;
				hantei = false;
			}
		} else if (sh == 19) {
			if (sm <= 30) {
				if (mm % 100 == 50) {
					mm += 50;
				}
				mm /= 2;
				hantei = false;
			}
		}
		if (hantei) {
			if (gh == 18) {
				if (mm % 100 == 50) {
					mm += 50;
				}
				mm /= 2;
			} else if (gh == 17) {
				if (gm >= 30) {
					if (mm % 100 == 50) {
						mm += 50;
					}
					mm /= 2;
				}
			} else if (gh == 19) {
				if (gm <= 30) {
					if (mm % 100 == 50) {
						mm += 50;
					}
					mm /= 2;
				}
			}
		}
		cout << mm << endl;
	}
	return 0;
}

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

int dist[] = {0, 6, 13, 18, 23, 43, 58};
int cost[][7] = {
	{   0,  300,  500,  600,  700, 1350, 1650},
	{ 300,    0,  350,  450,  600, 1150, 1500},
	{ 500,  350,    0,  250,  400, 1000, 1350},
	{ 600,  450,  250,    0,  250,  850, 1300},
	{ 700,  600,  400,  250,    0,  600, 1150},
	{1350, 1150, 1000,  850,  600,    0,  500},
	{1650, 1500, 1350, 1300, 1150,  500,    0},};

int main() {
	int s, g;
	while(cin >> s, s) {
		int h, m;
		bool half = false;
		cin >> h >> m;
		if(17 * 60 + 30 <= h * 60 + m && h * 60 + m <= 18 * 60 + 30)
			half = true;
		cin >> g >> h >> m;
		if(17 * 60 + 30 <= h * 60 + m && h * 60 + m <= 18 * 60 + 30)
			half = true;

		half &= abs(dist[s-1] - dist[g-1]) <= 40;

		if(half)
			cout << ((cost[s-1][g-1] / 50) + 1) / 2 * 50 << endl;
		else
			cout << cost[s-1][g-1] << endl;
	}
}

C++: #include <iostream>

using namespace std;

int a[7][7] = {
	{  0,   300,  500,  600,  700, 1350, 1650},
	{ 300,    0,  350,  450,  600, 1150, 1500},
	{ 500,  350,    0,  250,  400, 1000, 1350},
	{ 600,  450,  250,    0,  250,  850, 1300},
	{ 700,  600,  400,  250,    0,  600, 1150},
	{1350, 1150, 1000,  850,  600,    0, 500},
	{1650, 1500, 1350, 1300, 1150,  500,   0}
};

int d[7][7] = {
	{ 0,  6, 13, 18, 23, 43, 58},
	{ 6,  0,  7, 12, 17, 37, 52},
	{13,  7,  0,  5, 10, 30, 45},
	{18, 12,  5,  0,  5, 25, 40},
	{23, 17, 10,  5,  0, 20, 35},
	{43, 37, 30, 25, 20,  0, 15},
	{58, 52, 45, 40, 35, 15,  0}
};

int main(int argc, char **argv)
{
	int m1, s1, m2, s2, ic1, ic2;
	while(1){
		cin >> ic1;
		if(ic1 == 0){
			break;
		}
		cin >> m1 >> s1;
		cin >> ic2;
		cin >> m2 >> s2;
		ic1--;
		ic2--;
		if(!((m1 == 17 && s1 >= 30)||(m1 == 18)||(m1 == 19 && s1 <= 30))
		&&!((m2 == 17 && s2 >= 30)||(m2 == 18)||(m2 == 19 && s2 <= 30))){
			cout << a[ic1][ic2] << endl;
			continue;
		}
		
		if(d[ic1][ic2] <= 40){
			m1 = a[ic1][ic2]/2;
			m2 = m1 % 50;
			if(m2 != 0){
				m1 = m1 - m2 + 50;
			}
			cout << m1 << endl;
		} else {
			cout << a[ic2][ic1] << endl;
		}
		
	}
	return 0;
}