最小树形图(朱刘算法)(poj 3164)

题目连接node

                                                                                   ommand Networkgit

Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 20041   Accepted: 5749

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.dom

Inputide

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.oop

Outputui

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’.spa

Sample Input.net

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

 

如下转自该博客:https://blog.csdn.net/shuangde800/article/details/8039359

题目大意:给定一个有向图,根节点已知,求该有向图的最小树形图。最小树形图即有向图的最小生成树,定义为:选择一些边,使得根节点可以到达图中全部的节点,并使得选出的边的边权和最小。

题目算法:朱-刘算法(即由中国人朱永津和刘振宏共同发明的算法)。

算法步骤以下:(本文再也不证实,参考下面给出的我本身画的一个图便可理解)

1.判断图的连通性,若不连通直接无解,不然必定有解。

2.为除了根节点之外的全部点选择一个权值最小的入边,假设用pre数组记录前驱,f数组记录选择的边长,记所选边权和为temp。

3.(可利用并查集)判断选择的的边是否构成环,若没有则直接ans+=temp并输出ans,如有,则进行下一步操做。

4.对该环实施缩点操做,设该环上有点V1,V2……Vi……Vn,缩成的点为node ,对于全部不在环中的点P进行以下更改:

(1) 点P到node的距离为min{a[p,Vi]-f[Vi]} (a为边集数组)

 (2)点node到p的距离为min{a[Vi,p]}

操做(1)的理解:先假设环上全部边均选上,若下次选择某一条边进入该环,则能够断开进入点与进入点的前驱之间的边,即断开F[进入点],因此等效为直接把a[p,node]赋值为min{a[p,Vi]-f[Vi]}。

特别提醒:本题有自环,能够提早删掉,由于它没有用。

 

ac代码

/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1005;
struct point
{
	double x,y;
}p[maxn];
struct node
{
	int u,v;
	double len;
}edge[maxn*maxn];
int pre[maxn],id[maxn],vis[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 dir_mst(int root,int n,int m)
{
	double ans=0;
	while(1)
	{
		//先找出全部点的最小入边
		/*memset(in,inf,sizeof(in)); //这样写会报错!*/
		for(int i=0;i<n;++i)
		{
			in[i]=inf;
		}
		for(int i=0;i<m;++i)
		{
			int u=edge[i].u,v=edge[i].v;
			if(edge[i].len<in[v]&&u!=v)
			{
				pre[v]=u;in[v]=edge[i].len;
			}
		}
		for(int i=0;i<n;++i)
		{
			if(i==root)
			{
				continue;
			}
			if(in[i]==inf)
			{
				return -1;	//若是某点入度为零,一定找不到
			}
		}
		//检查这些边是否构成了环
		memset(id,-1,sizeof(id));
		memset(vis,-1,sizeof(vis));
		in[root]=0;
		int cnt=0;
		for(int i=0;i<n;++i)//标记环
		{
			ans+=in[i];
			int v=i;
			while(vis[v]!=i&&id[v]==-1&&v!=root)
			{
				vis[v]=i;
				v=pre[v];
			}
			if(v!=root&&id[v]==-1)//缩点
			{
				for(int u=pre[v];u!=v;u=pre[u])
				{
					id[u]=cnt;
				}
				id[v]=cnt++;
			}
		}
		if(cnt==0)
		{
			break;//无环
		}
		for(int i=0;i<n;++i)
		{
			if(id[i]==-1)
			{
				id[i]=cnt++;
			}
		}
		//创建新图
		for(int i=0;i<m;++i)
		{
			int u=edge[i].u,v=edge[i].v;
			edge[i].u=id[u];
			edge[i].v=id[v];
			if(id[u]!=id[v])
			{
				edge[i].len-=in[v];
			}
		}
		n=cnt;
		root=id[root];
	}
	return ans;
}
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=0;i<n;++i)
		{
			scanf("%lf%lf",&p[i].x,&p[i].y);
		}
		for(int i=0;i<m;++i)
		{
			scanf("%d%d",&edge[i].u,&edge[i].v);
			--edge[i].u;--edge[i].v;
			if(edge[i].u!=edge[i].v)
			{
				edge[i].len=dis(p[edge[i].u],p[edge[i].v]);
			}
			else
			{
				edge[i].len=inf;
			}
		}
		double ans=dir_mst(0,n,m);
		if(ans==-1)
		{
			printf("poor snoopy\n");
		}
		else
		{
			printf("%.2f\n",ans);
		}
	}
	return 0;
}
相关文章
相关标签/搜索