Submission #4635327
Source Code Expand
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::{stderr, stdin, stdout, BufWriter, StdoutLock, Write};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_macros)]
macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; let mut next = || { iter . next ( ) . unwrap ( ) } ; input_inner ! { next , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let stdin = std :: io :: stdin ( ) ; let mut bytes = std :: io :: Read :: bytes ( std :: io :: BufReader :: new ( stdin . lock ( ) ) ) ; let mut next = move || -> String { bytes . by_ref ( ) . map ( | r | r . unwrap ( ) as char ) . skip_while ( | c | c . is_whitespace ( ) ) . take_while ( | c |! c . is_whitespace ( ) ) . collect ( ) } ; input_inner ! { next , $ ( $ r ) * } } ; }
#[allow(unused_macros)]
macro_rules ! input_inner { ( $ next : expr ) => { } ; ( $ next : expr , ) => { } ; ( $ next : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ next , $ t ) ; input_inner ! { $ next $ ( $ r ) * } } ; ( $ next : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ next , $ t ) ; input_inner ! { $ next $ ( $ r ) * } } ; }
#[allow(unused_macros)]
macro_rules ! read_value { ( $ next : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ next , $ t ) ) ,* ) } ; ( $ next : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ next , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ next : expr , [ $ t : tt ] ) => { { let len = read_value ! ( $ next , usize ) ; ( 0 .. len ) . map ( | _ | read_value ! ( $ next , $ t ) ) . collect ::< Vec < _ >> ( ) } } ; ( $ next : expr , chars ) => { read_value ! ( $ next , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ next : expr , bytes ) => { read_value ! ( $ next , String ) . into_bytes ( ) } ; ( $ next : expr , usize1 ) => { read_value ! ( $ next , usize ) - 1 } ; ( $ next : expr , $ t : ty ) => { $ next ( ) . parse ::<$ t > ( ) . expect ( "Parse error" ) } ; }
#[allow(dead_code)]
struct Writer {
s: String,
}
#[allow(unused_imports)]
use std::fmt::Display;
#[allow(dead_code)]
#[doc = " let mut writer = Writer::new();"]
#[doc = " writer.writeln(hoge);"]
#[doc = " writer.flush()"]
impl Writer {
#[allow(dead_code)]
pub fn new() -> Writer {
Writer { s: String::new() }
}
#[allow(dead_code)]
pub fn flush(&mut self) {
print!("{}", self.s);
self.s.clear();
}
pub fn write<T: Display>(&mut self, x: T) {
self.s.push_str(&format!("{}", x));
}
pub fn writeln<T: Display>(&mut self, x: T) {
self.s.push_str(&format!("{}", x));
self.s.push('\n');
}
#[allow(dead_code)]
pub fn write_vec<T: Display>(&mut self, xs: &Vec<T>) {
if xs.len() == 0 {
self.writeln("");
return;
}
self.write(&format!("{}", xs[0]));
for i in 1..xs.len() {
self.write(&format!(" {}", xs[i]));
}
self.writeln("");
}
}
#[allow(unused_macros)]
macro_rules ! dbg { ( $ ( $ a : expr ) ,* ) => { writeln ! ( & mut stderr ( ) , concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) . unwrap ( ) ; } }
#[doc = " Ported from [bluss/permutohedron](https://github.com/bluss/permutohedron)"]
pub trait LexicalPermutation {
#[doc = " at the last ordered permutation."]
fn next_permutation(&mut self) -> bool;
#[doc = " at the first ordered permutation."]
fn prev_permutation(&mut self) -> bool;
}
impl<T> LexicalPermutation for [T]
where
T: Ord,
{
#[doc = " Original author in Rust: Thomas Backman <serenity@exscape.org>"]
fn next_permutation(&mut self) -> bool {
if self.len() < 2 {
return false;
}
let mut i = self.len() - 1;
while i > 0 && self[i - 1] >= self[i] {
i -= 1;
}
if i == 0 {
return false;
}
let mut j = self.len() - 1;
while j >= i && self[j] <= self[i - 1] {
j -= 1;
}
self.swap(j, i - 1);
self[i..].reverse();
true
}
fn prev_permutation(&mut self) -> bool {
if self.len() < 2 {
return false;
}
let mut i = self.len() - 1;
while i > 0 && self[i - 1] <= self[i] {
i -= 1;
}
if i == 0 {
return false;
}
self[i..].reverse();
let mut j = self.len() - 1;
while j >= i && self[j - 1] < self[i - 1] {
j -= 1;
}
self.swap(i - 1, j);
true
}
}
const INF: u64 = 1_000_000_000_000_000;
#[allow(non_snake_case)]
fn main() {
input! {
n: usize,
m: usize,
R: usize,
r: [usize1; R],
abc: [(usize1, usize1, u64); m]
}
let mut dist = vec![vec![INF; n]; n];
for i in 0..n {
dist[i][i] = 0;
}
for (a, b, c) in abc {
dist[a][b] = c;
dist[b][a] = c;
}
for k in 0..n {
for i in 0..n {
for j in 0..n {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
let mut p: Vec<usize> = (0..R).collect();
let mut ans: u64 = INF;
loop {
let mut sum: u64 = 0;
for i in 1..R {
sum += dist[r[p[i-1]]][r[p[i]]];
}
ans = min(ans, sum);
if !p.next_permutation() { break; }
}
println!("{}", ans);
}
Submission Info
Submission Time |
|
Task |
D - joisino's travel |
User |
wh317706 |
Language |
Rust (1.15.1) |
Score |
400 |
Code Size |
5813 Byte |
Status |
AC |
Exec Time |
29 ms |
Memory |
4352 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
400 / 400 |
Status |
|
|
Set Name |
Test Cases |
Sample |
sample_01.txt, sample_02.txt, sample_03.txt |
All |
01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, sample_01.txt, sample_02.txt, sample_03.txt |
Case Name |
Status |
Exec Time |
Memory |
01.txt |
AC |
28 ms |
4352 KB |
02.txt |
AC |
21 ms |
4352 KB |
03.txt |
AC |
21 ms |
4352 KB |
04.txt |
AC |
22 ms |
4352 KB |
05.txt |
AC |
23 ms |
4352 KB |
06.txt |
AC |
28 ms |
4352 KB |
07.txt |
AC |
29 ms |
4352 KB |
08.txt |
AC |
20 ms |
4352 KB |
sample_01.txt |
AC |
2 ms |
4352 KB |
sample_02.txt |
AC |
2 ms |
4352 KB |
sample_03.txt |
AC |
2 ms |
4352 KB |