結果

問題 No.848 なかよし旅行
ユーザー snow39snow39
提出日時 2019-07-06 00:47:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 1,411 bytes
コンパイル時間 906 ms
コンパイル使用メモリ 102,248 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2023-08-07 19:46:40
合計ジャッジ時間 3,140 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
10,752 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 32 ms
5,280 KB
testcase_12 AC 42 ms
5,612 KB
testcase_13 AC 56 ms
6,520 KB
testcase_14 AC 18 ms
4,376 KB
testcase_15 AC 53 ms
6,492 KB
testcase_16 AC 90 ms
8,228 KB
testcase_17 AC 64 ms
7,196 KB
testcase_18 AC 31 ms
5,072 KB
testcase_19 AC 27 ms
4,912 KB
testcase_20 AC 11 ms
4,376 KB
testcase_21 AC 78 ms
7,284 KB
testcase_22 AC 82 ms
7,616 KB
testcase_23 AC 10 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 98 ms
8,088 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#define MOD 1000000007
#define mkp make_pair
typedef long long ll;
using namespace std;

int N,M,P,Q,T;

vector<pair<int,ll>> g[2020];

ll dist[3][2020];

const ll INF=1e18;

void dijkstra(int id,int start){
  for(int i=1;i<=N;i++) dist[id][i]=INF;

  dist[id][start]=0;
  priority_queue<pair<ll,int>> PQ;
  PQ.push(mkp(0,start));
  while(!PQ.empty()){
    ll co,tp;
    tie(co,tp)=PQ.top();
    PQ.pop();

    co*=(-1);
    if(dist[id][tp]<co) continue;

    for(int i=0;i<g[tp].size();i++){
      int nex=g[tp][i].first;
      ll neco=g[tp][i].second;
      if(co+neco<dist[id][nex]){
        dist[id][nex]=co+neco;
        PQ.push(mkp(-dist[id][nex],nex));
      }
    }
  }
}

int main(){
  cin>>N>>M>>P>>Q>>T;
  for(int i=0;i<M;i++){
    int a,b,c;
    cin>>a>>b>>c;
    g[a].push_back(mkp(b,c));
    g[b].push_back(mkp(a,c));
  }

  dijkstra(0,1);
  dijkstra(1,P);
  dijkstra(2,Q);

  if(dist[0][P]+dist[1][Q]+dist[0][Q]<=T){
    cout<<T<<endl;
    return 0;
  }

  ll ans=-1;
  for(int i=1;i<=N;i++){
    for(int j=1;j<=N;j++){
      ll p=dist[1][i]+dist[1][j];
      ll q=dist[2][i]+dist[2][j];
      ll r=dist[0][i]+dist[0][j];

      if(max(p,q)+r<=T){
        ans=max(ans,T-max(p,q));
      }
    }
  }

  cout<<ans<<endl;



  return 0;
}
0