Source: POJ 3164 Command Network
The minimum tree structure with 1 root does not have an output string.
Train of Thought: the direct high zhu liu algorithm can be learned by Baidu, that is, the direct set of templates is actually the constant elimination of the circle. If it does not constitute a circle, there will be a solution and no solution can be obtained from the root to other points.
#include
#include
#include
const int maxn = 110;const int maxm = 50010;const double INF = 999999999;struct edge{int u, v;double w;edge(){}edge(int u, int v, double w) : u(u), v(v), w(w){}}e[maxm];struct Point{double x, y;}p[maxn];int pre[maxn], vis[maxn], no[maxn];double in[maxn];double dis(Point a, Point b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } double MST(int n, int m, int rt){double ans = 0;while(1){for(int i = 1; i <= n; i++)in[i] = INF;for(int i = 1; i <= m; i++){int u = e[i].u;int v = e[i].v;if(u != v && in[v] > e[i].w){pre[v] = u;in[v] = e[i].w;}}for(int i = 1; i <= n; i++){if(i == rt)continue;if(in[i] == INF)return -1;}memset(no, -1, sizeof(no));memset(vis, -1, sizeof(vis));in[rt] = 0;int cnt = 0;for(int i = 1; i <= n; i++){ans += in[i];int v = i;while(v != rt && vis[v] != i && no[v] == -1){vis[v] = i;v = pre[v];}if(v != rt && no[v] == -1){for(int u = pre[v]; u != v; u = pre[u])no[u] = cnt+1;no[v] = cnt+1;cnt++;}}if(!cnt)return ans;for(int i = 1; i <= n; i++)if(no[i] == -1)no[i] = ++cnt;for(int i = 1; i <= m; i++){int u = e[i].u;int v = e[i].v;e[i].u = no[u];e[i].v = no[v];if(no[u] != no[v]){e[i].w -= in[v];}}n = cnt;rt = no[rt];}return ans;}int main(){int n, m;while(scanf("%d %d", &n, &m) != EOF){for(int i = 1 ; i <= n; i++)scanf("%lf %lf", &p[i].x, &p[i].y);int add = 1;for(int i = 1; i <= m; i++){scanf("%d %d", &e[add].u, &e[add].v);if(e[add].u == e[add].v)continue;e[add].w = dis(p[e[add].u], p[e[add].v]);add++;}double ans = MST(n, add-1, 1);if(ans < 0)puts("poor snoopy");elseprintf("%.2f\n", ans);}return 0;}