zoukankan      html  css  js  c++  java
  • LuoguP1131 [ZJOI2007]时态同步 (树形DP,贪心)

    贪心就离根最大距离

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
    #define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
    #define Max(a,b) ((a) > (b) ? (a) : (b))
    #define Min(a,b) ((a) < (b) ? (a) : (b))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Abs(a) ((a) < 0 ? -(a) : (a))
    #define Swap(a,b) a^=b^=a^=b
    #define ll long long
    
    #define ON_DEBUG
    
    #ifdef ON_DEBUG
    
    #define D_e_Line printf("
    
    ----------
    
    ")
    #define D_e(x)  cout << #x << " = " << x << endl
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt","r",stdin);
    
    #else
    
    #define D_e_Line ;
    #define D_e(x)  ;
    #define Pause() ;
    #define FileOpen() ;
    
    #endif
    
    struct ios{
        template<typename ATP>ios& operator >> (ATP &x){
            x = 0; int f = 1; char c;
            for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
            while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
            x*= f;
            return *this;
        }
    }io;
    using namespace std;
    
    const int N = 500007;
    
    struct Edge{
    	int nxt, pre, w;
    }e[N << 1];
    int head[N], cntEdge;
    inline void add(int u, int v, int w){
    	e[++cntEdge] = (Edge){ head[u], v, w}, head[u] = cntEdge;
    }
    
    long long ans;
    int dis[N];
    inline void DFS(int u, int fa){
    	for(register int i = head[u]; i; i = e[i].nxt){
    		int v = e[i].pre;
    		if(v == fa) continue;
    		DFS(v, u);
    		dis[u] = Max(dis[u], dis[v] + e[i].w);
    	}
    	for(register int i = head[u]; i; i = e[i].nxt){
    		int v = e[i].pre;
    		if(v == fa) continue;
    		ans += dis[u] - dis[v] - e[i].w;
    	}
    }
    
    int main(){
    	int n, rt;
    	io >> n >> rt;
    	R(i,2,n){
    		int u, v, w;
    		io >> u >> v >> w;
    		add(u, v, w);
    		add(v, u, w);
    	}
    	
    	DFS(rt, 0);
    	
    	printf("%lld", ans);
    	
    	return 0;
    }
    

  • 相关阅读:
    DELPHI IDFTP
    关于网络的一些小知识
    bootstrap弹出框
    GIt的简单使用
    Ubantu搭建虚拟环境
    python中的随机模块random
    python中动态创建类
    关于深浅拷贝的测试
    关于面向对象的属性访问
    多任务的使用模式
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11252116.html
Copyright © 2011-2022 走看看