結果

問題 No.2387 Yokan Factory
ユーザー yurina256yurina256
提出日時 2023-07-21 22:34:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 481 ms / 5,000 ms
コード長 8,787 bytes
コンパイル時間 2,123 ms
コンパイル使用メモリ 191,148 KB
実行使用メモリ 13,732 KB
最終ジャッジ日時 2023-10-21 22:32:57
合計ジャッジ時間 6,597 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 262 ms
12,216 KB
testcase_16 AC 146 ms
11,980 KB
testcase_17 AC 422 ms
13,732 KB
testcase_18 AC 405 ms
11,600 KB
testcase_19 AC 192 ms
9,968 KB
testcase_20 AC 117 ms
8,556 KB
testcase_21 AC 351 ms
10,864 KB
testcase_22 AC 109 ms
8,156 KB
testcase_23 AC 481 ms
9,908 KB
testcase_24 AC 153 ms
8,824 KB
testcase_25 AC 144 ms
6,740 KB
testcase_26 AC 210 ms
9,556 KB
testcase_27 AC 155 ms
9,884 KB
testcase_28 AC 3 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 3 ms
4,348 KB
testcase_31 AC 3 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 3 ms
4,348 KB
testcase_34 AC 4 ms
4,348 KB
testcase_35 AC 5 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std; 
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define length size()
#define int long long
#define ll long long
constexpr ll inf = 1001001001001001001ll;
constexpr ll mod = 1000000007;
constexpr double pi = 3.141592653589793;
//小数出力
//cout << std::setprecision(12);
//imos
void to_ruiseki(vector<int> &vec){
    for(int i=1;i<vec.size();i++){
        vec[i] += vec[i-1];
    }
}
void to_2druiseki(vector<vector<int>> &vec){
    rep(i,vec.size()){
        rep(j,vec[0].size()){
            int s = 0;
            if(i!=0) s += vec[i-1][j];
            if(j!=0) s += vec[i][j-1];
            if(i!=0 && j!=0) s -= vec[i-1][j-1];
            s += vec[i][j];
            vec[i][j] = s;
        }
    }
}
//modpow(a,n,mod)
int modpow(int a, int n, int mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
//素因数分解
vector<pair<int,int>> p_fact(int N) {
    vector<pair<int,int>> res;
    for (int a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        int ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0) {
            ++ex;
            N /= a;
        }

        // その結果を push
        res.push_back({a, ex});
    }

    // 最後に残った数について
    if (N != 1) res.push_back({N, 1});
    return res;
}
//ctoi
int ctoi(const char c){
  switch(c){
    case '0': return 0;
    case '1': return 1;
    case '2': return 2;
    case '3': return 3;
    case '4': return 4;
    case '5': return 5;
    case '6': return 6;
    case '7': return 7;
    case '8': return 8;
    case '9': return 9;
    default :  throw runtime_error("hoge");
  }
}
//max
template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
//min
template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
//print
void print() { cout << '\n'; }
template<typename T>
void print(const T &t) { cout << t << '\n'; }
template<typename Head, typename... Tail>
void print(const Head &head, const Tail &... tail) {
    cout << head << ' ';
    print(tail...);
}
//join
template<typename T> string join(vector<T> &vec ,const string &sp){
    int si = vec.length;
    if(si==0){
        return "";
    }else{
        stringstream ss;
        rep(i,si-1){
            ss << vec[i] << sp;
        }
        ss << vec[si - 1];
        return ss.str();
    }
}
//木の直径
int tree_diameter(vector<vector<int>> &vec){
    //0-indexed
    queue<pair<int,int>> que;
    vector<bool> is_searched(vec.size(),false);
    que.push(make_pair(0,0));
    int max_d = 0;
    int max_e = 0;
    while(!que.empty()){
        int e = que.front().first;
        int distance = que.front().second;
        is_searched[e] = true;
        if(max_d<distance){
            max_d = distance;
            max_e = e;
        }
        que.pop();
        rep(i,vec[e].size()){
            if(!is_searched[vec[e][i]]){
                que.push( make_pair( vec[e][i], distance + 1 ) );
            }
        }
    }
    fill(is_searched.begin(),is_searched.end(),false);
    que.push(make_pair(max_e,0));
    max_d = 0;
    max_e = 0;
    
    while(!que.empty()){
        int e = que.front().first;
        int distance = que.front().second;
        is_searched[e] = true;
        if(max_d<distance){
            max_d = distance;
            max_e = e;
        }
        que.pop();
        rep(i,vec[e].size()){
            if(!is_searched[vec[e][i]]){
                que.push( make_pair( vec[e][i], distance + 1 ) );
            }
        }
    }
    return max_d;
}
//uf 根:root(x) 判定:issame(x,y) 結合:unite(x,y) 大きさ:size(x)
int getlongpath(vector<vector<int>> &vec,int st){
    queue<pair<int,int>> que;
    vector<bool> is_searched(vec.size(),false);
    is_searched[st] = true;
    que.push(make_pair(st,0));
    int max_d = 0;
    int max_e = 0;
    while(!que.empty()){
        int e = que.front().first;
        int distance = que.front().second;
        if(max_d<distance){
            max_d = distance;
            max_e = e;
        }
        que.pop();
        rep(i,vec[e].size()){
            if(!is_searched[vec[e][i]]){
                que.push( make_pair( vec[e][i], distance + 1 ) );
                is_searched[vec[e][i]] = true;
            }
        }
    }
    fill(is_searched.begin(),is_searched.end(),false);
    que.push(make_pair(max_e,0));
    return max_d;
}
struct UnionFind {
    vector<int> par, rank, siz;

