結果

問題 No.614 壊れたキャンパス
ユーザー pppppp
提出日時 2017-12-16 02:45:05
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 5,498 bytes
コンパイル時間 1,040 ms
コンパイル使用メモリ 100,620 KB
実行使用メモリ 816,256 KB
最終ジャッジ日時 2024-05-08 12:48:40
合計ジャッジ時間 4,367 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <fstream>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <random>

using namespace std;

#define INT_MAX_VALUE 2147483647
#define LONG_LONG_MAX_VALUE 9223372036854775807

template <class T>
T mymax(T a,T b){
    if(a>=b) return a;
    return b;
}
template <class T>
T mymin(T a,T b){
    if(a<=b) return a;
    return b;
}
//
//long long gcd(long long a, long long b){
//    if(a<b){
//        swap(a,b);
//    }
//    while(b){
//        long long r = a%b;
//        a=b;
//        b=r;
//    }
//    return a;
//}
//
//long long lcm(long long a, long long b){
//    return (a*b)/gcd(a,b);
//}
//
//long long isPrim(long long a){
//    if(a==1){
//        return a;
//    }
//    for(int i=2;i*i<=a;i++){
//        if(a%i==0){
//            return i;
//        }
//    }
//    return a;
//}
//
struct XX{
    int x;
    int y;
    int z;
    int iy;
};

class xxGreater {
public:
    bool operator()(const XX& riLeft, const XX& riRight) const {
        //第2条件
        if((riLeft.x) == (riRight.x)){
            return riLeft.y < riRight.y;//<:昇順(小さいものから順番)、>:降順(大きいものから順番)
            //プライオリティキューの場合は > で、top()すると値の小さいものがとれる
        }
        //第1条件
        return (riLeft.x) < (riRight.x);
    }
};


//
////kruskal
//struct edge{
//    int u;
//    int v;
//    int cost;
//};
//
//bool comp(edge& e1,edge& e2){
//    return e1.cost < e2.cost;
//}
//
//edge es[1000];
//
//long long kruskal(int V,int E){//V:頂点数,E:辺数
//    sort(es,es+E,comp);
//    init(V);
//    long long res = 0;
//    for(int i=0;i<E;i++){
//        edge e = es[i];
//        if(!same(e.u,e.v)){
//            unite(e.u,e.v);
//            res+=e.cost;
//        }
//    }
//    return res;
//}

//map<long long,long long> prime_f(long long n){
//    map<long long,long long>res;
//    for(int i=2;i*i<=n;i++){
//        while(n%i==0){
//            ++res[i];
//            n/=i;
//        }
//    }
//    if(n!=1)res[n]=1;
//    return res;
//}

int ppar[100000]; //親
int rrank[100000];//木の深さ

void init(int n){
    for(int i=1;i<=n;i++){
        ppar[i]=i;
        rrank[i]=0;
    }
}

int find(int x){
    if(ppar[x]==x){
        return x;
    }else{
        return ppar[x]=find(ppar[x]);
    }
}

void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y){
        return;
    }
    if(rrank[x]<rrank[y]){
        ppar[x]=y;
    }else{
        ppar[y]=x;
        if(rrank[x]==rrank[y]){
            rrank[x]++;
        }
    }
}
bool same(int x,int y){
    return find(x)==find(y);
}

int comb(int n, int r){
    int ret=1;
    for(int i=0;i<r;i++){
        ret*=(n-i);
    }
    int bunbo=1;
    for(int i=1;i<=r;i++){
        bunbo*=i;
    }
    return ret/bunbo;
}

vector<int>ll[200005];
vector<int>rr[200005];
#define NNN 100000000
long long dp[2][NNN];

int main(int argc, const char * argv[])
{
    //std::ios::sync_with_stdio(false);
    //scanf("%s",S);
    //scanf("%d",&N);
    //sscanf(tmp.c_str(),"%dd%d%d",&time[i], &dice[i], &z[i]);
    //getline(cin, target);
    //cin >> x >> y;
    //テスト用
    //ifstream ifs( "1_06.txt" );
    //ifs >> a;
    //ここから
    
    //入力高速化
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int N,M,K,S,T;
    cin >> N >> M >> K >> S >> T;
    XX a[200005];
    for(int i=0;i<M;i++){
        int tmp1,tmp2,tmp3;
        cin >> tmp1 >> tmp2 >> tmp3;
        a[i].x=tmp1-1;
        a[i].y=tmp2-1;
        a[i].z=tmp3-1;
    }
    sort(a,a+M,xxGreater());
    
    for(int i=0;i<M;i++){
        ll[a[i].x].push_back(a[i].y);
        rr[a[i].x].push_back(a[i].z);
    }
    if(M==0){
        cout << abs(S-T) << endl;
        return 0;
    }
    
    for(int i=0;i<NNN;i++){
        dp[0][i]=INT_MAX_VALUE;
        dp[1][i]=INT_MAX_VALUE;
    }
    for(int i=0;i<ll[0].size();i++){
        dp[0][rr[0][i]]=abs((S-1)-ll[0][i]);
    }
    int first=0;
    int second=1;
    for(int i=1;i<N;i++){
        fill(dp[second],dp[second]+NNN,INT_MAX_VALUE);
        
        for(int j=0;j<ll[i].size();j++){
            sort(rr[i-1].begin(),rr[i-1].end());
            auto it=lower_bound(rr[i-1].begin(),rr[i-1].end(),ll[i][j]);
            if(it==rr[i-1].end()){
                it--;
                dp[second][rr[i][j]]=min(dp[second][rr[i][j]],
                                         dp[first][*it]+abs((*it-ll[i][j])));
                continue;
            }
            dp[second][rr[i][j]]=min(dp[second][rr[i][j]],
                                     dp[first][*it]+abs((*it-ll[i][j])));
            if(it==rr[i-1].begin()) continue;
            it--;
            dp[second][rr[i][j]]=min(dp[second][rr[i][j]],
                                     dp[first][*it]+abs((*it-ll[i][j])));
        }
        swap(first,second);
    }
    long long ans=INT_MAX_VALUE;
    for(int i=0;i<K;i++){
        ans=min(ans,abs((T-1)-i)+dp[second][i]);
    }
    if(ans!=INT_MAX_VALUE){
        cout << ans << endl;
    }else{
        cout << -1 << endl;
    }
    
    //ここまで
    //cout << "debug" << endl;
    //cout << "ans" << endl;改行含む
    //printf("%.0f\n",ans);//小数点以下表示なし
    //printf("%.7f\n",p);
    //printf("%f\n",pow(2,ans.size()));
    
    return 0;
}

0