zoukankan      html  css  js  c++  java
  • BZOJ1827: [Usaco2010 Mar]gather 奶牛大集会

    1827: [Usaco2010 Mar]gather 奶牛大集会

    Time Limit: 1 Sec  Memory Limit: 64 MB
    Submit: 689  Solved: 295
    [Submit][Status]

    Description

    Bessie 正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会。当然,她会选择最方便的地点来举办这次集会。每个奶牛居住在 N(1<=N<=100,000) 个农场中的一个,这些农场由N-1条道路连接,并且从任意一个农场都能够到达另外一个农场。道路i连接农场A_i和B_i(1 <= A_i <=N; 1 <= B_i <= N),长度为L_i(1 <= L_i <= 1,000)。集会可以在N个农场中的任意一个举行。另外,每个牛棚中居住者C_i(0 <= C_i <= 1,000)只奶牛。在选择集会的地点的时候,Bessie希望最大化方便的程度(也就是最小化不方便程度)。比如选择第X个农场作为集会地点,它的不方 便程度是其它牛棚中每只奶牛去参加集会所走的路程之和,(比如,农场i到达农场X的距离是20,那么总路程就是C_i*20)。帮助Bessie找出最方 便的地点来举行大集会。 考虑一个由五个农场组成的国家,分别由长度各异的道路连接起来。在所有农场中,3号和4号没有奶牛居住。

    Input

    第一行:一个整数N * 第二到N+1行:第i+1行有一个整数C_i * 第N+2行到2*N行,第i+N+1行为3个整数:A_i,B_i和L_i。

    Output

    * 第一行:一个值,表示最小的不方便值。

    Sample Input

    5
    1
    1
    0
    0
    2
    1 3 1
    2 3 2
    3 4 3
    4 5 3

    Sample Output

    15

    HINT

    Source

    Gold

    题解:

    原来居然不会做,是有多弱。。。

    今天又来看这题,发现好水,类似与POI 的 STA,记录一个节点的子树有多少头牛,然后任何一个节点的ans可以由它的父节点在O(1)的时间算出

    所以一次dfs+一次dp就ok了

    代码:

      1 #include<cstdio>
      2 
      3 #include<cstdlib>
      4 
      5 #include<cmath>
      6 
      7 #include<cstring>
      8 
      9 #include<algorithm>
     10 
     11 #include<iostream>
     12 
     13 #include<vector>
     14 
     15 #include<map>
     16 
     17 #include<set>
     18 
     19 #include<queue>
     20 
     21 #include<string>
     22 
     23 #define inf 100000000000000
     24 
     25 #define maxn 150000
     26 
     27 #define maxm 500+100
     28 
     29 #define eps 1e-10
     30 
     31 #define ll long long
     32 
     33 #define pa pair<int,int>
     34 
     35 #define for0(i,n) for(int i=0;i<=(n);i++)
     36 
     37 #define for1(i,n) for(int i=1;i<=(n);i++)
     38 
     39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
     40 
     41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
     42 
     43 #define mod 1000000007
     44 
     45 using namespace std;
     46 
     47 inline int read()
     48 
     49 {
     50 
     51     int x=0,f=1;char ch=getchar();
     52 
     53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     54 
     55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
     56 
     57     return x*f;
     58 
     59 }
     60 struct edge{int go,next;ll w;}e[2*maxn];
     61 int n,tot,head[maxn];
     62 ll sum,s[maxn],f[maxn];
     63 bool v[maxn];
     64 inline void insert(int x,int y,int z)
     65 {
     66     e[++tot].go=y;e[tot].next=head[x];head[x]=tot;e[tot].w=z;
     67     e[++tot].go=x;e[tot].next=head[y];head[y]=tot;e[tot].w=z;
     68 }
     69 void dfs(int x)
     70 {
     71     v[x]=1;
     72     for(int i=head[x],y;i;i=e[i].next)
     73     if(!v[y=e[i].go])
     74     {
     75         dfs(y);
     76         s[x]+=s[y];
     77         f[x]+=f[y]+s[y]*e[i].w;
     78     }
     79 }
     80 void dp(int x)
     81 {
     82     v[x]=0;
     83     for(int i=head[x],y;i;i=e[i].next)
     84     if(v[y=e[i].go])
     85     {
     86         f[y]=f[x]+(sum-s[y]-s[y])*e[i].w;
     87         dp(y);
     88     }
     89 }
     90 
     91 int main()
     92 
     93 {
     94 
     95     freopen("input.txt","r",stdin);
     96 
     97     freopen("output.txt","w",stdout);
     98 
     99     n=read();
    100     for1(i,n)s[i]=read(),sum+=s[i];
    101     for1(i,n-1){int x=read(),y=read(),z=read();insert(x,y,z);}
    102     dfs(1);
    103     dp(1);
    104     ll ans=inf;
    105     for1(i,n)ans=min(ans,f[i]);
    106     printf("%lld
    ",ans);
    107 
    108     return 0;
    109 
    110 }
    View Code

    inf开到1000亿然后WA了,怒加了几个0就A了T_T

  • 相关阅读:
    2.16.8.内核启动的C语言阶段5
    2.16.7.内核启动的C语言阶段4
    2.16.6.内核启动的C语言阶段3
    2.16.5.内核启动的C语言阶段2
    JAVA_SE基础——34.static修饰成员变量
    JAVA_SE基础——33.this关键字的练习
    JAVA_SE基础——32.this关键字调用本类的构造方法
    JAVA_SE基础——31.this关键字
    JAVA类的方法调用和变量(全套)
    JAVA_SE基础——30.构造代码块
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4029435.html
Copyright © 2011-2022 走看看