题目描述
blablabla
样例
blablabla
prim
问题:
怎样才能用BufferedReader来读这种输入有两个空格有一个空格的数据?
Java 代码
import java.io.*;
import java.util.*;
public class Main{
private static int N = 110;
private static int n,INF = 0x3f3f3f3f;
private static int[][] g = new int[N][N];
private static int[] dist = new int[N];
private static boolean[] st = new boolean[N];
private static int prim(){
Arrays.fill(dist,INF);
int ans = 0;
for(int i = 0 ; i < n ;i++){
int t = -1;
for(int j = 1 ; j <= n ; j++){
while(!st[j] && (t == -1 || dist[t] > dist[j])){
t = j;
}
}
if(i != 0 && dist[t] == INF) return INF;
if(i != 0) ans += dist[t];
st[t] = true;
for(int j = 1 ; j <= n ; j++){
dist[j] = Math.min(dist[j],g[t][j]);
}
}
return ans;
}
public static void main(String[] args) throws IOException{
//BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(int i = 1 ; i <= n ; i++){
//String[] s = in.readLine().split(" ");
for(int j = 1 ; j <= n ; j++){
g[i][j] = sc.nextInt();
}
}
int ans = prim();
if(ans == INF) System.out.println("impossible");
else System.out.println(ans);
}
}
去搜正则表达式
已解决。多谢~