CODE FESTIVAL 2016 Grand Final

Submission #1205634

Source codeソースコード

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok().unwrap();
    ret
}
fn get_word() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

/**
 * Union-Find tree.
 * Verified by yukicoder No.94 (http://yukicoder.me/submissions/82111)
 */
struct UnionFind { disj: Vec<usize> }

impl UnionFind {
    fn new(n: usize) -> Self {
        let mut disj = vec![0; n];
        for i in 0 .. n {
            disj[i] = i;
        }
        UnionFind { disj: disj }
    }
    fn root(self: &mut Self, x: usize) -> usize {
        if x != self.disj[x] {
            let par = self.disj[x];
            let r = self.root(par);
            self.disj[x] = r;
        }
        return self.disj[x];
    }
    fn unite(self: &mut Self, x: usize, y: usize) {
        let xr = self.root(x);
        let yr = self.root(y);
        self.disj[xr] = yr;
    }
    fn is_same_set(self: &mut Self, x: usize, y: usize) -> bool {
        return self.root(x) == self.root(y);
    }
}

fn get_triple() -> (f64, f64, f64) {
    let x = get();
    let y = get();
    let a = get();
    (x, y, a)
}

fn cmp_f64(x: f64, y: f64) -> Ordering {
    if x < y { return Ordering::Less; }
    if x > y { return Ordering::Greater; }
    Ordering::Equal
}

fn max_f64(x: f64, y: f64) -> f64 {
    if x < y { y } else { x }
}
fn min_f64(x: f64, y: f64) -> f64 {
    if x > y { y } else { x }
}

fn conn_optimal(v: &[(f64, f64, f64)]) -> f64 {
    let n = v.len();
    let mut uf = UnionFind::new(n);
    let mut edges = Vec::new();
    for i in 0 .. n {
        for j in 0 .. i {
            let dist = ((v[i].0 - v[j].0).powi(2) + (v[i].1 - v[j].1).powi(2))
                .sqrt();
            edges.push((dist, (j, i)));
        }
    }
    edges.sort_by(|x, y| cmp_f64(x.0, y.0));
    let mut tot_a = 0.0;
    let mut tot_dist = 0.0; // Minimum Spanning Tree
    for i in 0 .. n {
        tot_a += v[i].2;
    }
    for (d, (u, v)) in edges {
        if !uf.is_same_set(u, v) {
            uf.unite(u, v);
            tot_dist += d;
        }
    }
    (tot_a - tot_dist) / n as f64
}

// This code is written after the author read the editorial.
fn solve() {
    let n = get();
    let xya: Vec<_> = (0 .. n).map(|_| get_triple()).collect();
    let mut dp = vec![1.0 / 0.0; 1 << n];
    let mut conn_opt = vec![-1.0 / 0.0; 1 << n];
    for bits in 1 .. 1 << n {
        let mut vert = Vec::new();
        for i in 0 .. n {
            if (bits & 1 << i) != 0 {
                vert.push(xya[i]);
            }
        }
        conn_opt[bits] = conn_optimal(&vert);
    }
    for bits in 1 .. 1 << n {
        let mut sub = bits;
        let mut ma = -1.0 / 0.0;
        loop {
            if sub > 0 {
                ma = max_f64(ma, min_f64(dp[bits - sub], conn_opt[sub]));
            }
            if sub == 0 { break; }
            sub = (sub - 1) & bits;
        }
        dp[bits] = ma;
    }
    println!("{}", dp[(1 << n) - 1]);
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

Submission

Task問題 E - Water Distribution
User nameユーザ名 koba@チーム戦の時は別垢を作れ!
Created time投稿日時
Language言語 Rust (1.15.1)
Status状態 AC
Score得点 1000
Source lengthソースコード長 3867 Byte
File nameファイル名
Exec time実行時間 70 ms
Memory usageメモリ使用量 8572 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - example0.txt,example1.txt
All 1000 / 1000 000.txt,001.txt,002.txt,003.txt,004.txt,005.txt,006.txt,007.txt,008.txt,009.txt,010.txt,011.txt,012.txt,013.txt,014.txt,015.txt,016.txt,017.txt,018.txt,019.txt,020.txt,021.txt,022.txt,023.txt,024.txt,025.txt,026.txt,027.txt,028.txt,029.txt,030.txt,031.txt,032.txt,033.txt,034.txt,035.txt,036.txt,037.txt,038.txt,039.txt,040.txt,041.txt,042.txt,043.txt,example0.txt,example1.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
000.txt AC 69 ms 8572 KB
001.txt AC 69 ms 8572 KB
002.txt AC 69 ms 8572 KB
003.txt AC 69 ms 8572 KB
004.txt AC 69 ms 8572 KB
005.txt AC 69 ms 8572 KB
006.txt AC 70 ms 8572 KB
007.txt AC 69 ms 8572 KB
008.txt AC 69 ms 8572 KB
009.txt AC 70 ms 8572 KB
010.txt AC 69 ms 8572 KB
011.txt AC 69 ms 8572 KB
012.txt AC 70 ms 8572 KB
013.txt AC 69 ms 8572 KB
014.txt AC 70 ms 8572 KB
015.txt AC 70 ms 8572 KB
016.txt AC 70 ms 8572 KB
017.txt AC 69 ms 8572 KB
018.txt AC 69 ms 8572 KB
019.txt AC 69 ms 8572 KB
020.txt AC 70 ms 8572 KB
021.txt AC 70 ms 8572 KB
022.txt AC 68 ms 8572 KB
023.txt AC 69 ms 8572 KB
024.txt AC 69 ms 8572 KB
025.txt AC 69 ms 8572 KB
026.txt AC 69 ms 8572 KB
027.txt AC 69 ms 8572 KB
028.txt AC 69 ms 8572 KB
029.txt AC 69 ms 8572 KB
030.txt AC 70 ms 8572 KB
031.txt AC 70 ms 8572 KB
032.txt AC 69 ms 8572 KB
033.txt AC 69 ms 8572 KB
034.txt AC 67 ms 8572 KB
035.txt AC 70 ms 8572 KB
036.txt AC 69 ms 8572 KB
037.txt AC 68 ms 8572 KB
038.txt AC 69 ms 8572 KB
039.txt AC 69 ms 8572 KB
040.txt AC 3 ms 8572 KB
041.txt AC 3 ms 8572 KB
042.txt AC 3 ms 8572 KB
043.txt AC 3 ms 8572 KB
example0.txt AC 3 ms 8572 KB
example1.txt AC 69 ms 8572 KB