結果

問題 No.848 なかよし旅行
ユーザー 👑 tute7627tute7627
提出日時 2019-07-05 23:20:44
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,252 bytes
コンパイル時間 1,752 ms
コンパイル使用メモリ 177,796 KB
実行使用メモリ 19,976 KB
最終ジャッジ日時 2024-04-16 08:07:05
合計ジャッジ時間 6,662 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
19,976 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 WA -
testcase_03 AC 2 ms
6,940 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
6,944 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 5 ms
6,944 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define ld long double
#define ll long long
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define MP make_pair
#define test cout<<"test"<<endl;
#define fi first
#define se second
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
const ll zero=0;
using P = pair<ll, ll>;
void chmin(ll &a,ll b){if(a>b)a=b;}
void chmax(ll &a,ll b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
void ans(bool x,ll y,ll z){if(x)cout<<y<<endl;else cout<<z<<endl;}
void ans(bool x,string y,string z){if(x)cout<<y<<endl;else cout<<z<<endl;}   
ll gcd(ll x,ll y){ll r;while((r=x%y)!=0){x=y;y=r;}return y;}

ll MAX_V = 3000; //頂点数
struct edge {
    ll to;
    ll cost;
};
//<最短距離, 頂点の番号>using P = pair<ll, ll>;
vector<vector<struct edge>> G(MAX_V); //隣接リスト
vector<ll> d(MAX_V); //各頂点への最短距離
void dijkstra(ll s) {
    priority_queue<P, vector<P>, greater<P> > que;
    fill(ALL(d), INF);
    d[s] = 0;
    que.push(P(0, s));

    while (!que.empty()) {
        P p = que.top();
        que.pop();
        ll v = p.second;
        if (d[v] < p.first) continue;

        for (ll i=0; i<G[v].size(); i++) {
            struct edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}

int main(){
  ll i,j,o;
  ll res=-1,res1=INF,res2=-INF,buf=0,buf1=0,buf2=0,buf3=0,buf4=0,sum=0;
  bool judge = true;
  ll n,m,p,q,t;cin>>n>>m>>p>>q>>t;
  vector<ll> u(m),v(m),a(m);
  for(i=0;i<m;i++){
    cin>>u[i]>>v[i]>>a[i];
    struct edge e ={v[i],a[i]};
    G[u[i]].push_back(e);
    e ={u[i],a[i]};
    G[v[i]].push_back(e);
  }
  vector<ll>dp(n+1),dq(n+1);
  dijkstra(p);
  for(i=1;i<=n;i++)dp[i]=d[i];
  dijkstra(q);
  for(i=1;i<=n;i++)dq[i]=d[i];
  if(dp[1]>dq[1]){
    queue<ll> r;
    r.push(1);
    while(!r.empty()){
      buf=r.front();
      chmax(res,t-max(2*dp[1],
      dp[1]-dp[buf]+dq[buf]+dq[1])+dp[1]-dp[buf]);
      if(dq[buf]<=dp[buf])
      chmax(res,t-2*dq[1]+2*(dp[1]-dp[buf]));
      for(i=0;i<G[buf].size();i++){
        struct edge e=G[buf][i];
        if(dp[buf]-dp[e.to]==e.cost)
          r.push(e.to);
      }
      r.pop();
    }
  }
  else{
    queue<ll> r;
    r.push(1);
    while(!r.empty()){
      buf=r.front();
      //cout<<buf spa res spa
      //2*dq[1] spa dq[1]-dq[buf]+dp[buf]+dp[1]<<endl;
      chmax(res,t-max(2*dq[1],
      dq[1]-dq[buf]+dp[buf]+dp[1])+dq[1]-dq[buf]);
      if(dp[buf]<=dq[buf])
      chmax(res,t-2*dq[1]+2*(dq[1]-dq[buf]));
      for(i=0;i<G[buf].size();i++){
        struct edge e=G[buf][i];
        if(dq[buf]-dq[e.to]==e.cost)
          r.push(e.to);
      }
      r.pop();
    }
  }
  if(dp[1]+dq[1]+dp[q]<=t)cout<<t<<endl;
  else if(2*dp[1]<=t&&2*dq[1]<=t)cout<<res<<endl;
  else cout<<-1<<endl;
  return 0;
}
0