标签:lan sid closed 特殊 前缀和 tran str contains not
Problem Description
Input
Output
Sample Input
Sample Output
Source
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1005; 4 struct node{ 5 int a,b,c; 6 }p[N]; 7 struct data{ 8 int u,v,w; 9 }e[N*N]; 10 int n,k,m,v,w,x,y,z,ans,dis[N],pre[N],id[N],vis[N]; 11 void add(int u,int v,int w){e[m].u=u; e[m].v=v; e[m++].w=w;} 12 int get_dis(node a,node b){return abs(a.a-b.a)+abs(a.b-b.b)+abs(a.c-b.c);} 13 int Directed_MST(int root,int cnt,int sum) 14 { 15 int res=0; 16 while (1) 17 { 18 for (int i=0;i<cnt;++i) dis[i]=1e9; 19 for (int i=0;i<sum;++i) 20 if (e[i].w<dis[e[i].v]&&e[i].u!=e[i].v) 21 { 22 pre[e[i].v]=e[i].u; 23 dis[e[i].v]=e[i].w; 24 } 25 for (int i=0;i<cnt;++i) 26 { 27 if (i==root) continue; 28 if (dis[i]==1e9) return -1; 29 } 30 int tot=0; 31 memset(id,-1,sizeof(id)); 32 memset(vis,-1,sizeof(vis)); 33 dis[root]=0; 34 for (int i=0;i<cnt;++i) 35 { 36 res+=dis[i]; 37 int v=i; 38 while (v!=root&&id[v]==-1&&vis[v]!=i) vis[v]=i,v=pre[v]; 39 if (v!=root&&id[v]==-1) 40 { 41 for (int j=pre[v];j!=v;j=pre[j]) id[j]=tot; 42 id[v]=tot++; 43 } 44 } 45 if (!tot) break; 46 for (int i=0;i<cnt;++i) 47 if (id[i]==-1) id[i]=tot++; 48 for (int i=0;i<sum;++i) 49 { 50 int v=e[i].v; 51 e[i].u=id[e[i].u]; 52 e[i].v=id[e[i].v]; 53 if (e[i].u!=e[i].v) e[i].w-=dis[v]; 54 } 55 cnt=tot; root=id[root]; 56 } 57 return res; 58 } 59 int main() 60 { 61 while ((scanf("%d%d%d%d",&n,&x,&y,&z))&&(n+x+y+z)) 62 { 63 m=0; 64 for (int i=1;i<=n;++i) 65 { 66 scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].c); 67 add(0,i,p[i].c*x); 68 } 69 for (int i=1;i<=n;++i) 70 { 71 scanf("%d",&k); 72 for (int j=1;j<=k;++j) 73 { 74 scanf("%d",&v); 75 if (i==v) continue; 76 w=get_dis(p[i],p[v])*y; 77 if (p[v].c>p[i].c) w+=z; 78 add(i,v,w); 79 } 80 } 81 ans=Directed_MST(0,n+1,m); 82 if (ans==-1) printf("poor XiaoA\n"); 83 else printf("%d\n",ans); 84 } 85 return 0; 86 }
标签:lan sid closed 特殊 前缀和 tran str contains not
原文地址:http://www.cnblogs.com/zk1431043937/p/7763852.html