pku 3164 Command Network(最小树形图)

Command Network
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 10411 Accepted: 3028

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


题意:给出n个点的坐标,和m个这些点的关系,有向
题解:求又点1到其他所有的连通的图的权值最小,即最小树形图

#include<stdio.h>
#include<math.h>
#include<string.h>
#define INF 0x7ffffff
struct point{
    double x,y;
}p[105];
struct edge{
    int x,y;
    double len;
}v[10005];
int pre[105],has[105],mark[105],n,cou;
double in[105];
double distan(struct point a,struct point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void add(int x,int y)
{
    v[cou].x=x;
    v[cou].y=y;
    v[cou++].len=distan(p[x],p[y]);
}
double Z_N(int root)
{
    double sum=0;
    int i,x,y,counode;

    while(1)
    {
        for(i=1;i<=n;i++) in[i]=INF;
        for(i=0;i<cou;i++)    
        {
            x=v[i].x;
            y=v[i].y;
            if(v[i].len<in[y]&&x!=y)   //找出指向各个点最短的边,而重构图后有可能有自环
            {
                pre[y]=x;   //记录该点最短入边的标号
                in[y]=v[i].len;   //记录该点最短入边的长度
            }
        }
        for(i=1;i<=n;i++)
        {
            if(i==root) continue;
            if(in[i]==INF) return -1;  //没有边指向该点,该图不连通
        }
        counode=1;
        memset(has,-1,sizeof(has));
        memset(mark,-1,sizeof(mark));
        in[root]=0;
        for(i=1;i<=n;i++)
        {
            sum+=in[i];  //计算图的权值
            x=i;
            while(mark[x]!=i&&has[x]==-1&&x!=root)   //若某一点的最短入边会循环到自己,证明找到的图有环
            {
                mark[x]=i;
                x=pre[x];
            }
            if(x!=root&&has[x]==-1)  //若这两个条件都满足,证明上面是由于mark【x】==i退出的,即有环
            {
                for(y=pre[x];y!=x;y=pre[y]) has[y]=counode;  //将环内所有点都重新标号为同一个
                has[x]=counode++;
            }
        }
        if(counode==1) break;  //该图无环,算法结束
        for(i=1;i<=n;i++)  //该图有环,将仍未标记的结点即不在环内的结点标号
        {
            if(has[i]==-1)
            has[i]=counode++;
        }
        for(i=0;i<cou;i++)  //将所有边的结点的标号更新
        {
            y=v[i].y;
            v[i].y=has[v[i].y];
            v[i].x=has[v[i].x];
            if(v[i].y!=v[i].x) v[i].len-=in[y];  //若不是环内的点,则其边的长度减去其最小入边的长度
        }
        n=counode-1;
        root=has[root];
    }

    return sum;
}
int main()
{
    double res;
    int m,i,x,y;

    while(scanf("%d%d",&n,&m)>0)
    {
        for(i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
        for(cou=i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            if(x==y) continue;   //消自环
            add(x,y);
        }
        res=Z_N(1);
        if(res==-1)  printf("poor snoopy\n");
        else  printf("%.2f\n",res);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值