题干:

Far, far away there is a world known as Selfishland because of the nature of its inhabitants. Hard times have forced the cities of Selfishland to exchange goods among each other. C1 cities are willing to sell some goods and the other C2 cities are willing to buy some goods (each city can either sell or buy goods, but not both). There would be no problem if not for the selfishness of the cities. Each selling city will sell its goods to one city only, and each buying city will buy goods from one city only. 

Your goal is to connect the selfish cities in such a way that the amount of exchanged goods is maximalized.

Input

The first line contains a positive integer t<=1000 indicating the number of test cases. Each test case is an instance of the problem defined above. The first line of each test case is a pair of positive integers C1 and C2 (the number of cities wanting to sell their goods C1<=100 and the number of cities wanting to buy goods C2<=100). The lines that follow contain a sequence of (c1,c2,g) trios ending with three zeros. (c1,c2,g) means that the city c1 can offer the city c2 the amount of g<=100 goods.

Output

For each test case print the maximal amount of goods exchanged.

Example

Input:
3
3 2
1 1 10
2 1 19
2 2 11
3 2 1
0 0 0
4 4
1 1 6
1 2 6
2 1 8
2 3 9
2 4 8
3 2 8
4 3 7
0 0 0
3 2
1 1 10
2 1 21
2 2 11
3 2 1
0 0 0

Output:
21
29
22

题目大意:(摘抄)

    有A个城市卖东西,B个城市买东西,一个城市只能卖东西给一个城市,一个城市只能从一个城市买东西,问最多有多少货物被交易

解题报告:

   直接造一个超级源点超级汇点建图跑MCMF就可以了,,注意边权取反,,因为要跑最大费用流,答案也取反就行了,,

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;

const int MAXN = 70000;
const int MAXM = 100005;
const int INF = 0x3f3f3f3f;
struct Edge {
	int to,next,cap,flow,cost;
} edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从 1 ~ N
void init(int n) {
	N = n;
	tol = 0;
	memset(head, -1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost) {
	edge[tol].to = v;
	edge[tol].cap = cap;
	edge[tol].cost = cost;
	edge[tol].flow = 0;
	edge[tol].next = head[u];
	head[u] = tol++;
	edge[tol].to = u;
	edge[tol].cap = 0;
	edge[tol].cost = -cost;
	edge[tol].flow = 0;
	edge[tol].next = head[v];
	head[v] = tol++;
}
bool spfa(int s,int t) {
	queue<int>q;
	for(int i = 0; i <= N; i++) {
		dis[i] = INF;
		vis[i] = false;
		pre[i] = -1;
	}
	dis[s] = 0;
	vis[s] = true;
	q.push(s);
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		vis[u] = false;
		for(int i = head[u]; i !=-1; i = edge[i].next) {
			int v = edge[i].to;
			if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge
			        [i].cost ) {
				dis[v] = dis[u] + edge[i].cost;
				pre[v] = i;
				if(!vis[v]) {
					vis[v] = true;
					q.push(v);
				}
			}
		}
	}
	if(pre[t] ==-1)return false;
	else return true;

}
//返回的是最大流,cost 存的是最小费用
int minCostMaxflow(int s,int t,int &cost) {
	int flow = 0;
	cost = 0;
	while(spfa(s,t)) {
		int Min = INF;
		for(int i = pre[t]; i !=-1; i = pre[edge[i^1].to]) {
			if(Min > edge[i].cap-edge[i].flow)
				Min = edge[i].cap-edge[i].flow;
		}
		for(int i = pre[t]; i !=-1; i = pre[edge[i^1].to]) {
			edge[i].flow += Min;
			edge[i^1].flow-= Min;
			cost += edge[i].cost * Min;
		}
		flow += Min;
	}
	return flow;
}
int main()
{
	int n1,n2,n;
	int t;
	cin>>t;
	while(t--) {
	scanf("%d%d",&n1,&n2);
		//int st=0,ed=n1+n2+1;
		int st = 0,ed=n1+n2+1;
		init(ed+1);
		int a,b,w;
		while(scanf("%d%d%d",&a,&b,&w)) {
			if(a+b+w==0) break;
			b+=n1;
//			printf("%d  %d  %d\n",a,b,w);
			addedge(a,b,1,-w);
		}
		for(int i = 1; i<=n1; i++) addedge(st,i,1,0);
		for(int i = n1+1; i<=n1+n2; i++) addedge(i,ed,1,0);
		int cost;
		int ans = minCostMaxflow(st,ed,cost);
		printf("%d\n",-cost);
	}
	return 0 ;
}