[JAVA] ABC - 022 - A & B & C.

AtCoder ABC 022 A&B&C AtCoder - 022

** 14.05.2019 Nachschrift ** Problem B: Codeänderung, um die Summe der res zu erhalten

Ein Problem

--Überprüfen Sie, ob das Gewicht bei jeder Aufnahme im richtigen Bereich liegt

	private void solveA() {
		int n = nextInt();
		int s = nextInt();
		int t = nextInt();
		int w = nextInt();

		long sum = w;
		int res = 0;
		for (int i = 0; i < n; i++) {
			if (i != 0) {
				sum += nextInt();
			}
			if (s <= sum && sum <= t) {
				res++;
			}
		}

		out.println(res);
	}

B Problem

(2019/05/14 Codekorrektur, um die Summe der Res zu erhalten)

		int numN = nextInt();

		Map<Integer, Long> tmp = IntStream.range(0, numN).map(i -> nextInt()).mapToObj(i -> new Integer(i))
				.collect(Collectors.groupingBy(s -> s, Collectors.counting()));

		long res = tmp.values().stream().mapToLong(i -> i - 1).sum();

		//		long res = tmp.values().stream().filter(i -> i.longValue() > 1).reduce(0L, (sum, i) -> sum += (i - 1));

		//		int[] wk = IntStream.range(0, numN).map(i -> nextInt()).toArray();
		//		Map<Integer, Long> tmp = Arrays.stream(wk).mapToObj(i -> new Integer(i))
		//				.collect(Collectors.groupingBy(s -> s, Collectors.counting()));
		//		long res = tmp.values().stream().filter(i -> i.longValue() > 1).reduce(0L, (sum, i) -> sum += (i - 1));

		out.println(res);

Problem C: Worshall Floyd-Methode

――Gehen Sie aus 1 heraus und kehren Sie zu 1 zurück ――Jedoch können Sie nicht dieselbe Straße benutzen

Peak 1 2 3 4 5
1 0 INF INF INF INF
2 INF 0 5 10 3
3 INF 5 0 5 2
4 INF 10 5 0 7
5 INF 3 2 7 0
Peak 1 2 3 4 5
Peak 1 0 2 6 1 5
2 2 0 5 3 3
3 6 5 0 5 2
4 1 3 5 0 6
5 5 3 2 6 0
	private void solveC() {
		final long CONST_INF = Long.MAX_VALUE / 10;

		int n = nextInt();
		int m = nextInt();
		long[][] graphWithOutStart = new long[n][n];
		long[][] graph = new long[n][n];
		List<Integer> edgeNearByStart = new ArrayList<Integer>();

		/*
		 *Graphinitialisierung
		 * [1]Mit benachbarter Matrix inklusive[1]Erstellen einer benachbarten Matrix ohne
		 */
		for (int i = 0; i < n; i++) {
			Arrays.fill(graph[i], CONST_INF);
			Arrays.fill(graphWithOutStart[i], CONST_INF);
			graphWithOutStart[i][i] = 0;
			graph[i][i] = 0;
		}

		for (int i = 0; i < m; i++) {

			/*
			 *Nach dem Index-1 und erfassen
			 */
			int from = nextInt() - 1;
			int to = nextInt() - 1;
			int cost = nextInt();
			if (from != 0) {
				/*
				 * [1]Grafik ohne
				 */
				graphWithOutStart[from][to] = cost;
				graphWithOutStart[to][from] = cost;
			} else {
				/*
				 * [1]Angrenzend an die Eckpunkte
				 */
				edgeNearByStart.add(to);
			}
			graph[from][to] = cost;
			graph[to][from] = cost;
		}

		long res = Long.MAX_VALUE;

		Collections.sort(edgeNearByStart);

		/*
		 *Warshall - Floyd-Methode zum Aktualisieren von Scheitelpunkten
		 */

		getMinByWarshall(graphWithOutStart, n);

		for (int i = 0; i < edgeNearByStart.size(); i++) {
			for (int j = i + 1; j < edgeNearByStart.size(); j++) {
				int start = edgeNearByStart.get(i);
				int end = edgeNearByStart.get(j);
				/*
				 * [1]Von[Benachbarte Eckpunkte 1]Kosten+ [Benachbarte Eckpunkte 1][Benachbarte Eckpunkte 2]Kosten+ [Benachbarte Eckpunkte 2]Von[1]Kosten
				 */
				long total = graph[0][start] + graphWithOutStart[start][end] + graph[0][end];
				//Niedrigen Wert annehmen
				res = Long.min(res, total);
			}

		}

		out.println(res >= CONST_INF ? -1 : res);

	}

	/**
	 *
	 * @param edge
	 * @param point
	 */
	private void getMinByWarshall(long[][] edge, int point) {
		for (int k = 0; k < point; k++) {
			for (int i = 0; i < point; i++) {
				for (int j = 0; j < point; j++) {
					edge[i][j] = Long.min(edge[i][j], edge[i][k] + edge[k][j]);
				}
			}
		}

	}

