题意:
n个点 X Y Z (点标从1开始)
下面n个点的坐标(三维)
下面第i行表示i点能向 u 点引流
给定n个村要用水
1、自己打井,花费:坐标的高度*X
2、从有井的村落引流,花费:曼哈顿距离*Y (若水是往高处流的,还要花费Z购买水泵)
3、假设开始时所有村落没有任何井和管道
问:
若大家都能用上水则输出最小花费,否则输出poor XiaoA
思路:
因为引流一定要从有井的村落开始,所以建虚根0,连向每个村落,边权为打井花费。
然后跑个最小树形图
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <math.h> using namespace std; /* * 最小树形图 * 复杂度O(NM) * 点从0开始 */ const int INF = 100000000; const int MAXN = 1010; //点数 const int MAXM = 1010000;//边数 #define ll int struct Edge{ int u,v; ll cost; }edge[MAXM];//从0开始 int pre[MAXN],id[MAXN],visit[MAXN]; ll in[MAXN]; ll zhuliu(int root,int n,int m,Edge edge[])//任意一个起点 点数 边数 edge { int u,v; ll res=0; 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; in[edge[i].v] = edge[i].cost; } int tn = 0; memset(id,-1,sizeof(id)); memset(visit,-1,sizeof(visit)); in[root] = 0; for(int i = 0;i < n;i++) { res += in[i]; v = i; while( visit[v] != i && id[v] == -1 && v != root) { visit[v] = i; v = pre[v]; } if( v != root && id[v] == -1 ) { 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;) { v = edge[i].v; 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; } struct node{ ll a,b,c; }p[MAXN]; ll Dis(node a, node b){return abs(a.a-b.a)+abs(a.b-b.b)+abs(a.c-b.c);} int n; ll X, Y, Z; int main() { int i, j, k, u; while(scanf("%d%d%d%d", &n,&X,&Y,&Z), n+X+Y+Z){ for(i = 1; i <= n; i++) { scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].c); } int edgenum = 0; for(i = 1; i <= n; i++) { scanf("%d",&k); Edge E = {0, i, p[i].c*X}; edge[edgenum++] = E; while(k--) { scanf("%d", &u); if(u==i)continue; ll cost = Dis(p[i], p[u])*Y; if(p[u].c>p[i].c) cost+=Z; Edge E2 = {i, u, cost}; edge[edgenum++] = E2; } } printf("%d\n", zhuliu(0, n+1, edgenum, edge)); } return 0; } /* 2 10 20 30 1 3 2 2 4 1 1 2 2 1 2 0 0 0 0 */