码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-03-13 18:57:25      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:poj   朱刘算法   

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;
}
技术分享



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

标签:poj   朱刘算法   

原文地址:http://blog.csdn.net/regina8023/article/details/44242267

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!