ZOJ Problem Set - 3885 The Exchange of Items
飞机票:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3885
题意:给你n对数据Ai,Bi,Ai代表没有当前值,Bi代表目标值,然后有m个交换,Xi,Yi,说名Xi可以变为Yi,或则Yi可以变为Xi,求最小的交换次数,使得n个数据到达目标值。
分析:一看就知道,假设p为Ai-Bi,如果p大于0,那么i这个点必须要交换掉p个数,如果p<0那么就说明i必须要得到p个数,这样的话,如果我们想知道有可行的方案使得在改变后Ai全都变为Bi,我们可以使用最大流,但是这里我们将p>o加到s1里去,p<0加到s2里去,比较s1和s2是否相等,因为不等的话就肯定跑不了。如果相同,具体怎么建图?p大于0,那么就从超级源点加一条流量为p的边到i,如果小于0,那么就从i加一条流量为p的边到超级汇点,然后根据m个可以交换的边,加m条从Xi到Yi的边,和Yi到Xi的边,因为是双向的,且容量为INF,因为没有限制。这样设最大流max_flow,如果max_flow和s1相等,那么就是可行的。关键题目让我们求最少的交换次数,具体的交换次数就是经过Xi和Yi这条边多少次。那么想到这,应该就知道了,可以用最小费用最大流,p大于0,那么就从超级源点加一条流量为p的边到i且花费为0,如果小于0,那么就从i加一条流量为p的边到超级汇点,花费也为0,具体就是m条边的花费,因为经过一次就要加一次交换次数,那么就是流量为INF,花费为1,这样跑一边,如果max_flow和s1相等,那么输出最小花费就是答案,最后啪啪啪就可以AC了
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <algorithm> using namespace std; #define forr(i,f_start,f_end) for(int i=f_start;i<f_end;++i) #define mem(x,i) memset(x,i,sizeof(x)) const int maxn = 123; const int maxm = 123 * 8; typedef long long LL; const int INF = 0xfffffff; int input() { int a; scanf("%d", &a); return a; } int Flow; ///-----------------最小费用流----------------- //cost 代表一个流量要的费用 cap代表容量 //init 到N+2,如果超级汇点就是0,N+1 //else 就是s到t init到多少个点加1 struct Edge { int to, next, cap, flow, cost; } edge[maxm]; struct MCMF { int head[maxn], tol; int pre[maxn], dis[maxn]; bool vis[maxn]; int 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; } } mcmf; ///----------------------------------------- int N, M; int main() { while (scanf("%d%d", &N, &M)!=EOF) { mcmf.init(N+2); int cmp=0; int cmp1=0; for(int i=1; i<=N; i++) { int a,b; scanf("%d%d",&a,&b); int p=a-b; if(p>0) { cmp+=p; mcmf.addedge(0,i,p,0); } else if(p<0) { cmp1-=p; mcmf.addedge(i,N+1,-p,0); } } int a,b; for(int i=1; i<=M; i++) { scanf("%d%d",&a,&b); mcmf.addedge(a,b,INF,1); mcmf.addedge(b,a,INF,1); } int cost; int Flow = mcmf.minCostMaxflow(0, N+1, cost); if(cmp1!=cmp||cmp!=Flow)printf("-1\n"); else printf("%d\n",cost); } return 0; }