下面证明最小割对应取点方案就是最小取点数
当然还有一种是神奇的放大边权方法
建图前,对所有b[i],执行变换b[i]=b[i]*10000-1,然后,会惊异地发现,
此时最大流所对应的方案就是满足辞退最少人数的了。
为什么?显然,变换后的流量r2除以10000后再取整就等于原来的流量,但是
r2的后四位却蕴含了辞退人数的信息:每多辞退一个人,流量就会少1。
上代码
#include <iostream> #include <algorithm> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <queue> #include <map> #include <set> #define MAXN 5555 #define MAXM 555555 #define INF 1000000007 using namespace std; struct node { int ver; // vertex int cap; // capacity int flow; // current flow in this arc int next, rev; }edge[MAXM]; int dist[MAXN], numbs[MAXN], src, des, n; int head[MAXN], e; void add(int x, int y, int c) { //e记录边的总数 edge[e].ver = y; edge[e].cap = c; edge[e].flow = 0; edge[e].rev = e + 1; //反向边在edge中的下标位置 edge[e].next = head[x]; //记录以x为起点的上一条边在edge中的下标位置 head[x] = e++; //以x为起点的边的位置 //反向边 edge[e].ver = x; edge[e].cap = 0; //反向边的初始容量为0 edge[e].flow = 0; edge[e].rev = e - 1; edge[e].next = head[y]; head[y] = e++; } void rev_BFS() { int Q[MAXN], qhead = 0, qtail = 0; for(int i = 1; i <= n; ++i) { dist[i] = MAXN; numbs[i] = 0; } Q[qtail++] = des; dist[des] = 0; numbs[0] = 1; while(qhead != qtail) { int v = Q[qhead++]; for(int i = head[v]; i != -1; i = edge[i].next) { if(edge[edge[i].rev].cap == 0 || dist[edge[i].ver] < MAXN)continue; dist[edge[i].ver] = dist[v] + 1; ++numbs[dist[edge[i].ver]]; Q[qtail++] = edge[i].ver; } } } void init() { e = 0; memset(head, -1, sizeof(head)); } long long maxflow() { int u; long long totalflow = 0; int Curhead[MAXN], revpath[MAXN]; for(int i = 1; i <= n; ++i)Curhead[i] = head[i]; u = src; while(dist[src] < n) { if(u == des) // find an augmenting path { int augflow = INF; for(int i = src; i != des; i = edge[Curhead[i]].ver) augflow = min(augflow, edge[Curhead[i]].cap); for(int i = src; i != des; i = edge[Curhead[i]].ver) { edge[Curhead[i]].cap -= augflow; edge[edge[Curhead[i]].rev].cap += augflow; edge[Curhead[i]].flow += augflow; edge[edge[Curhead[i]].rev].flow -= augflow; } totalflow += augflow; u = src; } int i; for(i = Curhead[u]; i != -1; i = edge[i].next) if(edge[i].cap > 0 && dist[u] == dist[edge[i].ver] + 1)break; if(i != -1) // find an admissible arc, then Advance { Curhead[u] = i; revpath[edge[i].ver] = edge[i].rev; u = edge[i].ver; } else // no admissible arc, then relabel this vertex { if(0 == (--numbs[dist[u]]))break; // GAP cut, Important! Curhead[u] = head[u]; int mindist = n; for(int j = head[u]; j != -1; j = edge[j].next) if(edge[j].cap > 0)mindist = min(mindist, dist[edge[j].ver]); dist[u] = mindist + 1; ++numbs[dist[u]]; if(u != src) u = edge[revpath[u]].ver; // Backtrack } } return totalflow; } int nt, m; int vis[MAXN]; void dfs(int u) { if(u == des) return; vis[u] = 1; for(int i = head[u]; i != -1; i = edge[i].next) if(edge[i].cap > 0 && !vis[edge[i].ver]) dfs(edge[i].ver); } int main() { init(); scanf("%d%d", &nt, &m); src = nt + 1; des = nt + 2; n = des; long long sum = 0; int w, u, v; for(int i = 1; i <= nt; i++) { scanf("%d", &w); if(w > 0) { sum += w; add(src, i, w); } else if(w < 0) add(i, des, -w); } while(m--) { scanf("%d%d", &u, &v); add(u, v, INF); } rev_BFS(); long long flow = maxflow(); memset(vis, 0, sizeof(vis)); dfs(src); int ans = 0; for(int i = 1; i <= nt; i++) if(vis[i]) ans++; printf("%d %I64d\n", ans, sum - flow); return 0; }