題意:
n個點 X Y Z (點標從1開始)
下面n個點的坐標(三維)
下面第i行表示i點能向 u 點引流
給定n個村要用水
1、自己打井,花費:坐標的高度*X
2、從有井的村落引流,花費:曼哈頓距離*Y (若水是往高處流的,還要花費Z購買水泵)
3、假設開始時所有村落沒有任何井和管道
問:
若大家都能用上水則輸出最小花費,否則輸出poor XiaoA
思路:
因為引流一定要從有井的村落開始,所以建虛根0,連向每個村落,邊權為打井花費。
然後跑個最小樹形圖
#include#include #include #include #include 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 */