HDU-4009 Transfer Water (minimum tree shape)

Source: Internet
Author: User

Main topic: There are n points, each point has the corresponding three-dimensional coordinates (x, y, z)
Now there are two ways to get water at every point, or water.
1. Dig your own well, at a cost of x * Altitude Z
2. Laying pipelines for diversion.
A. If the altitude is less than the water diversion, the cost is between two Manhattan distances *y
B. If the altitude is greater than the drinking place, the cost is two Manhattan distance *y + Z

Problem-solving ideas: Set a virtual root, the virtual root leads to all points, the weight of the cost of digging well, and then according to the requirements of the edge, to find the smallest tree chart can be

#include <cstdio>#include <cstring>#define N 1010#define ABS (a) ((a) >0? ( A):(-(a)))structedge{intU, V, c;} E[n*n];structpoint{intX, Y, Z;} P[n];intn, x, y, z, tot;voidAddedge (intUintVintc) {e[tot].u = u; E[TOT].V = v; E[TOT++].C = C;}voidInit () {tot =0; for(inti =1; I <= N; i++) {scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].z); Addedge (0, I, p[i].z * x); }intK, T; for(inti =1; I <= N; i++) {scanf("%d", &k); while(k--) {scanf("%d", &t);if(t = = i)Continue;if(P[i].z >= p[t].z) {intdis =ABS(p[t].x-p[i].x) +ABS(P[T].Y-P[I].Y) +ABS(P[T].Z-P[I].Z);            Addedge (i, t, dis * y); }Else{intdis =ABS(p[t].x-p[i].x) +ABS(P[T].Y-P[I].Y) +ABS(P[T].Z-P[I].Z);            Addedge (i, t, dis * y + z); }        }    }}#define INF 0x3f3f3f3fintIn[n], Pre[n], vis[n], id[n];intDirected_mst (intRootintN) {intAns =0, u, V, TMP; while(1) { for(inti =0; I < n; i++) In[i] = INF; for(inti =0; i < tot;            i++) {u = e[i].u; v = e[i].v;if(U! = v && e[i].c < in[v])                {In[v] = e[i].c;            PRE[V] = u; }        }memset(Vis,-1,sizeof(VIS));memset(ID,-1,sizeof(ID)); In[root] =0;intsubnode =0; for(inti =0; I < n;            i++) {ans + = in[i]; TMP = i; while(vis[tmp]! = i && tmp! = root && id[tmp] = =-1) {Vis[tmp] = i;            TMP = pre[tmp]; }if(Id[tmp] = =-1&& tmp! = root) {u = pre[tmp]; while(U! = tmp)                    {Id[u] = subnode;                U = pre[u];            } id[tmp] = subnode++; }        }if(subnode = =0) Break; for(inti =0; I < n; i++)if(!                (~id[i])) Id[i] = subnode++; for(inti =0; i < tot;            i++) {tmp = 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].C-= in[tmp];        } n = subnode;    root = Id[root]; }returnAns;}voidSolve () {n++;intAns = directed_mst (0, n);if(ans = =-1)printf("Poor xiaoa\n");Else        printf("%d\n", ans);}intMain () { while(scanf("%d%d%d%d", &n, &x, &y, &z)! = EOF && n + x + y + z) {init ();    Solve (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU-4009 Transfer Water (minimum tree shape)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.