题目大意:一个双核CPU,假设分为A核和B核,这两个核处理一个任务所需要的花费不同,非别为Ai和Bi
现在有M个冲突,冲突表示如果两个任务在不同核执行完成的话,需要额外Wi的花费
问需要最少的花费是多少,才能使得所有任务都完成
解题思路:最小割解决矛盾的问题,我们要解决的矛盾是
s->u…->v->t(表示冲突的边),需要将这些线路断开,且花费的代价最小,这就是最小割了
建图的话,点i连向源点,容量为Ai
点i连向汇点,容量为Bi
冲突的点连双向边,容量为Wi
ISAP代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXNODE = 20010;
const int MAXEDGE = 600000;
typedef int Type;
const Type INF = 0x3f3f3f3f;
struct Edge {
int u, v, next;
Type cap, flow;
Edge() {}
Edge(int u, int v, Type cap, Type flow, int next): u(u), v(v), cap(cap), flow(flow), next(next) {}
};
struct ISAP {
int n, m, s, t;
Edge edges[MAXEDGE];
int head[MAXNODE], p[MAXNODE], num[MAXNODE], cur[MAXNODE], d[MAXNODE];
bool vis[MAXNODE];
void init(int n) {
this->n = n;
memset(head, -1, sizeof(head));
m = 0;
}
void AddEdge(int u, int v, Type cap, Type cap2) {
edges[m] = Edge(u, v, cap, 0, head[u]);
head[u] = m++;
edges[m] = Edge(v, u, cap2, 0, head[v]);
head[v] = m++;
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
for (int i = 0; i < n; i++)
d[i] = INF;
d[t] = 0;
vis[t] = 1;
Q.push(t);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = head[u]; ~i; i = edges[i].next) {
Edge &e = edges[i ^ 1];
if (!vis[e.u] && e.cap > e.flow) {
vis[e.u] = true;
d[e.u] = d[u] + 1;
Q.push(e.u);
}
}
}
return vis[s];
}
Type Augment() {
int u = t;
Type flow = INF;
while (u != s) {
Edge &e = edges[p[u]];
flow = min(flow, e.cap - e.flow);
u = edges[p[u]].u;
}
u = t;
while (u != s) {
edges[p[u]].flow += flow;
edges[p[u] ^ 1].flow -= flow;
u = edges[p[u]].u;
}
return flow;
}
Type Maxflow(int s, int t) {
this->s = s; this->t = t;
Type flow = 0;
BFS();
//如果s-->t走不通
if (d[s] >= n)
return 0;
memset(num, 0, sizeof(num));
for (int i = 0; i < n; i++)
cur[i] = head[i];
for (int i = 0; i < n; i++)
if (d[i] < INF)
num[d[i]]++;
int u = s;
while (d[s] < n) {
if (u == t) {
flow += Augment();
u = s;
}
bool ok = false;//纪录是否找到了下一个点
for (int i = cur[u]; ~i; i = edges[i].next) {
Edge &e = edges[i];
if (e.cap > e.flow && d[u] == d[e.v] + 1) {
ok = true;
p[e.v] = i;//点v由第i条边增广得到
cur[u] = i;//尝试到第i条边
u = e.v;
break;
}
}
//如果没找到下一个点,表示u到t的最短路要变长了,或者没路可走了
if (!ok) {
//找寻u到下一个点的最短路
int Min = n - 1;
for (int i = head[u]; ~i; i = edges[i].next) {
Edge &e = edges[i];
if (e.cap > e.flow)
Min = min(Min, d[e.v]);
}
if (--num[d[u]] == 0)//GAP优化
break;
num[d[u] = Min + 1]++;
cur[u] = head[u];
//返回前一个点,因为该点的最短距离已经变了
if (u != s)
u = edges[p[u]].u;
}
}
return flow;
}
}isap;
int n, m;
void solve() {
int source = 0, sink = n + 1;
isap.init(sink + 1);
int A, B;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &A, &B);
isap.AddEdge(source, i, A, 0);
isap.AddEdge(i, sink, B, 0);
}
int u, v, c;
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &u, &v, &c);
isap.AddEdge(u, v, c, c);
}
printf("%d\n", isap.Maxflow(source, sink));
}
int main() {
while (scanf("%d%d", &n, &m) != EOF) solve();
return 0;
}
Dinic代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXNODE = 20010;
const int MAXEDGE = 500000;
typedef int Type;
const Type INF = 0x3f3f3f3f;
struct Edge{
int u, v, next;
Type cap, flow;
Edge() {}
Edge(int u, int v, Type cap, Type flow, int next) : u(u), v(v), cap(cap), flow(flow), next(next){}
};
struct Dinic{
int n, m, s, t;
Edge edges[MAXEDGE];
int head[MAXNODE];
bool vis[MAXNODE];
Type d[MAXNODE];
vector<int> cut;
void init(int n) {
this->n = n;
memset(head, -1, sizeof(head));
m = 0;
}
void AddEdge(int u, int v, Type cap, Type cap2) {
edges[m] = Edge(u, v, cap, 0, head[u]);
head[u] = m++;
edges[m] = Edge(v, u, cap2, 0, head[v]);
head[v] = m++;
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = head[u]; ~i; i = edges[i].next) {
Edge &e = edges[i];
if (!vis[e.v] && e.cap > e.flow) {
vis[e.v] = true;
d[e.v] = d[u] + 1;
Q.push(e.v);
}
}
}
return vis[t];
}
Type DFS(int u, Type a) {
if (u == t || a == 0) return a;
Type flow = 0, f;
for (int i = head[u]; ~i; i = edges[i].next) {
Edge &e = edges[i];
if (d[u] + 1 == d[e.v] && (f = DFS(e.v, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[i ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
Type Maxflow(int s, int t) {
this->s = s; this->t = t;
Type flow = 0;
while (BFS()) flow += DFS(s, INF);
return flow;
}
void Mincut() {
cut.clear();
for (int i = 0; i < m; i += 2) {
if (vis[edges[i].u] && !vis[edges[i].v])
cut.push_back(i);
}
}
}dinic;
int n, m;
void solve() {
int source = 0, sink = n + 1;
dinic.init(n + 1);
int A, B;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &A, &B);
dinic.AddEdge(source, i, A, 0);
dinic.AddEdge(i, sink, B, 0);
}
int u, v, c;
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &u, &v, &c);
dinic.AddEdge(u, v, c, c);
}
printf("%d\n", dinic.Maxflow(source, sink));
}
int main() {
while (scanf("%d%d", &n, &m) != EOF) solve();
return 0;
}