Problem:
Flame of Nucleus

Year 20XX — a nuclear explosion has burned the world. Half the people on the planet have died. Fearful.

One city, fortunately, was not directly damaged by the explosion. This city consists of N domes (numbered 1
through N inclusive) and M bidirectional transportation pipelines connecting the domes. In dome i, Pi citizens
reside currently and there is a nuclear shelter capable of protecting Ki citizens. Also, it takes Di days to travel
from one end to the other end of pipeline i.

It has been turned out that this city will be polluted by nuclear radiation in L days after today. So each citizen
should take refuge in some shelter, possibly traveling through the pipelines. Citizens will be dead if they reach
their shelters in exactly or more than L days.

How many citizens can survive at most in this situation?

Input

The input consists of multiple test cases. Each test case begins with a line consisting of three integers N, M
and L (1 ≤ N ≤ 100, M ≥ 0, 1 ≤ L ≤ 10000). This is followed by M lines describing the configuration of
transportation pipelines connecting the domes. The i-th line contains three integers Ai, Bi and Di (1 ≤ Ai < Bi ≤
N, 1 ≤ Di ≤ 10000), denoting that the i-th pipeline connects dome Ai and Bi. There is at most one pipeline
between any pair of domes. Finally, there come two lines, each consisting of N integers. The first gives P1 , . . .,
PN (0 ≤ Pi ≤ 106) and the second gives K1 , . . ., KN (0 ≤ Ki ≤ 106).

The input is terminated by EOF. All integers in each line are separated by a whitespace.

Output

For each test case, print in a line the maximum number of people who can survive.

Sample Input

1 0 1
51
50
2 1 1
1 2 1
1000 0
0 1000
4 3 5
1 2 4
1 3 1
3 4 2
0 1000 1000 1000
3000 0 0 0

Output for the Sample Input

50
0
3000

Solution in C (judge verdict: Time Limit Exceeded)

// AOJ 2076 Flame of Nucleus
// 2018.3.10 bal4u

#include <stdio.h>
#include <string.h>

/*******************************************/
/* Dinic's Max Flow Algorithm 
/*******************************************/

#define INF 0x01010101
#define VMAX 205
typedef struct { int to; int cap; int rev; } EDGE;
EDGE edge[VMAX][VMAX]; int hi[VMAX];
int V;				// 頂点数
int level[VMAX], iter[VMAX];
int queue[2*VMAX], qtop;

void add_edge(int from, int to, int cap)
{
	int f, t;
	EDGE *e;

	f = hi[from]++, t = hi[to]++;
	e = &edge[from][f], e->to = to, e->cap = cap, e->rev = t; 
	e = &edge[to][t], e->to = from, e->cap = 0, e->rev = f;
}

void bfs(int s)
{
	int i, v;
	EDGE *e;
	
	memset(level, -1, V << 2);
	level[s] = 0;
	queue[0] = s, qtop =1;
	while (qtop) {
		v = queue[--qtop];
		for (i = 0; i < hi[v]; i++) {
			e = &edge[v][i];
			if (e->cap > 0 && level[e->to] < 0) {
				level[e->to] = level[v] + 1;
				queue[qtop++] = e->to;
			}
		}
	}
}
 
int dfs(int v, int t, int f)
{
	int *i, d;
	EDGE *e;
	
	if (v == t) return f;
	for (i = &iter[v]; *i < hi[v]; (*i)++) {
		e = &edge[v][*i];
		if (e->cap > 0 && level[v] < level[e->to]) {
			d = dfs(e->to, t, f <= e->cap? f: e->cap);
			if (d > 0) {
				e->cap -= d;
				edge[e->to][e->rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}
 
int maxFlow(int s, int t)
{
	int f, flow = 0;
	while (1) {
		bfs(s);
		if (level[t] < 0) break;
		memset(iter, 0, V << 2);
		while ((f = dfs(s, t, INF)) > 0) flow += f;
	}
	return flow;
}

/**********************************************/

int D[101][101];
int P[101];
int K[101];

//#define getchar_unlocked()  getchar()
int in()
{
	int n = 0;
	int c = getchar_unlocked();
	do n = (n<<3)+(n<<1) + (c & 0xf), c = getchar_unlocked();
	while (c >= '0');
	return n;
}

int main()
{
	int N, M, L, i, j, k, a, b, d;
	int source, sink;

	while (N = in()) {
		M = in(), L = in();

		memset(D, INF, sizeof(D));
		for (i = 0; i < N; i++) D[i][i] = 0;
		for (i = 0; i < M; i++) {
			a = in()-1, b = in()-1, d = in();
			D[a][b] = D[b][a] = d;
		}
		for (k = 0; k < N; k++) {
			for (i = 0; i < N; i++) if (D[i][k] != INF) {
				for (j = 0; j < N; j++) if (D[k][j] != INF) {
					d = D[i][k] + D[k][j];
					if (D[i][j] > d) D[i][j] = d;
				}
			}
		}
		for (i = 0; i < N; i++) P[i] = in();
		for (i = 0; i < N; i++) K[i] = in();

		source = N << 1, sink = source+1, V = sink+1;
		memset(hi, 0, V << 2);

		for (i = 0; i < N; i++) {
			add_edge(source, i, P[i]);
			for (j = 0; j < N; j++) if (D[i][j] < L) add_edge(i, N+j, INF);
			add_edge(N+i, sink, K[i]);
		}
		
		printf("%d\n", maxFlow(source, sink));
	}
	return 0;
}