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
思路:
这也是一道网络流的题目,不过建图是一个难点,自己是看了这篇博客才明白的。因为每个点只能交易一次,所以所有的出售的点和起点连一条流量为1的边,同理所有购买的点和终点连一条边,这样保证每个点只交易一次,因为是求最大的交易数量,因此我们可以将两条边的交易量取负,这样我们只需要求最小值,就转换成最小费用最大流了。购买的城镇和出售的城镇编号相同,所以我设了一个数组表示购买城镇的真实编号(出售城镇的数量加上这个购买城市本身的编号)。起点设为0,终点设为n+m+1。
ac代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<set>
#include<iostream>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#define ll long long
#define mod 1000000007
#define eps 1e-8
using namespace std;
const int MAXN = 10000;
const int MAXM = 100000;
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;//节点总个数,节点编号从 0 ~ N-1
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+1; 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 rb[5005];
int main()
{
int n,m,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<=m+2;i++)
{
rb[i]=i+n;
}
init(n+m+2);
int u,v,w;
for(int i=1;i<=n;i++)
addedge(0,i,1,0);
for(int i=1;i<=m;i++)
addedge(rb[i],n+m+1,1,0);
while(~scanf("%d%d%d",&u,&v,&w),u+v+w)
{
addedge(u,rb[v],1,-w);
}
int c;
minCostMaxflow(0,n+m+1,c);
cout<<-c<<endl;
}
return 0;
}