【最小树形图 朱刘算法 O(VE)】 Command Network POJ - 3164

After a long lasting war on words, a war on arms finally breaks out between littleken’s and KnuthOcean’s kingdoms. A sudden and violent assault by KnuthOcean’s force has rendered a total failure of littleken’s command network. A provisional network must be built immediately. littleken orders snoopy to take charge of the project.

With the situation studied to every detail, snoopy believes that the most urgent point is to enable littenken’s commands to reach every disconnected node in the destroyed network and decides on a plan to build a unidirectional communication network. The nodes are distributed on a plane. If littleken’s commands are to be able to be delivered directly from a node A to another node B, a wire will have to be built along the straight line segment connecting the two nodes. Since it’s in wartime, not between all pairs of nodes can wires be built. snoopy wants the plan to require the shortest total length of wires so that the construction can be done very soon.

Input

The input contains several test cases. Each test case starts with a line containing two integer N (N ≤ 100), the number of nodes in the destroyed network, and M (M ≤ 104), the number of pairs of nodes between which a wire can be built. The next N lines each contain an ordered pair xi and yi, giving the Cartesian coordinates of the nodes. Then follow M lines each containing two integers i and j between 1 and N (inclusive) meaning a wire can be built between node i and node j for unidirectional command delivery from the former to the latter. littleken’s headquarter is always located at node 1. Process to end of file.

Output

For each test case, output exactly one line containing the shortest total length of wires to two digits past the decimal point. In the cases that such a network does not exist, just output ‘poor snoopy’.


Sample Input
4 6
0 6
4 6
0 0
7 20
1 2
1 3
2 3
3 4
3 1
3 2
4 3
0 0
1 0
0 1
1 2
1 3
4 1
2 3
Sample Output
31.19
poor snoopy


// 朱刘算法 O(VE)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

const int mn = 105;
const int mm = 10010;
const double inf = 0x3f3f3f3f;

int n, m;
int x[mn], y[mn];
double e[mn][mn];

int pre[mn];
bool flag[mn];
bool vis[mn];

double zhuliu(int r)
{
	double ans = 0;
	memset(vis, 0, sizeof vis);
	memset(flag, 0, sizeof flag);
	while(1)
	{
		for (int i = 1; i <= n; i++) //求最短弧的集合
		{
			if (i == r || flag[i])
				continue;

			e[i][i] = inf;pre[i] = i;
			for (int j = 1; j <= n; j++)
				if(!flag[j] && e[j][i] < e[pre[i]][i])
					pre[i]  = j;

			if (pre[i] == i)
				return -1;
		}

		int i;
		for (i = 1; i <= n; i++) // 检查是否有环
		{
			if (i == r || flag[i])
				continue;
            int j = i, cnt = 0;
            while (j != r && pre[j] != i && cnt <= n)
				j = pre[j], cnt++;
			if (j == r || cnt > n)
				continue;
			break; // 找到环 缩点
		}

		if (i == n + 1)  // 无环 即为所求
		{
			for (int j = 1; j <= n; j++)
				if (j != r && !flag[j])
				ans += e[pre[j]][j];
			return ans;
		}

		int j = i;  // 缩点
		memset(vis, 0, sizeof vis);
		do
		{
			ans += e[pre[j]][j];
			j = pre[j];
			vis[j] = 1;
			flag[j] = 1;
		} while (j != i);
		flag[i] = 0; // i 为缩点

		for (int p = 1; p <= n; p++) // 改变权值
		{
			if (!vis[p])  // p 在环内
				continue;
			for (int q = 1; q <= n; q ++)
			{
				if(vis[q])  // q 在环外
					continue;
				if (e[i][q] > e[p][q])
					e[i][q] = e[p][q];
				if(e[q][p] < inf && (e[q][p] - e[pre[p]][p]) < e[q][i])
				e[q][i] = e[q][p] - e[pre[p]][p];
			}
		}
	}
}

int main()
{
	while(~scanf("%d %d", &n, &m))
	{
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
			e[i][j] = inf;

		for (int i = 1; i <= n; i++)
			scanf("%d %d", &x[i], &y[i]);
		for (int i = 1; i <= m; i++)
		{
			int a, b;
			scanf("%d %d", &a, &b);
			e[a][b] = sqrt((x[a] - x[b]) *(x[a] - x[b]) + (y[a] - y[b]) * (y[a] - y[b]));
		}

		double ans = zhuliu(1);
		if (ans == -1)
			printf("poor snoopy\n");
		else
			printf("%.2f\n", ans);
	}
}

猜你喜欢

转载自blog.csdn.net/ummmmm/article/details/80827256