标签:
2 10 20 30 1 3 2 2 4 1 1 2 2 1 2 0 0 0 0
30
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1010; 4 const int INF = 0x3f3f3f3f; 5 struct arc { 6 int u,v,w; 7 arc(int x = 0,int y = 0,int z = 0) { 8 u = x; 9 v = y; 10 w = z; 11 } 12 } e[500000]; 13 struct Point { 14 int x,y,z; 15 } p[maxn]; 16 int pre[maxn],hs[maxn],vis[maxn],in[maxn]; 17 int calc(int a,int b) { 18 return abs(p[a].x - p[b].x) + abs(p[a].y - p[b].y) + abs(p[a].z - p[b].z); 19 } 20 int DMST(int root,int n,int m,int ret = 0) { 21 while(true) { 22 for(int i = 0; i < n; ++i) { 23 in[i] = INF; 24 vis[i] = hs[i] = -1; 25 } 26 for(int i = 0; i < m; ++i) { 27 if(e[i].u != e[i].v && e[i].w < in[e[i].v]) { 28 in[e[i].v] = e[i].w; 29 pre[e[i].v] = e[i].u; 30 } 31 } 32 for(int i = 0; i < n; ++i) 33 if(i != root && in[i] == INF) return -1; 34 int cnt = in[root] = 0; 35 for(int i = 0; i < n; ++i) { 36 ret += in[i]; 37 int v = i; 38 while(vis[v] != i && v != root && hs[v] == -1) { 39 vis[v] = i; 40 v = pre[v]; 41 } 42 if(v != root && hs[v] == -1) { 43 for(int u = pre[v]; u != v; u = pre[u]) hs[u] = cnt; 44 hs[v] = cnt++; 45 } 46 } 47 if(!cnt) break; 48 for(int i = 0; i < n; ++i) 49 if(hs[i] == -1) hs[i] = cnt++; 50 for(int i = 0; i < m; ++i) { 51 int u = e[i].u; 52 int v = e[i].v; 53 e[i].u = hs[u]; 54 e[i].v = hs[v]; 55 if(e[i].u != e[i].v) e[i].w -= in[v]; 56 } 57 n = cnt; 58 root = hs[root]; 59 } 60 return ret; 61 } 62 int main() { 63 int n,m,X,Y,Z; 64 while(scanf("%d%d%d%d",&n,&X,&Y,&Z),n||X||Y||Z) { 65 for(int i = 1; i <= n; ++i) 66 scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z); 67 m = 0; 68 for(int i = 1,k,u; i <= n; ++i) { 69 scanf("%d",&k); 70 while(k--) { 71 scanf("%d",&u); 72 int tmp = calc(i,u)*Y; 73 if(p[i].z < p[u].z) tmp += Z; 74 e[m++] = arc(i,u,tmp); 75 } 76 } 77 for(int i = 1; i <= n; ++i) 78 e[m++] = arc(0,i,p[i].z*X); 79 printf("%d\n",DMST(0,n + 1,m)); 80 } 81 return 0; 82 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4761871.html