复杂度 n^3
在一个有向图中寻找最小生成树,即加入所选边使得源点可以到达任一点
如果没有源点,可以考虑认为加入一个源点,视题目而定
const int maxn = 120;
const double inf = 1000000000;
struct Edge{
int u, v;
double w;
void Get( int a, int b, double c)
{
u = a; v = b; w = c;
}
}edge[maxn*maxn];
double dist[maxn];
int n, r, pre[maxn], belong[maxn], vis[maxn];
double edmonds( int src )
{
double ret = 0;
int u, v, i, scnt;
while( true )
{
for(i = 0; i < n; i++)dist[i] = inf;
for(i = 0; i < r; i++)
{
u = edge[i].u;
v = edge[i].v;
if(edge[i].w < dist[v] && u != v)
{
pre[v] = u;
dist[v] = edge[i].w;
}
}
for(i = 0; i < n; i++)
{
if(i == src)continue;
if(dist[i] == inf)return -1;
}
scnt = 0;
memset(belong, -1, sizeof(belong));
memset(vis, -1, sizeof(vis));
dist[src] = 0;
for(i = 0; i < n; i++)
{
ret += dist[v = i];
while(vis[v] != i && belong[v] == -1 && v != src)
{
vis[v] = i;
v = pre[v];
}
if(v != src && belong[v] == -1)
{
for(u = pre[v]; u != v; u = pre[u])
belong[u] = scnt;
belong[v] = scnt++;
}
}
if(scnt == 0)break;
for(i = 0; i < n; i++)if(belong[i] == -1)
belong[i] = scnt++;
for(i = 0; i < r; i++)
{
v = edge[i].v;
edge[i].u = belong[edge[i].u];
edge[i].v = belong[edge[i].v];
if(edge[i].u != edge[i].v)
edge[i].w -= dist[v]; // *
}
n = scnt;
src = belong[src];
}
return ret;
}