結果

問題 No.2387 Yokan Factory
ユーザー tofu_dra2tofu_dra2
提出日時 2023-09-23 10:00:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 703 ms / 5,000 ms
コード長 8,517 bytes
コンパイル時間 4,627 ms
コンパイル使用メモリ 276,676 KB
実行使用メモリ 14,048 KB
最終ジャッジ日時 2023-09-23 10:00:24
合計ジャッジ時間 12,575 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 300 ms
14,048 KB
testcase_16 AC 194 ms
13,200 KB
testcase_17 AC 437 ms
13,652 KB
testcase_18 AC 466 ms
11,956 KB
testcase_19 AC 427 ms
9,600 KB
testcase_20 AC 308 ms
8,784 KB
testcase_21 AC 667 ms
11,636 KB
testcase_22 AC 277 ms
8,352 KB
testcase_23 AC 472 ms
9,740 KB
testcase_24 AC 328 ms
8,804 KB
testcase_25 AC 272 ms
7,092 KB
testcase_26 AC 560 ms
10,876 KB
testcase_27 AC 703 ms
12,288 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 5 ms
4,376 KB
testcase_30 AC 5 ms
4,376 KB
testcase_31 AC 4 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 5 ms
4,376 KB
testcase_35 AC 4 ms
4,376 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

typedef long long int ll;
typedef long double ld;

using namespace std;
using namespace atcoder;

#define inf 1010000000
#define llinf 1001000000000000000ll
#define pi 3.141592653589793238
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define rep1(i, n) for(ll i = 1; i <= (n); i++)
#define rep2(i,l,r) for(ll i = (l); i < (r); i++)
#define per(i, n) for(ll i = (n)-1; i >= 0; i--)
#define each(x, v) for (auto&& x : v)
#define rng(a) a.begin(),a.end()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define pob pop_back
#define st string
#define pcnt __builtin_popcountll
#define bit(n) (1LL<<(n))

template <class T = ll>
inline T in(){ T x; cin >> x; return (x);}
#define vcin(x,n) {for(ll loop=0; loop<(n); loop++) cin>>x[loop];}

#define dame { puts("-1"); return 0;}
#define yes { puts("Yes"); return 0;}
#define no { puts("No"); return 0;}
#define ret(x) { cout<<(x)<<endl;}
#define rets(x) { cout<<(x)<< " ";}
#define Endl cout<<endl;
#define dump(x) { cout << #x << " = " << (x) << endl;}

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false;}

// 仮マクロ 便利だったら昇格
#define unique(v) v.erase( unique(v.begin(), v.end()), v.end())
// ここまで仮マクロ

// clock()/CLOCKS_PER_SEC 秒数を知りたいときに用いる

#define mod 998244353
using mint = modint998244353;

/*

#define mod 1000000007
using mint = modint1000000007;

*/

vector<ll> dx={1,0,-1,0};
vector<ll> dy={0,1,0,-1};

using pl = pair<ll,ll>;
using ppl = pair<pl,ll>;
// G.assign(n, vector<ll>()); グローバル変数にGを置く時に置く

// 関数を置くのはここ以下

template <typename T>
struct edge {
  int src, to;
  T cost;

  edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {}
  edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnweightedGraph = vector<vector<int>>;

// Input of (Unweighted) Graph
UnweightedGraph graph(int N, int M = -1, bool is_directed = false,
                      bool is_1origin = true) {
  UnweightedGraph g(N);
  if (M == -1) M = N - 1;
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    if (is_1origin) x--, y--;
    g[x].push_back(y);
    if (!is_directed) g[y].push_back(x);
  }
  return g;
}

// Input of Weighted Graph
template <typename T>
WeightedGraph<T> wgraph(int N, int M = -1, bool is_directed = false,
                        bool is_1origin = true) {
  WeightedGraph<T> g(N);
  if (M == -1) M = N - 1;
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    T c;
    cin >> c;
    if (is_1origin) x--, y--;
    g[x].emplace_back(x, y, c);
    if (!is_directed) g[y].emplace_back(y, x, c);
  }
  return g;
}

// Input of Edges
template <typename T>
Edges<T> esgraph(int N, int M, int is_weighted = true, bool is_1origin = true) {
  Edges<T> es;
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    T c;
    if (is_weighted)
      cin >> c;
    else
      c = 1;
    if (is_1origin) x--, y--;
    es.emplace_back(x, y, c);
  }
  return es;
}

// Input of Adjacency Matrix
template <typename T>
vector<vector<T>> adjgraph(int N, int M, T INF, int is_weighted = true,
                           bool is_directed = false, bool is_1origin = true) {
  vector<vector<T>> d(N, vector<T>(N, INF));
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    T c;
    if (is_weighted)
      cin >> c;
    else
      c = 1;
    if (is_1origin) x--, y--;
    d[x][y] = c;
    if (!is_directed) d[y][x] = c;
  }
  return d;
}

// 一般のグラフのstからの距離!!!!
// unvisited nodes : d = -1
vector<int> Depth(const UnweightedGraph &g, int start = 0) {
  int n = g.size();
  vector<int> ds(n, -1);
  ds[start] = 0;
  queue<int> q;
  q.push(start);
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    int dc = ds[c];
    for (auto &d : g[c]) {
      if (ds[d] == -1) {
        ds[d] = dc + 1;
        q.push(d);
      }
    }
  }
  return ds;
}

