本文共 4524 字,大约阅读时间需要 15 分钟。
和luogu的分配问题一样,建边也非常简单,基本和[分配问题]一模一样;
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include //#pragma GCC optimize(2)using namespace std;#define maxn 20005#define inf 0x7fffffff//#define INF 1e18#define rdint(x) scanf("%d",&x)#define rdllt(x) scanf("%lld",&x)#define rdult(x) scanf("%lu",&x)#define rdlf(x) scanf("%lf",&x)#define rdstr(x) scanf("%s",x)#define mclr(x,a) memset((x),a,sizeof(x))typedef long long ll;typedef unsigned long long ull;typedef unsigned int U;#define ms(x) memset((x),0,sizeof(x))const long long int mod = 1e9 + 7;#define Mod 1000000000#define sq(x) (x)*(x)#define eps 1e-5typedef pair pii;#define pi acos(-1.0)//const int N = 1005;#define REP(i,n) for(int i=0;i<(n);i++)typedef pair pii;inline int rd() { int x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x;}ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b);}int sqr(int x) { return x * x; }/*ll ans;ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans;}*/bool vis[maxn];int n, m, s, t;int x, y, f, z;int dis[maxn], pre[maxn], last[maxn], flow[maxn];int maxflow, mincost;struct node { int to, nxt, flow, dis;}edge[maxn << 2];int head[maxn], cnt;void addedge(int from, int to, int flow, int dis) { edge[++cnt].to = to; edge[cnt].flow = flow; edge[cnt].dis = dis; edge[cnt].nxt = head[from]; head[from] = cnt;}bool spfa1(int s, int t) { memset(dis, 0x7f, sizeof(dis)); memset(flow, 0x7f, sizeof(flow)); ms(vis);queue q; q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = 0; for (int i = head[now]; i != -1; i = edge[i].nxt) { if (edge[i].flow > 0 && dis[edge[i].to] > dis[now] + edge[i].dis) { dis[edge[i].to] = edge[i].dis + dis[now]; pre[edge[i].to] = now; last[edge[i].to] = i; flow[edge[i].to] = min(flow[now], edge[i].flow); if (!vis[edge[i].to]) { vis[edge[i].to] = 1; q.push(edge[i].to); } } } } return pre[t] != -1;}bool spfa2(int s, int t) { memset(dis, 0x7f, sizeof(dis)); memset(flow, 0x7f, sizeof(flow)); ms(vis); queue q; q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = 0; for (int i = head[now]; i != -1; i = edge[i].nxt) { if (edge[i].flow > 0 && dis[edge[i].to] > dis[now] + edge[i].dis) { dis[edge[i].to] = edge[i].dis + dis[now]; pre[edge[i].to] = now; last[edge[i].to] = i; flow[edge[i].to] = min(flow[now], edge[i].flow); if (!vis[edge[i].to]) { vis[edge[i].to] = 1; q.push(edge[i].to); } } } } return pre[t] != -1;}void mincost_maxflow() { while (spfa1(s, t)) { int now = t; maxflow += flow[t]; mincost += flow[t] * dis[t]; while (now != s) { edge[last[now]].flow -= flow[t]; edge[last[now] ^ 1].flow += flow[t]; now = pre[now]; } }}void maxcost_maxflow() { while (spfa2(s, t)) { int now = t; maxflow += flow[t]; mincost += flow[t] * dis[t]; while (now != s) { edge[last[now]].flow -= flow[t]; edge[last[now] ^ 1].flow += flow[t]; now = pre[now]; } }}int A[maxn], B[maxn];int C[102][102];int main(){ // ios::sync_with_stdio(0); mclr(head, -1); cnt = 1; m = rd(); n = rd(); for (int i = 1; i <= m; i++)A[i] = rd(); for (int i = 1; i <= n; i++)B[i] = rd(); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++)C[i][j] = rd(); } int ans1 = 0, ans2 = 0; s = 0; t = n + m + 2; for (int i = 1; i <= m; i++) { addedge(s, i, A[i], 0); addedge(i, s, 0, 0); } for (int i = 1; i <= n; i++) { addedge(i + m, t, B[i], 0); addedge(t, i + m, 0, 0); } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { addedge(i, j + m, A[i], C[i][j]); addedge(j + m, i, 0, -C[i][j]); } } mincost_maxflow(); ans1 = mincost; mclr(head, -1); cnt = 1; ms(edge); ms(pre); ms(last); mincost = maxflow = 0; for (int i = 1; i <= m; i++) { addedge(s, i, A[i], 0); addedge(i, s, 0, 0); } for (int i = 1; i <= n; i++) { addedge(i + m, t, B[i], 0); addedge(t, i + m, 0, 0); } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { addedge(i, j + m, A[i], -C[i][j]); addedge(j + m, i, 0, C[i][j]); } } maxcost_maxflow(); ans2 = -mincost; cout << ans1 << endl; cout << ans2 << endl; return 0;}
转载于:https://www.cnblogs.com/zxyqzy/p/10384420.html