結果

問題 No.5007 Steiner Space Travel
ユーザー rhoorhoo
提出日時 2022-07-30 15:08:20
言語 Rust
(1.72.1)
結果
AC  
実行時間 982 ms / 1,000 ms
コード長 5,666 bytes
コンパイル時間 829 ms
実行使用メモリ 2,936 KB
スコア 5,907,028
最終ジャッジ日時 2022-07-30 15:08:53
合計ジャッジ時間 32,993 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
純コード判定しない問題か言語
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 981 ms
2,736 KB
testcase_01 AC 982 ms
2,756 KB
testcase_02 AC 981 ms
2,832 KB
testcase_03 AC 982 ms
2,936 KB
testcase_04 AC 981 ms
2,828 KB
testcase_05 AC 981 ms
2,724 KB
testcase_06 AC 982 ms
2,820 KB
testcase_07 AC 981 ms
2,692 KB
testcase_08 AC 982 ms
2,840 KB
testcase_09 AC 980 ms
2,724 KB
testcase_10 AC 982 ms
2,724 KB
testcase_11 AC 982 ms
2,628 KB
testcase_12 AC 982 ms
2,836 KB
testcase_13 AC 981 ms
2,756 KB
testcase_14 AC 982 ms
2,844 KB
testcase_15 AC 982 ms
2,836 KB
testcase_16 AC 981 ms
2,704 KB
testcase_17 AC 982 ms
2,788 KB
testcase_18 AC 981 ms
2,700 KB
testcase_19 AC 981 ms
2,648 KB
testcase_20 AC 981 ms
2,732 KB
testcase_21 AC 982 ms
2,756 KB
testcase_22 AC 982 ms
2,848 KB
testcase_23 AC 981 ms
2,868 KB
testcase_24 AC 981 ms
2,760 KB
testcase_25 AC 981 ms
2,824 KB
testcase_26 AC 982 ms
2,848 KB
testcase_27 AC 981 ms
2,756 KB
testcase_28 AC 981 ms
2,824 KB
testcase_29 AC 981 ms
2,936 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `p`
   --> Main.rs:261:9
    |
261 |     let p=up as f64/iter as f64;
    |         ^ help: if this is intentional, prefix it with an underscore: `_p`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted

ソースコード

diff #

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, Chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, Usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}



#[allow(unused)]
#[inline]
fn get_time()->f64{
    static mut START:f64=-1.;
    let t=std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs_f64();
    unsafe{
        if START<0.{START=t;}
        t-START
    }
}


#[cfg(local)]
#[allow(unused)]
macro_rules! debug{
    ()=>{
        eprintln!();
    };
    ($x:literal)=>{
        eprintln!("{:?}",$x);
    };
    ($x:expr)=>{
        eprintln!("{}: {:?}",stringify!($x),$x);
    };
    ($x:literal,$($l:expr),*)=>{
        eprint!("{:?}, ",$x);
        debug!($($l),*);
    };
    ($x:expr,$($l:expr),*)=>{
        eprint!("{}: {:?}, ",stringify!($x),$x);
        debug!($($l),*);
    }
}
#[cfg(not(local))]
#[allow(unused)]
macro_rules! debug{
    ($($_:expr),*)=>{}
}

#[allow(unused)]
mod rnd {
    static mut S:usize=88172645463325252;
    #[inline]
    pub fn next()->usize{
        unsafe{
            S^=S<<7;
            S^=S>>9;
            S
        }
    }
    
    #[inline]
    pub fn nextf()->f64{
        unsafe{
            std::mem::transmute::<_,f64>(0x3ff0000000000000|next()&0xfffffffffffff)-1.
        }
    }

    // [a,b)
    #[inline]
    pub fn range(a:usize,b:usize)->usize{
        next()%(b-a)+a
    }

    #[inline]
    pub fn shuffle<T>(list:&mut [T]){
        for i in (0..list.len()).rev(){
            list.swap(i,next()%(i+1));
        }
    }

    #[inline]
    pub fn exu(a:usize,b:usize,skip:usize)->usize{
        let ret=range(a,b-1);
        if ret==skip{b-1}
        else{ret}
    }
}

const N:usize=100;
const M:usize=8;
const A:usize=5;


struct In{
    pos:[(usize,usize);N]
}
impl In{
    #[inline]
    fn input()->In{
        input!{
            n:usize,
            m:usize,
            tp:[(usize,usize);n]
        }
        assert_eq!((n,m),(N,M));

        let mut it=tp.into_iter();
        let mut pos=[(!0,!0);N];
        pos.fill_with(||it.next().unwrap());

        In{pos}
    }
}


struct Out{
    route:[usize;N+1],
    dist:[[i64;N];N]
}
impl Out{
    #[inline]
    fn new(input:&In)->Out{
        let mut route=[!0;N+1];
        let mut it=0..;
        route.fill_with(||it.next().unwrap());
        route[N]=0;
        rnd::shuffle(&mut route[1..N]);

        let mut dist=[[-1;N];N];

        const TA:usize=A.pow(2);
        for i in 0..N{
            let (a,b)=input.pos[i];
            for j in 0..N{
                let (c,d)=input.pos[j];
                dist[i][j]=(TA*((a-c)*(a-c)+(b-d)*(b-d))) as i64;
            }
        }

        Out{route,dist}
    }

    #[inline]
    fn score(&self)->i64{
        let mut ret=0;
        for w in self.route.windows(2){
            ret+=self.dist[w[0]][w[1]];
        }

        ret
    }

    #[inline]
    fn try_opt2(&self,a:usize,b:usize)->i64{
        // assert!(1<=a.min(b) && a.min(b)+1<a.max(b) && a.max(b)<self.route.len()-1);
        let a0=self.route[a-1];
        let a1=self.route[a];
        let b0=self.route[b-1];
        let b1=self.route[b];

        let old=self.dist[a0][a1]+self.dist[b0][b1];
        let new=self.dist[a0][b0]+self.dist[a1][b1];

        new-old
    }

    #[inline]
    fn opt2(&mut self,mut a:usize,mut b:usize){
        if a>b{
            std::mem::swap(&mut a,&mut b);
        }
        self.route[a..b].reverse();
    }
}


fn sa(input:&In)->Out{
    let mut cur=Out::new(input);
    let mut score=cur.score();
    let mut best=score;

    let mut iter=0;
    let mut up=0;
    let mut th=[0;256];

    loop{
        if iter&1023==0{
            const TL:f64=0.98;
            let p=get_time()/TL;
            if p>=1.{
                break;
            }

            const T0:f64=1000000.;
            const T1:f64=0.;

            let heat=T0+(T1-T0)*p;

            th.fill_with(||(-heat*rnd::nextf().ln()) as i64);
        }
        iter+=1;

        let a=rnd::range(1,N);
        let mut b;
        while{
            b=rnd::range(1,N);
            a-b+1<=2
        }{}
        let diff=cur.try_opt2(a,b);
        
        if th[iter&255]>=diff{
            score+=diff;
            cur.opt2(a,b);
            up+=1;
            best=best.min(score);
        }
    }

    let p=up as f64/iter as f64;
    debug!(iter,p);
    debug!(score,best);
    assert_eq!(score,cur.score());

    cur
}


fn main(){
    let input=In::input();
    let out=sa(&input);

    // まずは何も作らない
    for _ in 0..M{
        println!("0 0");
    }

    println!("{}",out.route.len());
    for &i in out.route.iter(){
        println!("1 {}",i+1);
    }
}
0