Poj 3164 Command Network【最小树形图】

Command Network

Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 15914   Accepted: 4583

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 Mlines 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

POJ Monthly--2006.12.31, galaxy

题意:

给出一个有向图,给出n个顶点的坐标,以及每条边连接的顶点,指定一个起点,找到一个方案,使得从这个点到其他所有点的路径的权值和最小,求最小权值

题解:

传说中的最小树形图,相当于无向图的最小生成树,但是考虑的东西多了很多,毕竟有向图的连通性判断都比较复杂了....

不是太会这个算法,大概看明白了点,正在理解中,大神博客点这里...

网上找不到太多相关的内容,想试试模板都难....

/*
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;
}
时间: 2024-10-15 17:57:27

Poj 3164 Command Network【最小树形图】的相关文章

POJ 3164 Command Network 最小树形图-朱刘算法裸题

题目来源:POJ 3164 Command Network 题意:求以1为根的最小树形图 没有输出字符串 思路:直接高朱刘算法 不懂的可以百度 学会了就是直接套模板的事情 其实就是不断消圈而已 不构成圈就有解 无法从根到达其他点就无解 #include <cstdio> #include <cstring> #include <cmath> const int maxn = 110; const int maxm = 50010; const double INF =

POJ 3164 Command Network(最小树形图)

Command Network Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 13817   Accepted: 3973 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 a

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

题目地址:POJ 3164 最小树形图第一发. 把一个v写成u了.....TLE了一晚上...(虽说今晚出去玩了..) 刚开始看这个算法的时看模板以为又是一个isap....吓得一个哆嗦.但是仔细看了看之后发现还是挺好理解的.写下自己的理解. 朱刘算法其实只有3步,然后不断循环. 1:找到每个点的最小入边.既然是生成树,那么对于每个点来说,只要选一个权值最小的入边就可以了.贪心思想.因为如果不是最小入边,那么它肯定不是最小树形图的一条边,考虑它是没有意义的. 2:找环.找环找的是最小入边构成的新

最小树形图 【poj 3164 Command Network】

定义:在有向图上的最小生成树. 算法过程:和最小生成树一样,不过这个不是无向图的,但是也可以用类似的算法,最小树形图的第一个算法数朱刘算法,依据最小生成树数算法形成的. 我们知道,在最小生成树算法中,我们每次选长度最短的边,如果满足条件则加入最小生成树中,知道所有的点都在树中,最小树形图同样. 首先和最小生成树一样,首先必须保证图联通,否则不能形成最小树形图. 但是由于是有向的,而我们只要找所有点的入边中最小的入边的和就是这个图的最小树形图,但是最关键的地方在于可能形成环,我们要做的就是缩点,把

poj Command Network 最小树形图

规定根节点,求一颗生成树使得权值最小,但由于是有向图,所以最小生成树算法失效. 查资料后得知此类问题叫做最小树形图. 解决最小树形图问题的朱刘算法,算法核心基于找 [最小弧集->找环,消环缩点] 的思想,来慢慢构造树形图. 所有的灵魂都在这张图上.0.0 注意缩点后的弧权值的处理 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algo

poj3134 Command Network --- 最小树形图

新单词unidirectional get T T 求有向图上,以某点为根的,最小生成树 参考别人的模板 #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<iostream> #include<algorithm> #include<cmath> #define inf 2000000000 using namespac

poj 3164 Command Network(最小树形图模板)

Command Network http://poj.org/problem?id=3164 Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 18769   Accepted: 5392 Description After a long lasting war on words, a war on arms finally breaks out between littleken’s and KnuthOcean’s k

POJ 3164 Command Network(最小树形图模板题+详解)

http://poj.org/problem?id=3164 题意: 求最小树形图. 思路: 套模板. 引用一下来自大神博客的讲解:http://www.cnblogs.com/acjiumeng/p/7136604.html 算法步骤如下: 1.判断图的连通性,若不连通直接无解,否则一定有解. 2.为除了根节点以外的所有点选择一个权值最小的入边,假设用pre数组记录前驱,f数组记录选择的边长,记所选边权和为temp. 3.(可利用并查集)判断选择的的边是否构成环,若没有则直接$ans+=tem

poj 3164 Command Network 【最小树形图】【朱刘算法 入门】

Command Network Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 14782   Accepted: 4249 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 a