HDU 4966 ggs-ddu (Minimum tree shape-Zhu Liu algorithm)

Source: Internet
Author: User

Title Address: HDU 4966
I didn't see it at first. Total level only 500 this condition, as each one is 500. Then the idea of building a picture is crooked ..... It was only then that only 500 of the total was found. Then it's easy to build a map. Set each level of each subject to a single point and set all 0 levels to the same root. Then the high level of all the subjects to a lower level with a weight of 0 has a forward edge, the first function is to ensure that the final minimum tree is all the points can be reached, the second role is to ensure that each lesson of the required level, as long as the high level, so that the lower grade also meet. Then you can connect to each session.
The code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace STD;#define LL __int64#define PI ACOs ( -1.0)//#pragma comment (linker, "/stack:1024000000")Const intMod=1e9+7;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-9;Const intmaxn=40000+Ten;intCNT, NV;intin[510], color[510], id[510], pre[510], a[ -], num[ -];structnode{intU, V, W;} edge[20000];voidAddintUintVintW) {edge[cnt].v=v;        Edge[cnt].u=u; Edge[cnt++].w=w;}intD_mst (intRoot) {intans=0, I, u, v; while(1){memset(In,inf,sizeof(in)); for(i=0; i<cnt;i++) {u=edge[i].u; V=EDGE[I].V;if(U!=v&&edge[i].w<in[v])                                {IN[V]=EDGE[I].W;                        Pre[v]=u; }                } for(i=1; i<=nv;i++) {if(In[i]==inf&&i!=root)return-1; }inttot=0;memset(color,-1,sizeof(color));memset(id,-1,sizeof(ID)); in[root]=0; for(i=1; i<=nv;i++) {ans+=in[i]; U=i; while(color[u]!=i&&id[u]==-1&&u!=root) {color[u]=i;                        U=pre[u]; }if(id[u]==-1&&u!=root) {Id[u]=++tot; for(V=pre[u];v!=u;v=pre[v])                                {Id[v]=tot; }                        }                }if(!tot) Break; for(i=1; i<=nv;i++) {if(id[i]==-1) Id[i]=++tot; } for(i=0; i<cnt;i++) {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].W-=IN[V];                }} Nv=tot;        Root=id[root]; }returnAns;}intMain () {intN, M, I, J, K, C, D, L1, L2, W, ans; while(scanf("%d%d", &n,&m)!=eof&&n+m) {num[0]=1; for(i=1; i<=n;i++) {scanf("%d", &a[i]); num[i]=num[i-1]+a[i]; } cnt=0; Nv=num[n]; for(i=1; i<=n;i++) { for(j=1; j<=a[i];j++) {Add (num[i-1]+j,1,0); for(k=1; k<j;k++) {Add (num[i-1]+j,num[i-1]+k,0); }                        }                } while(m--) {scanf("%d%d%d%d%d", &c,&l1,&d,&l2,&w); Add (l1?num[c-1]+L1:1, l2?num[d-1]+L2:1, W); } ans=d_mst (1);printf("%d\n", ans); }return 0;}

HDU 4966 ggs-ddu (Minimum tree shape-Zhu Liu algorithm)

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.