hdu4009 Transfer water(最小树形图)

题目链接
Problem Description
XiaoA lives in a village. Last year flood rained the village. So they decide to move the whole village to the mountain nearby this year. There is no spring in the mountain, so each household could only dig a well or build a water line from other household. If the household decide to dig a well, the money for the well is the height of their house multiplies X dollar per meter. If the household decide to build a water line from other household, and if the height of which supply water is not lower than the one which get water, the money of one water line is the Manhattan distance of the two households multiplies Y dollar per meter. Or if the height of which supply water is lower than the one which get water, a water pump is needed except the water line. Z dollar should be paid for one water pump. In addition,therelation of the households must be considered. Some households may do not allow some other households build a water line from there house. Now given the 3‐dimensional position (a, b, c) of every household the c of which means height, can you calculate the minimal money the whole village need so that every household has water, or tell the leader if it can’t be done.

Input
Multiple cases.
First line of each case contains 4 integers n (1<=n<=1000), the number of the households, X (1<=X<=1000), Y (1<=Y<=1000), Z (1<=Z<=1000).
Each of the next n lines contains 3 integers a, b, c means the position of the i‐th households, none of them will exceeded 1000.
Then next n lines describe the relation between the households. The n+i+1‐th line describes the relation of the i‐th household. The line will begin with an integer k, and the next k integers are the household numbers that can build a water line from the i‐th household.
If n=X=Y=Z=0, the input ends, and no output for that.

Output
One integer in one line for each case, the minimal money the whole village need so that every household has water. If the plan does not exist, print “poor XiaoA” in one line.

Sample Input
2 10 20 30
1 3 2
2 4 1
1 2
2 1 2
0 0 0 0

Sample Output
30

HintIn 3‐dimensional space Manhattan distance of point A (x1, y1, z1) and B(x2, y2, z2) is |x2‐x1|+|y2‐y1|+|z2‐z1|.

Source
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest

题意:第一行输入n,x,y,z,再给出n个点的三维坐标(a,b,c),表示n个村庄,其中a,b,c分别表示长,宽,高。然后对于每一个村庄给出一些友好村庄,该村庄可以去友好村庄取水。现在,要使每一个村庄都能供上水。供水取决于如下两种方式:
第一种,自己挖井,花销是x乘上ci。第二种是到友好村庄中取,花销是y乘上两个村庄的哈曼顿距离,如果友好村庄海拔比自己村庄低,还要加上z。
思路:这个题的做法在cf里碰到过就是建立一个超级源点求一下最小生成树,但是略有不同的是这里给出的是有向图,所以不能用常规的生成树算法,百度了一下才知道这玩意是最小树形图,简单来说就是有向图的最小生成树。

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
struct node{
    int a,b,c;
}p[N];
struct data{
    int u,v,w;
}e[N*N];
int n,k,m,v,w,x,y,z,ans,dis[N],pre[N],id[N],vis[N];
void add(int u,int v,int w){e[m].u=u; e[m].v=v; e[m++].w=w;}
int get_dis(node a,node b){return abs(a.a-b.a)+abs(a.b-b.b)+abs(a.c-b.c);}
int Directed_MST(int root,int cnt,int sum)//root结点、点数、边数
{
    int res=0;
    while (1)
    {
        for (int i=0;i<cnt;++i) dis[i]=1e9;//初始化为无穷大
        for (int i=0;i<sum;++i)//寻找每个点的最小入边
        if (e[i].w<dis[e[i].v]&&e[i].u!=e[i].v)//更新最小入边
        {
            pre[e[i].v]=e[i].u;//记录前驱
            dis[e[i].v]=e[i].w;
        }
        for (int i=0;i<cnt;++i) //判断是否存在最小树形图
        {
            if (i==root) continue;
            if (dis[i]==1e9) return -1;//除根节点外的点存在孤立点
        }
        //寻找所有的环
        int tot=0;//记录环数
        memset(id,-1,sizeof(id));
        memset(vis,-1,sizeof(vis));
        dis[root]=0;
        for (int i=0;i<cnt;++i)//标记每个环
        {
            res+=dis[i];//记录权值
            int v=i;
            while (v!=root&&id[v]==-1&&vis[v]!=i) vis[v]=i,v=pre[v];//寻找图中有向环,三种情况会终止:找到出现同样标记的点、结点已属其他环、遍历到根
            if (v!=root&&id[v]==-1)
            {
                for (int j=pre[v];j!=v;j=pre[j]) id[j]=tot;
                id[v]=tot++;
            }
        }
        if (!tot) break;
        for (int i=0;i<cnt;++i)
        if (id[i]==-1) id[i]=tot++;
        for (int i=0;i<sum;++i)
        {
            int v=e[i].v;
            e[i].u=id[e[i].u];
            e[i].v=id[e[i].v];
            if (e[i].u!=e[i].v) e[i].w-=dis[v];
        }
        cnt=tot; root=id[root];
    }
    return res;
}
int main()
{
    while ((scanf("%d%d%d%d",&n,&x,&y,&z))&&(n+x+y+z))
    {
        m=0;
        for (int i=1;i<=n;++i)
        {
            scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].c);
            add(0,i,p[i].c*x);
        }
        for (int i=1;i<=n;++i)
        {
            scanf("%d",&k);
            for (int j=1;j<=k;++j)
            {
                scanf("%d",&v);
                if (i==v) continue;
                w=get_dis(p[i],p[v])*y;
                if (p[v].c>p[i].c) w+=z;
                add(i,v,w);
            }
        }
        ans=Directed_MST(0,n+1,m);
        if (ans==-1) printf("poor XiaoA\n");
        else printf("%d\n",ans);
    }
    return 0;
}

