Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <cstdio>
- #include <queue>
- #include <vector>
- #define task "SHORTCUT"
- #define ll long long
- using namespace std;
- struct Tadj{
- int v;
- ll w;
- Tadj () {}
- Tadj(int u, ll t){
- v = u;
- w = t;
- }
- bool operator < (const Tadj &a) const{
- return v < a.v
- || (v == a.v && w < a.w);
- }
- };
- const int N = 1e5 + 2;
- int n, m;
- ll a[N];
- ll t;
- ll dis[N];
- vector<Tadj> adj[N];
- int par[N];
- const ll Inf = 1e18;
- void Read(){
- cin >> n >> m >> t;
- for(int i = 1; i <= n; ++i)
- cin >> a[i];
- for(int i = 1; i <= m; ++i){
- int a, b, c;
- cin >> a >> b >> c;
- adj[a].push_back(Tadj(b, c));
- adj[b].push_back(Tadj(a, c));
- }
- // for(int i = 1; i <= n; ++i)
- // sort(adj[i].rbegin(), adj[i].rend());
- }
- struct Tque{
- int v;
- ll w;
- Tque() {}
- Tque(int u, ll t){
- v = u;
- w = t;
- }
- bool operator < (const Tque& a) const{
- return w > a.w;
- }
- bool Valid(){
- return w == dis[v];
- }
- };
- void Init(){
- fill_n(dis, N, Inf);
- dis[1] = 0;
- par[1] = 1;
- }
- bool Relax(int c, int i, ll w){
- ll res = dis[c] + w;
- if(dis[i] > res){
- dis[i] = res;
- return true;
- }
- if(dis[i] == res)
- par[i] = min(par[i], c);
- return false;
- }
- void Dijkstra(){
- Init();
- priority_queue<Tque, vector<Tque> > s;
- s.push(Tque(1, 0));
- while(s.size()){
- Tque c = s.top();
- s.pop();
- if(!c.Valid())
- continue;
- for(Tadj i : adj[c.v])
- if(Relax(c.v, i.v, i.w)){
- s.push(Tque(i.v, dis[i.v]));
- par[i.v] = c.v;
- }
- }
- }
- void dfs(int i){
- for(Tadj j : adj[i])
- if(par[j.v] == i){
- dfs(j.v);
- a[i] += a[j.v];
- }
- }
- void trace(int i){
- if(i == 1){
- cerr << 1;
- return;
- }
- cerr << i << " ";
- trace(par[i]);
- }
- void Solve(){
- Dijkstra();
- //trace(n);cerr << '\n';
- dfs(1);
- ll res = 0;
- for(int i = 1; i <= n; ++i){
- if(dis[i] != Inf)
- res = max(res, a[i] * (dis[i] - t));
- }
- cout << res;
- }
- main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0); if(fopen(task".INP", "r"))
- freopen(task".INP", "r", stdin),
- freopen(task".OUT", "w", stdout);
- if(fopen("shortcut.in", "r"))
- freopen("shortcut.in", "r", stdin),
- freopen("shortcut.out", "w", stdout);
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment