zoukankan      html  css  js  c++  java
  • LuoguP5201 [USACO19JAN]Shortcut(最短路树)

    字典序?建树时从小枚举,用(Dijkstra)的血泪建好树,(size)大小决定贡献

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #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 Fill(a,b) memset(a, b, sizeof(a))
    #define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
    #define QWQ
    #ifdef QWQ
    #define D_e_Line printf("
    ---------------
    ")
    #define D_e(x) cout << (#x) << " : " << x << "
    "
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt", "r", stdin)
    #define FileSave() freopen("out.txt", "w", stdout)
    #define TIME() fprintf(stderr, "
    TIME : %.3lfms
    ", clock() * 1000.0 / CLOCKS_PER_SEC)
    #else
    #define D_e_Line ;
    #define D_e(x) ;
    #define Pause() ;
    #define FileOpen() ;
    #define FileSave() ;
    #define TIME() ;
    #endif
    struct ios {
    	template<typename ATP> inline 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;
    template<typename ATP> inline ATP Max(ATP a, ATP b) {
    	return a > b ? a : b;
    }
    template<typename ATP> inline ATP Min(ATP a, ATP b) {
    	return a < b ? a : b;
    }
    template<typename ATP> inline ATP Abs(ATP a) {
    	return a < 0 ? -a : a;
    }
    #include <queue>
    #include <vector>
    
    const int N = 1e4 + 7;
    const int M = 5e4 + 7;
    
    #define int long long
    int n, m, T;
    int SIZ[N], dis[N];
    
    struct Edge {
    	int nxt, pre, w;
    } e[M << 1];
    int head[N], cntEdge;
    inline void add(int u, int v, int w) {
    	e[++cntEdge] = (Edge){ head[u], v, w}, head[u] = cntEdge;
    }
    struct nod {
    	int x, w;
    	bool operator < (const nod &com) const {
    		return w > com.w;
    	}
    };
    priority_queue<nod> q;
    inline void Dijkstra(int st) {
    	Fill(dis, 0x3f);
    	dis[st] = 0;
    	q.push((nod){ st, 0});
    	while(!q.empty()){
    		int u = q.top().x, w = q.top().w;
    		q.pop();
    		if(w != dis[u]) continue;
    		for(register int i = head[u]; i; i = e[i].nxt){
    			int v = e[i].pre;
    			if(dis[v] > dis[u] + e[i].w){
    				dis[v] = dis[u] + e[i].w;
    				q.push((nod){ v, dis[v]});
    			}
    		}
    	}
    }
    
    int ans;
    vector<int> G[N];
    bool vis[N];
    inline void Build()	{
    	R(u,1,n){
    		for(register int i = head[u]; i; i = e[i].nxt){
    			int v = e[i].pre;
    			if(dis[v] == dis[u] + e[i].w && !vis[v]){
    				vis[v] = 1;
    				G[u].push_back(v);
    			}
    		}
    	}
    }
    
    inline int DFS(int u) {
    	int siz = SIZ[u];
    	for(vector<int>::iterator v = G[u].begin(); v != G[u].end(); ++v){
    //		if(*v == fa) continue;
    		siz += DFS(*v);
    	}
    	ans = Max(ans, siz * (dis[u] - T));
    	return siz;
    }
    #undef int
    int main() {
    #define int long long
    // FileOpen();
    	io >> n >> m >> T;
    	R(i,1,n){
    		io >> SIZ[i];
    	}
    	R(i,1,m){
    		int u, v, w;
    		io >> u >> v >> w;
    		add(u, v, w);
    		add(v, u, w);
    	}
    	
    	Dijkstra(1);
    	
    	Build();
    	DFS(1);
    	
    	printf("%lld", ans);
    	
    	return 0;
    }
    

  • 相关阅读:
    北京燃气IC卡充值笔记
    随机分析、随机控制等科目在量化投资、计算金融方向有哪些应用?
    量化交易平台大全
    Doctor of Philosophy in Computational and Mathematical Engineering
    Institute for Computational and Mathematical Engineering
    Requirements for the Master of Science in Computational and Mathematical Engineering
    MSc in Mathematical and Computational Finance
    万字长文:详解多智能体强化学习的基础和应用
    数据处理思想和程序架构: 使用Mbedtls包中的SSL,和服务器进行网络加密通信
    31-STM32+W5500+AIR202/302基本控制篇-功能优化-W5500移植mbedtls库以SSL方式连接MQTT服务器(单向忽略认证)
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11745522.html
Copyright © 2011-2022 走看看