这里贴一个最小树形图的板子

struct Edge{
    int x,y;
    int w;
}edge[N];
int vis[N];
int id[N];//结点所属环编号
int in[N],pre[N];//in[]为最小入边权,pre[]为其对应的起点
int zhuLiu(int root,int n,int m){//root结点、点数、边数
    int res=0;//最小树形图总权值
    while(true){
        for(int i=0;i<n;i++)//初始化为无穷大
            in[i]=INF;
 
        //寻找每个点的最小入边
        for(int i=0;i<m;i++){//遍历每条边
            int x=edge[i].x;
            int y=edge[i].y;
            if(edge[i].w<in[y] && x!=y){//更新最小入边
                pre[y]=x;//记录前驱
                in[y]=edge[i].w;//更新
            }
        }
 
        //判断是否存在最小树形图
        for(int i=0;i<n;i++){
            if(i==root)
                continue;
            if(in[i]==INF)//除根节点外的点存在孤立点
                return -1;
        }
 
        //寻找所有的环
        int cnt=0;//记录环数
        in[root]=0;
        memset(id,-1,sizeof(id));
        memset(vis,-1,sizeof(vis));
        for(int i=0;i<n;i++){//标记每个环
            res+=in[i];//记录权值
 
            int y=i;
            while(vis[y]!=i&&id[y]==-1&&y!=root){//寻找图中有向环
                //三种情况会终止:找到出现同样标记的点、结点已属其他环、遍历到根
                vis[y]=i;//标记
                y=pre[y];//向上找
            }
 
            if(y!=root&&id[y]==-1){//没有遍历到根或没有找到结点属于其他环,说明找到有向环
                for(int x=pre[y];x!=y;x=pre[x])//标记结点x为第几个环
                    id[x]=cnt;//记录结点所属环号
                id[y]=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 x=edge[i].x;
            int y=edge[i].y;
            edge[i].x=id[x];
            edge[i].y=id[y];
 
            if(id[x]!=id[y])//两点不在同一环内,更新边权值
                edge[i].w-=in[y];//x到y的距离为边权-in[y]
        }
 
        n=cnt;//以环数为下次操作的点数,继续上述操作,直到无环
        root=id[root];
    }
    return res;
}
int main(){
    int n,m;//n个点m条有向边
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){//建图
        scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].w);
        if(edge[i].x==edge[i].y)//除去自环,即点到自身距离为INF
            edge[i].w=INF;
    }
    int res=zhuLiu(0,n,m);
    if(res==-1)
        printf("No\n");
    else
        printf("%d\n",res);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值