// Depth of Rooted Weighted Tree
// unvisited nodes : d = -1
template <typename T>
vector<T> Depth(const WeightedGraph<T> &g, int start = 0) {
  vector<T> d(g.size(), -1);
  auto dfs = [&](auto rec, int cur, T val, int par = -1) -> void {
    d[cur] = val;
    for (auto &dst : g[cur]) {
      if (dst == par) continue;
      rec(rec, dst, val + dst.cost, cur);
    }
  };
  dfs(dfs, start, 0);
  return d;
}

// Diameter of Tree
// return value : { {u, v}, length }
pair<pair<int, int>, int> Diameter(const UnweightedGraph &g) {
  auto d = Depth(g, 0);
  int u = max_element(begin(d), end(d)) - begin(d);
  d = Depth(g, u);
  int v = max_element(begin(d), end(d)) - begin(d);
  return make_pair(make_pair(u, v), d[v]);
}

// Diameter of Weighted Tree
// return value : { {u, v}, length }
template <typename T>
pair<pair<int, int>, T> Diameter(const WeightedGraph<T> &g) {
  auto d = Depth(g, 0);
  int u = max_element(begin(d), end(d)) - begin(d);
  d = Depth(g, u);
  int v = max_element(begin(d), end(d)) - begin(d);
  return make_pair(make_pair(u, v), d[v]);
}

// nodes on the path u-v ( O(N) )
template <typename G>
vector<int> Path(G &g, int u, int v) {
  vector<int> ret;
  int end = 0;
  auto dfs = [&](auto rec, int cur, int par = -1) -> void {
    ret.push_back(cur);
    if (cur == v) {
      end = 1;
      return;
    }
    for (int dst : g[cur]) {
      if (dst == par) continue;
      rec(rec, dst, cur);
      if (end) return;
    }
    if (end) return;
    ret.pop_back();
  };
  dfs(dfs, u);
  return ret;
}

template <typename T>
vector<T> dijkstra(WeightedGraph<T> &g, int start = 0) {
  using P = pair<T, int>;
  int N = (int)g.size();
  vector<T> d(N, T(-1));
  priority_queue<P, vector<P>, greater<P> > Q;
  d[start] = 0;
  Q.emplace(0, start);
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    int cur = p.second;
    if (d[cur] < p.first) continue;
    for (auto dst : g[cur]) {
      if (d[dst] == T(-1) || d[cur] + dst.cost < d[dst]) {
        d[dst] = d[cur] + dst.cost;
        Q.emplace(d[dst], dst);
      }
    }
  }
  return d;
}

// Nyaanさんのグラフ構造体

// edge<T>	型Tの重みが付いた辺
// Edges<T>	型Tの重みが付いた辺のリスト
// WeightedGraph<T>	型Tの重みが付いた(有向/無向)グラフ
// UnweightedGraph	重みなし(有向/無向)グラフ

// graph(N, M(-1の時N-1), bool is_directed = false, bool is_1origin = true)
//   重みなしのグラフを標準入力から読み込み、UnweightedGraph型で返す。
//   ex) graph(n,,,) であればN頂点の無向木
// wgraph<T>(,,,) = 重みつきグラフ
// esgraph,adjgraph = 隣接リスト,隣接行列を返す

// - 機能 - ( だいたいO(N) )
// vector<int> Depth(UnweightedGraph g, int s = 0)
//   sからの最短距離
// vector<T> Depth(weightedGraph<T> g, int s = 0)
//   (重み付き根つき木(無向でも可だが遅くなる)) sからの距離
// pair<pair<int, int>, int> Diameter(UnweightedGraph g)
// pair<pair<int, int>, T> Diameter<T>(WeightedGraph<T> g)
//   木の直径の両端,長さを返す
// vector<int> Path<UnweightedGraph>(G &g, int u, int v)
// vector<int> Path<WeightedGraph>(G &g, int u, int v)
//   (両端含む)パスの頂点列を返す
// vector<T> dijkstra(g, s = 0) 
//   ダイクストラ法 (O(ElogV))
//   計算量:O(ElogV)
//   届かなかったら-1

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  ll n,m,x; cin >> n >> m >> x;
  vector<pair<pl,pl>> v;
  rep(i,m){
    ll a,b,c,d; cin >> a >> b >> c >> d; a--;b--;
    pair<pl,pl> p;
    p.fi = {a,b};
    p.se = {c,d};
    v.eb(p);
  }
  ll ng = llinf;
  ll ok = 0; //okは必ずtrue, ngは必ずfalse

  auto isOK = [&](ll mid){
    WeightedGraph<ll> g(n);
    rep(i,m){
      if(v[i].se.se>=mid){
        g[v[i].fi.fi].eb(v[i].fi.se,v[i].se.fi);
        g[v[i].fi.se].eb(v[i].fi.fi,v[i].se.fi);
      }
    }
    ll t = dijkstra(g)[n-1];
    if(t!=-1 && t<=x) return true;
    return false;
  };

  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;

    if (isOK(mid)) ok = mid;
    else ng = mid;
  }
  if(ok==0) dame;
  ret(ok);

}
0