結果
| 問題 |
No.19 ステージの選択
|
| ユーザー |
夕叢霧香(ゆうむらきりか)
|
| 提出日時 | 2018-01-05 12:17:47 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 122 ms / 5,000 ms |
| コード長 | 2,616 bytes |
| コンパイル時間 | 2,556 ms |
| コンパイル使用メモリ | 79,424 KB |
| 実行使用メモリ | 41,576 KB |
| 最終ジャッジ日時 | 2024-12-23 10:09:15 |
| 合計ジャッジ時間 | 6,004 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int n=sc.nextInt();
int[]l=new int[n],s=new int[n];
for(int i=0;i<n;++i){
l[i]=sc.nextInt();
s[i]=sc.nextInt()-1;
}
int ans=0;
boolean[]vis=new boolean[n];
for(int i=0;i<n;++i){
if(vis[i])continue;
Map<Integer,Integer>hm=new HashMap<Integer,Integer>();
List<Integer>lis=new ArrayList<Integer>();
int step=0;
int cur=i;
while(true){
if(vis[cur])break;
vis[cur]=true;
hm.put(cur,step);
lis.add(cur);
cur=s[cur];
step++;
}
int min=1<<30;
Integer stm=hm.get(cur);
int st=stm==null?lis.size():stm;
for(int j=st;j<lis.size();++j)min=Math.min(min,l[lis.get(j)]);
if(stm==null)min=0;
ans+=min;
for(int v:lis)ans+=l[v];
}
out.printf("%.1f\n",ans/2.0);
out.close();
}
// http://codeforces.com/blog/entry/7018
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextIntArray(int n){
int[]r=new int[n];
for(int i=0;i<n;++i)r[i]=nextInt();
return r;
}
}
}
夕叢霧香(ゆうむらきりか)