题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853
题意:给出一张有向带权图,找出图中所有的环,对于每个结点只能属于某一个环,问是否所有点都在这些环中,如果在输出这些有向环的最小权值之和
思路:还是拆点思想,每个点都需要匹配其他点及被其他点所匹配,故在二分图中需要形成完备匹配,如果哪个点最后没被匹配到,那就证明不存在这样的环,可以用KM算法或者最小费用流求解
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <functional>
#include <deque>
#include <ctime>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int MAXN = 1000;
const int MAXM = 100000;
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;
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;
}
int main()
{
int n, m;
while (~scanf("%d%d", &n, &m))
{
int s = 0, t = n + n + 1;
init(t + 1);
for (int i = 1; i <= n; i++)
{
addedge(s, i, 1, 0);
addedge(i + n, t, 1, 0);
}
while (m--)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addedge(u, v + n, 1, w);
}
int ans;
if (minCostMaxflow(s, t, ans) != n)
puts("-1");
else
cout << ans << endl;
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <functional>
#include <deque>
#include <ctime>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N = 310;
const int INF = 0x3f3f3f3f;
int nx, ny;
int g[N][N];
int linker[N], lx[N], ly[N];
int slack[N];
bool visx[N], visy[N];
bool DFS(int x)
{
visx[x] = true;
for (int y = 0; y < ny; y++)
{
if (visy[y])continue;
int tmp = lx[x] + ly[y] - g[x][y];
if (tmp == 0)
{
visy[y] = true;
if (linker[y] == -1 || DFS(linker[y]))
{
linker[y] = x;
return true;
}
}
else if (slack[y] > tmp)
slack[y] = tmp;
}
return false;
}
int res;
bool KM()
{
memset(linker, -1, sizeof(linker));
memset(ly, 0, sizeof(ly));
for (int i = 0; i < nx; i++)
{
lx[i] = -INF;
for (int j = 0; j < ny; j++)
if (g[i][j] > lx[i])
lx[i] = g[i][j];
}
for (int x = 0; x < nx; x++)
{
for (int i = 0; i < ny; i++)
slack[i] = INF;
while (true)
{
memset(visx, false, sizeof(visx));
memset(visy, false, sizeof(visy));
if (DFS(x)) break;
int d = INF;
for (int i = 0; i < ny; i++)
if (!visy[i] && d > slack[i])
d = slack[i];
for (int i = 0; i < nx; i++)
if (visx[i])
lx[i] -= d;
for (int i = 0; i < ny; i++)
{
if (visy[i])ly[i] += d;
else slack[i] -= d;
}
}
}
res = 0;
for (int i = 0; i < ny; i++)
if (linker[i] != -1 && g[linker[i]][i] != -INF)
res += g[linker[i]][i];
else
return false;
return true;
}
int main()
{
int n, m;
while (~scanf("%d%d", &n, &m))
{
nx = ny = n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
g[i - 1][j - 1] = -INF;
while (m--)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
u--, v--;
if (g[u][v] < -w)
g[u][v] = -w;
}
if (!KM())
puts("-1");
else
cout << -res << endl;
}
return 0;
}