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;
    }
    

  • 相关阅读:
    JAVA合并两个有序的单链表,合并之后的链表依然有序
    excel如何将一个单元格内容拆分成多个单元格?(用到了数据->分列)
    Navicat导入excel的xlsx文件提示无法打开文件
    Request对象实现请求转发
    MessageFormat.format()和String.format()
    使用Servlet动态生成验证码
    Http协议
    使用freemarker导出word
    java注解学习(1)注解的作用和三个常用java内置注解
    SSM_CRUD新手练习(6)分页后台控制器编写
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11745522.html
Copyright © 2011-2022 走看看