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("\n---------------\n")
#define D_e(x) cout << (#x) << " : " << x << "\n"
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define TIME() fprintf(stderr, "\nTIME : %.3lfms\n", 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;
}

posted @ 2019-10-26 21:02  邱涵的秘密基地  阅读(168)  评论(0编辑  收藏  举报