本文主要是介绍CUGB图论专场:D - Command Network(最小树形图:朱刘算法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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
参考芳哥和魏神的博客:http://blog.csdn.net/wsniyufang/article/details/6747392 和 http://blog.csdn.net/sdj222555/article/details/7459738。
思路:最小树形图,就是给有向带权图中指定一个特殊的点root,求一棵以root为根的有向生成树T,并且T中所有边的总权值最小。最小树形图的第一个算法是 1965年朱永津和刘振宏提出的复杂度为O(VE)的算法。
因为刚开始接触有向图的代码,所以看了一下午,理解了一下午才理解……不过收获也挺大的,继续默默努力吧……
自己的理解:刚开始自己并未理解最小树形图的概念,感觉和平常的生成树也没多大差别。不过深入研究以后确实差别挺大的。我们以前做的生成树是无向图,求的树是无环的,而这个最小树形图可以有环,解决的办法就是把环变成结点。但是如果把环变成结点的话,那边的权值就得改变了,不然求的树就不是原先的树了。设这个环中指向u的边权是in[u],那么对于每条从u出发的边(u, i, w),在新图中连接(new, i, w)的边,其中new为新加的人工顶点; 对于每条进入u的边(i, u, w),在新图中建立边(i, new, w-in[u])的边,即用w-in[u]更新边权值。还有一些理解有代码中会有注释!感觉如果把所有的环都变成结点以后,所求的最小树形图其实就是一条线把这些结点联结起来,求的就是这条线的最小值。
用到的思想及算法等:有向图的强连通分量,缩点法。
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define M 102
#define INF 10000000
using namespace std;
typedef long long ll;
struct Point
{double x,y;
}p[M];
struct node
{int u,v; //边的两端结点double value; //边权值
}e[M*M];
int pre[M],newnode[M],visit[M],n,m,i; //前向结点数组,新结点指向数组,访问数组
double in[M]; //边权值数组
double dist(Point a,Point b) //坐标两点之间的距离
{return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double zhuliu(int root,int n1,int m1)
{double ans=0; //因为把环删除后其边权值是当前和与原先和之和,所以得在循环外定义int u,v;while(1){//(1).找最小边放入集合for(i=0;i<n1;i++)in[i]=INF;for(i=0;i<m1;i++){u=e[i].u; v=e[i].v;if(e[i].value<in[v]&&u!=v){pre[v]=u; //每次都找最小边进入集合,以达最优解in[v]=e[i].value;}}for(i=0;i<n1;i++)if(in[i]==INF&&i!=root) return -1; //除了根以外还有别的结点没有入边,则说明没有连通,无最小树形图//(2).找环,若有环,则生成新结点,强连通分量,即环int New=0; //新结点还是从0开始mem(newnode,-1);mem(visit,-1);in[root]=0;for(i=0;i<n1;i++) //寻找每个结点是否有环{ans+=in[i]; v=i;while(visit[v]!=i&&newnode[v]==-1&&v!=root) //每个点寻找其前向点,要么最终寻找至根部,要么找到一个环{visit[v]=i;v=pre[v];}if(v!=root&&newnode[v]==-1) //把环缩成一个点,缩点法{for(u=pre[v];u!=v;u=pre[u])newnode[u]=New;newnode[v]=New++;}}if(New==0) break; //如果New无变化,当然就无环啦for(i=0;i<n1;i++)if(newnode[i]==-1) newnode[i]=New++;//(3).建立新图,即把环缩成点,然后指向和边权值也随之改变for(i=0;i<m1;i++){u=e[i].u; v=e[i].v;e[i].u=newnode[u];e[i].v=newnode[v];if(newnode[u]!=newnode[v]) e[i].value-=in[v]; //两个不同的结点之间的边权值改变}n1=New;root=newnode[root]; //结点更新之后,根结点可能有环而改变,所以根结点也要更新}return ans;
}
int main()
{double ans;while(~scanf("%d%d",&n,&m)){for(i=0;i<n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);for(i=0;i<m;i++){scanf("%d%d", &e[i].u, &e[i].v);e[i].u--; e[i].v--; //结点从0开始if(e[i].u != e[i].v)e[i].value=dist(p[e[i].u],p[e[i].v]);else e[i].value=INF; //结点相同就是自环,则要去除自环}ans=zhuliu(0,n,m);if(ans==-1) printf("poor snoopy\n");else printf("%.2f\n", ans);}return 0;
}
这篇关于CUGB图论专场:D - Command Network(最小树形图:朱刘算法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!