H - Transfer water (不定根)

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

Hint

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

题意:第一行输入n,x,y,z,再给出n个点的三维坐标(a,b,c),表示n个村庄,其中a,b,c分别表示长,宽,高。然后对于每一个村庄给出一些友好村庄,该村庄可以去友好村庄取水。现在,要使每一个村庄都能供上水。供水取决于如下两种方式:
第一种,自己挖井,花销是x乘上ci。第二种是到友好村庄中取,花销是y乘上两个村庄的哈曼顿距离,如果友好村庄海拔比自己村庄低,还要加上z。
最后问所有村庄都供上水的最小花费。
提示:哈曼顿距离为abs(ai-aj)+abs(bi-bj)+abs(ci-cj)。(推荐博客

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1005;
struct nede
{
    int u;
    int v;
    int cost;
} Edge[maxn*maxn];
struct node
{
  int a, b, c;
}p[maxn];
int pre[maxn], visit[maxn], id[maxn];
int in[maxn];
int ma;
//int m;
int zhuliu(int root, int n, int m)
{
    int res = 0;
    int v;
    while(1)
    {
        for(int i=0; i<n; i++)//到达当前点的最小权值,先初始化
            in[i] = INF;
        for(int i=0; i<m; i++)//遍历每一个边
        {
            if(Edge[i].u!=Edge[i].v&&Edge[i].cost<in[Edge[i].v])//如果是一个边并且权值小于到达当前点的权值,更新
            {
                pre[Edge[i].v] = Edge[i].u;//pre记录前一个点,也就是从哪一点到达当前点
                in[Edge[i].v] = Edge[i].cost;//更新权值
//                if(Edge[i].u==root)
//                    edge_point = i;
            }
        }
        for(int i=0; i<n; i++)//遍历每一个节点
        {
            if(i!=root&&in[i]==INF)//如果当前点不是根节点,但入度值最小为INF,也就是没有任何边指向此点,那么就不存在最小树形图
                return -1;//返回-1
        }
        memset(id, -1, sizeof(id));//记录环的标号
        memset(visit, -1, sizeof(visit));//是否访问过
        int tn = 0;//环数
        in[root] = 0;//根节点清零
        for(int i=0; i<n; i++)//遍历每个节点,求最短弧的集合
        {
            res += in[i];//加入权值
            int v = i;
            while(visit[v]!=i&&id[v]==-1&&v!=root)//如果当前点没有被访问过,并且不是根节点,并且不在任何一个环上
            {
                visit[v] = i;
                v = pre[v];
            }
            if(id[v]==-1&&v!=root)
            {
                for(int u=pre[v]; u!=v; u=pre[u])
                {
                    id[u] = tn;
                }
                id[v] = tn++;
            }
        }
        if(tn==0)//没有环出现,则找到了最小树形图
            break;//跳出
        for(int i=0; i<n; i++)//收缩有向环
        {
            if(id[i]==-1)
            {
                id[i] = tn++;
            }
        }
        for(int i=0; i<m;i++)
        {
            v = Edge[i].v;
//            int u = Edge[i].u;
            Edge[i].u = id[Edge[i].u];
            Edge[i].v = id[Edge[i].v];
            if(Edge[i].u!=Edge[i].v)
                Edge[i].cost -= in[v];
//            else
//                swap(Edge[i], Edge[--m]);
        }
        n = tn;
        root = id[root];
    }
    return res;
}
int dis(struct node aa, struct node bb)
{
   return fabs(bb.a-aa.a)+fabs(bb.b-aa.b)+fabs(bb.c-aa.c);
}
void add(int u, int v, int w)
{
  Edge[ma].u = u;
  Edge[ma].v = v;
  Edge[ma++].cost = w;
}
int main()
{
    int n, x, y, z, v, w;
    while(~scanf("%d %d %d %d", &n, &x, &y, &z)&&n&&x&&y&&z)
    {
        ma = 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);
        }
        int k;
        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 = dis(p[i], p[v])*y;
              if(p[v].c>p[i].c)
              w += z;
              add(i, v, w);
           }
        }
        //edge_w++;
//        for(int i=0; i<n; i++)
//        {
//            Edge[i+m].u = 0;
//            Edge[i+m].v = i+1;
//            Edge[i+m].cost = edge_w;
//        }
        int res = zhuliu(0, n+1, ma);
        if(res==-1)
            printf("poor XiaoA\n");
        else
            printf("%d\n", res);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值