【POJ 3164】【朱刘算法模板】Command Network

Command Network
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 13977 Accepted: 4012

Description

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

Source



最小树形图-朱刘算法。


最小树形图其实就是求有向图的“最小生成树”。


lyd的讲解比较清楚。


大致的算法流程是:

1.除了根结点,为每一个结点找到权值最小的入边


2.如果这些权值最小的入边没有构成环,那么这些入边的和就是答案,输出答案然后退出;

否则先把这些入边的和加入到答案中去


3.缩点


4.修改边权:

注意到在第二步中把所有边权和都加入到答案中去了,可是最后有一些边是不需要的,比如最小树形图中肯定没有环,环上的边至少要删掉一个;环与环之间要有边相连;

因此,我们把每条边的边权都减去出点的最小入边的权值(如果加上这条边,相当于删除原来的入边)


5.返回1继续执行


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#define M 10000+5
#define inf 0x3f3f3f3f
using namespace std;
int n,m,pre[M],id[M],v[M];
double in[M];
struct Point
{
	double x,y;
}p[M];
struct Edge
{
	int x,y;
	double v;
}e[M];
void Zhu_Liu(int root)
{
	int idx=n;
	double ans=0.0;
	while (idx)
	{
		for (int i=1;i<=n;i++)
			in[i]=(double)inf,id[i]=-1,v[i]=-1;
		for (int i=1;i<=m;i++)
		{
			if (e[i].v>in[e[i].y]||e[i].x==e[i].y) continue;
			pre[e[i].y]=e[i].x;
			in[e[i].y]=e[i].v;
		}
		in[root]=0,pre[root]=root;
		for (int i=1;i<=n;i++)
		{
			if (in[i]==(double)inf)
			{
				puts("poor snoopy");
				return;
			}
			ans+=in[i];
		}
		idx=0;
		for (int i=1;i<=n;i++)
			if (v[i]==-1)
			{
				int t=i;
				while (v[t]==-1)
					v[t]=i,t=pre[t];
				if (v[t]!=i||t==root) continue;
				id[t]=++idx;
				int s;
				for (s=pre[t];s!=t;s=pre[s])
					id[s]=idx;
			}
		if (!idx) break;
		for (int i=1;i<=n;i++)
			if (id[i]==-1) id[i]=++idx;
		for (int i=1;i<=m;i++)
		{
			e[i].v-=in[e[i].y];
			e[i].x=id[e[i].x];
			e[i].y=id[e[i].y];
		}
		n=idx;
		root=id[root];
	}
	printf("%.2f\n",ans);
}
double Getdis(int a,int b)
{
	return sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y));
}
int main()
{
        while (scanf("%d%d",&n,&m)!=EOF)
	{
		for (int i=1;i<=n;i++)
			scanf("%lf%lf",&p[i].x,&p[i].y);
		for (int i=1;i<=m;i++)
			scanf("%d%d",&e[i].x,&e[i].y),e[i].v=Getdis(e[i].x,e[i].y);
		Zhu_Liu(1);
	}
	return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值