您的位置:首页 > 其它

poj 3164 最小树形图模板题目,朱刘算法

2014-01-23 18:04 501 查看
Command Network

Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 11621 Accepted: 3369
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
这个题目是最小树形图的问题,有向网求一个生成树,那么朱刘算法究竟是啥意思呢?举个简单的例子,1—2的权值为3,,2—1为4,3—1为9, 4[b]—2为7,找出环,1与[/b]
[b]2可形成一个环,我们求出3到1时就不再是9了,而是9-4=5,因为2到1之间的4被省略了,所以相当于路径减小了,所以4-2也同样是7-3=4,然后化成[/b]
[b]下面是朱刘算法的构造图



[/b]
一个无圈有收缩点的图,然后再进行展开,可以求出一个树了

#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#define N 102
#define M 999999999
using namespace std;
struct node
{
double x,y;
}unit[N*N];
double map

;
int pre
;
bool flag
,visit
;
int n,m;
double sum;
double dis(double &x1,double &y1,double &x2,double &y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

void zhuliu()
{
int i,j,k;
for(i=1;i<=n;i++)
map[i][i]=M,flag[i]=false;//开始没考虑自环,就没有map[i][i]=M,然后WA了N久
sum=0;
pre[1]=1;
while(true)
{
//求最短弧集合S
for(i=2;i<=n;i++)
{
if(flag[i]) continue;
pre[i]=i;
for(j=1;j<=n;j++)
if(!flag[j]&&map[j][i]<map[pre[i]][i])
pre[i]=j;
if(pre[i]==i) //说明不连通
{sum=-1.0;return;}
}
//检查S
for(i=2;i<=n;i++)
{
if(flag[i]) continue;
memset(visit,0,sizeof(visit));
visit[1]=true;
j=i;
do
{
visit[j]=true;
j=pre[j];
}while(!visit[j]);

if(j==1) continue;//无环

//有环
i=j;
do
{
sum+=map[pre[j]][j];//记录环的权值
j=pre[j];
}while(j!=i);
j=i;
//对与环上的点有关的边,修改边权
do
{
for(k=1;k<=n;k++)
{
if(flag[k]) continue;
if(map[k][j]<M&&k!=pre[j])
map[k][j]-=map[pre[j]][j];
}
j=pre[j];
}while(j!=i);

//将环集中在i点
for(j=1;j<=n;j++)
{
if(j==i)continue;
for(k=pre[i];k!=i;k=pre[k])
{
if(map[k][j]<map[i][j]) map[i][j]=map[k][j];
if(map[j][k]<map[j][i]) map[j][i]=map[j][k];
}
}
for(j=pre[i];j!=i;j=pre[j]) flag[j]=true;
break;
}
if(i==n+1)
{
for(i=2;i<=n;i++) if(!flag[i]) sum+=map[pre[i]][i];
break;
}
}
return;
}

int main()
{

int i,j,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
map[i][j]=M;
}
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&unit[i].x,&unit[i].y);
}
for(i=0;i<m;i++)
{
scanf("%d%d",&j,&k);
map[j][k]=dis(unit[j].x,unit[j].y,unit[k].x,unit[k].y);
}
zhuliu();
if(sum<0.0)
printf("poor snoopy\n");
else
printf("%.2lf\n",sum);

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息