【SPOJ - SCITIES】Selfish Cities (二分图最优匹配,最大费用流)
題干:
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 0Output: 21 29 22題目大意:(摘抄)
? ? 有A個(gè)城市賣東西,B個(gè)城市買東西,一個(gè)城市只能賣東西給一個(gè)城市,一個(gè)城市只能從一個(gè)城市買東西,問(wèn)最多有多少貨物被交易
解題報(bào)告:
? ?直接造一個(gè)超級(jí)源點(diǎn)超級(jí)匯點(diǎn)建圖跑MCMF就可以了,,注意邊權(quán)取反,,因?yàn)橐茏畲筚M(fèi)用流,答案也取反就行了,,
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;//節(jié)點(diǎn)總個(gè)數(shù),節(jié)點(diǎn)編號(hào)從 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 存的是最小費(fèi)用 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 ; }?
總結(jié)
以上是生活随笔為你收集整理的【SPOJ - SCITIES】Selfish Cities (二分图最优匹配,最大费用流)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 全员4G!疑似华为Mate 50手机壳曝
- 下一篇: 【洛谷 - U43391】不是0-1背包