結果

問題 No.848 なかよし旅行
ユーザー chocoruskchocorusk
提出日時 2020-05-08 08:41:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 3,703 bytes
コンパイル時間 1,416 ms
コンパイル使用メモリ 130,268 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2024-04-25 14:16:44
合計ジャッジ時間 3,269 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
7,680 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 4 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 33 ms
6,944 KB
testcase_12 AC 44 ms
6,944 KB
testcase_13 AC 60 ms
6,940 KB
testcase_14 AC 19 ms
6,940 KB
testcase_15 AC 56 ms
6,944 KB
testcase_16 AC 94 ms
8,064 KB
testcase_17 AC 71 ms
7,296 KB
testcase_18 AC 33 ms
6,940 KB
testcase_19 AC 29 ms
6,944 KB
testcase_20 AC 6 ms
6,940 KB
testcase_21 AC 81 ms
7,424 KB
testcase_22 AC 88 ms
7,424 KB
testcase_23 AC 11 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 101 ms
8,192 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 1 ms
6,944 KB
testcase_28 AC 1 ms
6,944 KB
testcase_29 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
using namespace std;
typedef long long int ll;
typedef pair<ll, int> P;
template<typename val_t, typename key_t>
struct FibonacciHeap{
	struct Node{
		val_t val;
		key_t key;
		int deg;
		Node *par, *ch, *l, *r;
		bool mark;
		Node(val_t val, key_t key):val(val), key(key), par(nullptr), ch(nullptr), l(this), r(this), mark(false), deg(0){}
	};
	vector<Node*> vr;
	Node *min_node;
	int sz;
	FibonacciHeap():sz(0), min_node(nullptr){}
	bool empty(){
		return sz==0;
	}
	void concat(Node *&s, Node *t){
		if(!t) return;
		if(!s){
			s=t;
			return;
		}
		s->r->l=t->l;
		t->l->r=s->r;
		s->r=t;
		t->l=s;
	}
	void delete_node(Node *t){
		t->l->r=t->r;
		t->r->l=t->l;
		t->l=t;
		t->r=t;
	}
	Node *push(val_t val, key_t key){
		sz++;
		Node *t=new Node(val, key);
		concat(min_node, t);
		if(!min_node || t->val<min_node->val) min_node=t;
		return t;
	}
	void link(Node *t, Node *c){
		t->deg++;
		delete_node(c);
		c->par=t;
		concat(c, t->ch);
		if(!t->ch) t->ch=c;
	}
	pair<val_t, key_t> pop(){
		sz--;
		pair<val_t, key_t> ret=make_pair(min_node->val, min_node->key);
		auto y=(min_node->r==min_node)?nullptr:min_node->r;
		delete_node(min_node);
		auto x=min_node->ch;
		if(x) x->par=nullptr;
		concat(x, y);
		min_node=nullptr;
		if(!x){
			return ret;
		}
		auto x1=x;
		do{
			x1->par=nullptr;
			if(!min_node || x1->val<min_node->val) min_node=x1;
			x1=x1->r;
		}while(x1!=x);
		x1=min_node;
		int last=-1;
		do{
			auto xr=x1->r;
			while(x1->deg<vr.size() && vr[x1->deg]){
				auto x2=vr[x1->deg];
				vr[x1->deg]=nullptr;
				if(x2==min_node || x1->val>x2->val){
					swap(x1, x2);
				}
				link(x1, x2);
			}
			if(x1->deg>=vr.size()) vr.resize(x1->deg+1);
			vr[x1->deg]=x1;
			last=max(last, x1->deg);
			x1=xr;
		}while(x1!=min_node);
		for(int i=0; i<=last; i++) vr[i]=nullptr;
		return ret;
	}
	void decrease(Node *t, val_t d){//t->val=d
		t->val=d;
		if(!t->par){
			if(!min_node || t->val<min_node->val) min_node=t;
			return;
		}
		if(t->par->val<=d) return;
		while(t->par){
			auto tp=t->par;
			t->par->deg--;
			t->par->ch=((t->r==t)?nullptr:t->r);
			delete_node(t);
			t->par=nullptr;
			concat(min_node, t);
			if(!min_node || t->val<min_node->val) min_node=t;
			if(!tp->mark){
				tp->mark=1;
				break;
			}
			t=tp;
		}
	}
};
const ll INF=1e16;
int n, m, p, q;
vector<P> g[2001];
void dijkstra(int s, ll d[2001]){
	fill(d, d+n, INF);
	d[s]=0;
	FibonacciHeap<ll, int> que;
	using node=FibonacciHeap<ll, int>::Node;
	vector<node*> v(n);
	v[s]=que.push(0, s);
	while(!que.empty()){
		auto p1=que.pop();
		int x=p1.second;
		for(int j=0; j<g[x].size(); j++){
			P q1=g[x][j];
			int y=q1.second;
			if(d[y]>d[x]+q1.first){
				d[y]=d[x]+q1.first;
				if(!v[y]) v[y]=que.push(d[y], y);
				else que.decrease(v[y], d[y]);
			}
		}
	}
}
int main()
{
	ll t;
	cin>>n>>m>>p>>q>>t; p--; q--;
	for(int i=0; i<n; i++) g[i].clear();
	for(int i=0; i<m; i++){
		int a, b; ll c; cin>>a>>b>>c; a--; b--;
		g[a].push_back(P(c, b));
		g[b].push_back(P(c, a));
	}
	ll d1[2001], dp[2001], dq[2001];
	dijkstra(0, d1);
	dijkstra(p, dp);
	dijkstra(q, dq);
	if(t<max(2*d1[p], 2*d1[q])){
		cout<<-1<<endl;
		return 0;
	}else if(t>=d1[p]+dp[q]+d1[q]){
		cout<<t<<endl;
		return 0;
	}
	ll ans=0;
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			ll dm=max(dp[i]+dp[j], dq[i]+dq[j]);
			if(dm+d1[i]+d1[j]>t) continue;
			ans=max(ans, t-dm);
		}
	}
	cout<<ans<<endl;
	return 0;
}
0