Problem C: Worshall Floyd-Methode (Kurzcode-Version)


Peak 1 2 3 4 5
Peak 1 0 2 INF 1 12
2 2 0 5 10 3
3 INF 5 0 5 2
4 1 10 5 0 7
5 12 3 2 7 0
Peak 1 2 3 4 5
1 0 INF INF INF INF
2 INF 0 5 10 3
3 INF 5 0 5 2
4 INF 10 5 0 7
5 INF 3 2 7 0

	private void solveC() {
		final long CONST_INF = Long.MAX_VALUE / 10;

		int n = nextInt();
		int m = nextInt();
		long[][] graph = new long[n][n];

		/*
		 *Graphinitialisierung
		 * [1]Mit benachbarter Matrix inklusive[1]Erstellen einer benachbarten Matrix ohne
		 */
		for (int i = 0; i < n; i++) {
			Arrays.fill(graph[i], CONST_INF);
			graph[i][i] = 0;
		}

		for (int i = 0; i < m; i++) {

			/*
			 *Nach dem Index-1 und erfassen
			 */
			int from = nextInt() - 1;
			int to = nextInt() - 1;
			int cost = nextInt();
			graph[from][to] = cost;
			graph[to][from] = cost;
		}

		long res = Long.MAX_VALUE;

		/*
		 *Warshall - Floyd-Methode zum Aktualisieren von Scheitelpunkten
		 */

		/*
		 *Wenn Sie den Index mit 1 starten, wird er für 1 nicht aktualisiert
		 */
		for (int k = 1; k < n; k++) {
			for (int i = 1; i < n; i++) {
				for (int j = 1; j < n; j++) {
					graph[i][j] = Math.min(graph[i][j], graph[i][k] + graph[k][j]);
				}
			}
		}

		for (int i = 1; i < n; i++) {
			for (int j = 1; j < n; j++) {
				if (i != j) {
					res = Math.min(res, graph[0][i] + graph[i][j] + graph[j][0]);
				}
			}
		}

		out.println(res >= CONST_INF ? -1 : res);

	}

Recommended Posts

ABC - 013-A & B & C.
ABC - 023 - A & B & C.
ABC - 036-A & B & C.
ABC - 010 - A & B & C.
ABC - 028 - A & B & C.
ABC - 015 - A & B & C.
ABC - 012-A & B & C.
ABC - 018 - A & B & C.
ABC - 054 - A & B & C.
ABC - 017 - A & B & C.
ABC - 029 - A & B & C.
ABC - 022 - A & B & C.
ABC - 019 - A & B & C.
ABC - 020 - A & B & C.
ABC - 030 - A & B & C.
ABC - 127 - A & B & C.
ABC - 007 - A & B & C.
ABC - 132 - A & B & C.
ABC - 026 - A & B & C.
ABC - 014 - A & B & C.
ABC - 016 - A & B & C.
ABC - 011-A & B & C.
ABC - 031 - A & B & C.
ABC - 021 - A & B & C.
ABC - 025 - A & B & C.
ABC - 024 - A & B & C.
ABC - 027 - A & B & C.
ABC - 080 - A & B & C.
ABC - 129 - A & B & C & D.
ABC - 122 - A & B & C & D.
ABC - 125 - A & B & C & D.
ABC - 130 - A & B & C & D.
ABC - 126 - A & B & C & D.
ABC - 134 - A & B & C & D & E.
ABC - 131 - A & B & C & D & E.
diverta 2019 Programmierwettbewerb A & B & C & D.
AtCoder Anfängerwettbewerb 169 A, B, C mit Rubin
atcoder ABC113 C Problem
ABC093 C - Gleiche Ganzzahlen
atcoder ABC115 C Problem
AtCoder Anfängerwettbewerb 170 A, B, C bis Rubin
Eine Person, die C ++ schreibt, hat versucht, Java zu schreiben
Machen Sie einen SOAP-Aufruf in C #
Rufen Sie C-Sprachfunktionen von Swift aus auf
NLP4J 100 Sprachverarbeitungsklopfen mit NLP4J # 34 "A B"