ZOJ-1522

来源:互联网 发布:淘宝网太极裤 编辑:程序博客网 时间:2024/06/04 19:35

图论加模拟题,看解题报告分类说是水题,用Floyd求传递闭包,源发出一条信息,可达点全部收到信息,看可达点有多少条出度,即他会转发给几个人,然后根据题意判断出相应的结果,注意,如果源发出的信息如果有些点不可达,就是说没收到,那默认算这个人没有转发,那肯定是最差的评价!这个有点坑啊,没收到信息竟然算转发了0个人。。。还有注意输出每个string后都要加空格,包括最后一个,PE了一次

import java.util.Arrays;import java.util.Scanner;public class Main{static void floyd(boolean map[][], int n){for (int k = 1; k <= n; k++)for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)map[i][j] = map[i][j] || (map[i][k] && map[k][j]);}public static void main(String[] args){Scanner sc = new Scanner(System.in);boolean[][] map = new boolean[21][21];int[] friend = new int[21];String[] res = new String[21];while (true){Arrays.fill(res, "");int f, now, t1, t2, n = sc.nextInt();if (n == 0)break;for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)map[i][j] = false;for (int i = 1; i <= n; i++){friend[i] = 0;while ((f = sc.nextInt()) != 0){map[i][f] = true;friend[i]++;}}sc.nextLine();floyd(map, n);while ((now = sc.nextInt()) != 0){String line = sc.nextLine();String ss[] = line.trim().split(" ");t1 = Integer.parseInt(ss[0]);t2 = Integer.parseInt(ss[1]);for (int j = 1; j <= n; j++){int to = 0;if (j == now || map[now][j] == true)to = friend[j];if (to < t1)res[j] += ss[2] + " ";else if (to < t2)res[j] += ss[3] + " ";elseres[j] += ss[4] + " ";}}sc.nextLine();for (int i = 1; i <= n; i++)System.out.println(sc.nextLine() + ": " + res[i]);}sc.close();}}


0 0
原创粉丝点击