    // 構造体の初期化
    UnionFind(int n) : par(n,-1), rank(n,0), siz(n,1) { }

    // 根を求める
    int root(int x) {
        if (par[x]==-1) return x; // x が根の場合は x を返す
        else return par[x] = root(par[x]); // 経路圧縮
    }

    // x と y が同じグループに属するか (= 根が一致するか)
    bool issame(int x, int y) {
        return root(x)==root(y);
    }

    // x を含むグループと y を含むグループを併合する
    bool unite(int x, int y) {
        int rx = root(x), ry = root(y); // x 側と y 側の根を取得する
        if (rx==ry) return false; // すでに同じグループのときは何もしない
        // union by rank
        if (rank[rx]<rank[ry]) swap(rx, ry); // ry 側の rank が小さくなるようにする
        par[ry] = rx; // ry を rx の子とする
        if (rank[rx]==rank[ry]) rank[rx]++; // rx 側の rank を調整する
        siz[rx] += siz[ry]; // rx 側の siz を調整する
        return true;
    }

    // x を含む根付き木のサイズを求める
    int size(int x) {
        return siz[root(x)];
    }
};
    int is_searched[16][16];
    int is_searched_now[16][16];
    char vec[16][16];
    int max_ans = -1;
    int h,w;
void saiki(int x,int y,int s){
        is_searched_now[x][y] = true;
        // << x << " " << y << " " << s << endl;
        if(x>0){
            if(!is_searched_now[x-1][y] && vec[x-1][y] != '#'){
                saiki(x-1,y,s+1);
            }
            if(vec[x-1][y] == 'g' && s >=2) chmax(max_ans,s+1);
        }
        if(x<h-1){
            if(!is_searched_now[x+1][y] && vec[x+1][y] != '#'){
                saiki(x+1,y,s+1);
            }
            if(vec[x+1][y] == 'g' && s >=2) chmax(max_ans,s+1);
        }
        if(y>0){
            if(!is_searched_now[x][y-1] && vec[x][y-1] != '#'){
                saiki(x,y-1,s+1);
            }
            if(vec[x][y-1] == 'g' && s >=2) chmax(max_ans,s+1);
        }
        if(y<w-1){
            if(!is_searched_now[x][y+1] && vec[x][y+1] != '#'){
                saiki(x,y+1,s+1);
            }
            if(vec[x][y+1] == 'g' && s >=2) chmax(max_ans,s+1);
        }
        //is_searched[x][y] = true;
        is_searched_now[x][y] = false;
    }
int n,m,x;
vector<vector<tuple<int,int,int>>> tree;
bool solve(int width){
    //print("solve:",width);
    priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> que;
    vector<bool> is_checked(n,false);
    que.push(make_pair(0,0));
    while(!que.empty()){
        int t = que.top().first;
        int e = que.top().second;
        que.pop();
        if(is_checked[e]) continue;
        is_checked[e] = true;
        if(e == n-1){
        //    print(width,"true");
            return true;    
        }
        //print(t,e);
        rep(i,tree[e].size()){
            int ln = get<0>(tree[e][i]);
            int lt = get<1>(tree[e][i]);
            int lw = get<2>(tree[e][i]);
            if(lw < width) continue;
            if(t + lt > x) continue;
           // print("push:",t+lt,ln);
            que.push(make_pair(t + lt,ln));
        }
    }
   // print(width,"false");
    return false;
}
signed main(void){
    cin >> n >> m >> x;
    tree.resize(n);
    rep(i,m){
        int u1,u2,a,b;
        cin >> u1 >> u2 >> a >> b;
        u1--;
        u2--;
        tree[u1].push_back(make_tuple(u2,a,b));
        tree[u2].push_back(make_tuple(u1,a,b));
    }
    
    //めぐる式(https://qiita.com/drken/items/97e37dd6143e33a64c8c)
    int left = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1
    int right = powl(10,9)+1; // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は a.size()

    /* どんな二分探索でもここの書き方を変えずにできる! */
    while (right - left > 1) {
        int mid = left + (right - left) / 2;
      //  print("bs:",left,right,mid);
        if (solve(mid)) left = mid;
        else right = mid;
    }

    /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている */
    print(left);
}
0