poj 3164(最小树形图)

Command Network
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 6571 Accepted: 1918

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, andM (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 iand 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

分析:算法讲解具体看 这里这里
这题令我很纠结阿,看完算法讲解,自己写了个算法,步骤一致,不过具体操作不同,我的算法应该是O(ElogE+V*V*C),C是小常数,然后居然1A,我都不知道对不对,囧,速度居然比O(VE)的来得慢。。。到底什么情况,还有,同样的算法hdu4009wa了,别人的接近超时或直接TLE了,这个模板有待检测,有兴趣的可以帮忙测试。。。拿去用的错了尽管鄙视吧。。。 尴尬
代码:
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int mm=22222;
const int mn=222;
struct edge
{
    int s,t;
    double w;
}g[mm];
double x[mn],y[mn],in[mn],w[mn],ans,sum;
int head[mm],next[mm];
int p[mn],q[mn],mark[mn],fp[mn],from[mn],vis[mn];
int i,j,k,n,m,e,r;
bool huan;
inline void addedge(int u,int v,double c)
{
    g[e].s=u,g[e].t=v,g[e].w=c,next[e]=head[u],head[u]=e++;
}
inline bool cmp(edge a,edge b)
{
    return a.t<b.t||(a.t==b.t&&a.w<b.w);
}
inline double len(int i,int j)
{
    return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
void dfs(int u)
{
    ++sum,vis[u]=1;
    for(int i=head[u];i>=0;i=next[i])
        if(!vis[g[i].t])dfs(g[i].t);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=-1)
    {
        for(i=1;i<=n;++i)scanf("%lf%lf",&x[i],&y[i]),head[i]=-1;
        for(e=k=0;k<m;++k)
        {
            scanf("%d%d",&i,&j);
            if(i!=j)addedge(i,j,len(i,j));
        }
        for(sum=i=0;i<=n;++i)vis[i]=0;
        dfs(1);
        if(sum<n)
        {
            printf("poor snoopy\n");
            continue;
        }
        sort(g,g+e,cmp);
        for(i=0;i<=n;++i)fp[i]=p[i]=-1,in[i]=vis[i]=0,mark[i]=i;
        for(i=0;i<e;++i)
            if(p[g[i].t]<0)p[g[i].t]=i;
        huan=1,ans=sum=0;
        while(huan)
        {
            huan=0;
            for(i=2;i<=n;++i)
                if(fp[j=mark[i]]>=0)
                {
                    if(fp[i]<0)in[i]+=w[j],mark[i]=mark[mark[i]];
                    else
                    {
                        in[i]+=w[i],ans+=w[i];
                        if(g[++p[fp[i]]].t!=fp[i])p[fp[i]]=-1;
                    }
                }
            for(i=0;i<=n;++i)fp[i]=-1,vis[i]=0;
            for(i=2;i<=n;++i)
                if(p[i]>=0)
                {
                    if(fp[j=mark[i]]<0||(fp[j]>=0&&w[j]>g[p[i]].w-in[i]))
                       w[j]=g[p[i]].w-in[i],fp[j]=i,from[j]=mark[g[p[i]].s];
                }
            for(sum=0,i=2;i<=n;++i)
                if(fp[i]>=0)sum+=w[i];
            for(i=2;i<=n;++i)
                if(!vis[i])
                {
                    r=0,j=i;
                    while(j>0&&vis[j]>=0)
                    {
                        if(vis[j]>0)
                        {
                            huan=1;
                            while(q[--r]!=j)mark[q[r]]=j,vis[q[r]]=-1;
                            vis[j]=-1;
                        }
                        else if(!vis[j])vis[q[r++]=j]=1;
                        if(fp[j]>=0)j=from[j];
                        else j=-1;
                    }
                    while(r--)vis[q[r]]=fp[q[r]]=-1;
                }
        }
        printf("%.2lf\n",ans+sum);
    }
    return 0;
}
普通版本:
代码:
#include<cstdio>
#include<cmath>
#define type double
using namespace std;
const int mm=11111;
const int mn=111;
const double oo=1e30;
type c[mm],x[mn],y[mn],in[mn],ans;
int s[mm],t[mm],id[mn],pre[mn],q[mn];
inline double len(int i,int j)
{
    return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
type Directed_MST(int root,int NV,int NE)
{
    type ret=0;
    int i,j,u,v,cnt,r;
    while(1)
    {
        for(i=0;i<=NV;++i)in[i]=oo;
        for(i=0;i<NE;++i)
            if((u=s[i])!=(v=t[i])&&in[v]>c[i])
                pre[v]=u,in[v]=c[i];
        pre[root]=-1,in[root]=0;
        for(i=1;i<=NV;++i)
            if(in[i]==oo)return -1;
        for(i=1;i<=NV;++i)id[i]=-1,ret+=in[i];
        for(cnt=0,i=1;i<=NV;++i)
            if(id[i]<0)
            {
                r=0,j=i;
                while(j>=0&&id[j]<0)
                {
                    if(id[j]==-2)
                    {
                        id[j]=++cnt;
                        while(q[--r]!=j)id[q[r]]=cnt;
                    }
                    else id[q[r++]=j]=-2,j=pre[j];
                }
                while(r--)id[q[r]]=++cnt;
            }
        if(cnt==NV)break;
        for(i=0;i<NE;++i)
        {
            j=t[i],s[i]=id[s[i]],t[i]=id[t[i]];
            if(s[i]!=t[i])c[i]-=in[j];
        }
        NV=cnt;
        root=id[root];
    }
    return ret;
}
int main()
{
    int i,n,m,e;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        for(i=1;i<=n;++i)scanf("%lf%lf",&x[i],&y[i]);
        for(e=i=0;i<m;++i)
        {
            scanf("%d%d",&s[e],&t[e]);
            if(s[e]!=t[e])c[e++]=len(s[e],t[e]);
        }
        type ans=Directed_MST(1,n,e);
        if(ans<0)printf("poor snoopy\n");
        else printf("%.2lf\n",ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值