Skip to content

Commit b31e293

Browse files
committed
modify code
1 parent e204c3f commit b31e293

File tree

2 files changed

+179
-1
lines changed

2 files changed

+179
-1
lines changed

src/class47/Code01_StrangePrinter.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,52 @@
33
// 本题测试链接 : https://leetcode.com/problems/strange-printer/
44
public class Code01_StrangePrinter {
55

6-
public static int strangePrinter(String s) {
6+
public static int strangePrinter1(String s) {
7+
if (s == null || s.length() == 0) {
8+
return 0;
9+
}
10+
char[] str = s.toCharArray();
11+
return process1(str, 0, str.length - 1);
12+
}
13+
14+
public static int process1(char[] str, int L, int R) {
15+
if (L == R) {
16+
return 1;
17+
}
18+
int ans = R - L + 1;
19+
for (int k = L + 1; k <= R; k++) {
20+
ans = Math.min(ans, process1(str, L, k - 1) + process1(str, k, R) - (str[L] == str[k] ? 1 : 0));
21+
}
22+
return ans;
23+
}
24+
25+
public static int strangePrinter2(String s) {
26+
if (s == null || s.length() == 0) {
27+
return 0;
28+
}
29+
char[] str = s.toCharArray();
30+
int N = str.length;
31+
int[][] dp = new int[N][N];
32+
return process2(str, 0, N - 1, dp);
33+
}
34+
35+
public static int process2(char[] str, int L, int R, int[][] dp) {
36+
if (dp[L][R] != 0) {
37+
return dp[L][R];
38+
}
39+
int ans = R - L + 1;
40+
if (L == R) {
41+
ans = 1;
42+
} else {
43+
for (int k = L + 1; k <= R; k++) {
44+
ans = Math.min(ans, process2(str, L, k - 1, dp) + process2(str, k, R, dp) - (str[L] == str[k] ? 1 : 0));
45+
}
46+
}
47+
dp[L][R] = ans;
48+
return ans;
49+
}
50+
51+
public static int strangePrinter3(String s) {
752
if (s == null || s.length() == 0) {
853
return 0;
954
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// 本题测试链接:
2+
// https://lightoj.com/problem/internet-bandwidth
3+
// 这是一道DinicAlgorithm算法的题
4+
// 把如下代码粘贴进网页所提供的编译器环境中
5+
// 选择java编译环境
6+
// 不需要修改任何内容可以直接通过
7+
// DinicAlgorithm用法请看网页上的题目描述并结合main函数的写法
8+
9+
package class47;
10+
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.LinkedList;
14+
import java.util.Scanner;
15+
16+
public class Code03_DinicAlgorithm {
17+
18+
public static class Edge {
19+
public int from;
20+
public int to;
21+
public int available;
22+
23+
public Edge(int a, int b, int c) {
24+
from = a;
25+
to = b;
26+
available = c;
27+
}
28+
}
29+
30+
public static class Dinic {
31+
private int N;
32+
private ArrayList<ArrayList<Integer>> nexts;
33+
private ArrayList<Edge> edges;
34+
private int[] depth;
35+
private int[] cur;
36+
37+
public Dinic(int nums) {
38+
N = nums + 1;
39+
nexts = new ArrayList<>();
40+
for (int i = 0; i <= N; i++) {
41+
nexts.add(new ArrayList<>());
42+
}
43+
edges = new ArrayList<>();
44+
depth = new int[N];
45+
cur = new int[N];
46+
}
47+
48+
public void addEdge(int u, int v, int r) {
49+
int m = edges.size();
50+
edges.add(new Edge(u, v, r));
51+
nexts.get(u).add(m);
52+
edges.add(new Edge(v, u, 0));
53+
nexts.get(v).add(m + 1);
54+
}
55+
56+
public int maxFlow(int s, int t) {
57+
int flow = 0;
58+
while (bfs(s, t)) {
59+
Arrays.fill(cur, 0);
60+
flow += dfs(s, t, Integer.MAX_VALUE);
61+
}
62+
return flow;
63+
}
64+
65+
private boolean bfs(int s, int t) {
66+
depth[s] = 0;
67+
LinkedList<Integer> queue = new LinkedList<>();
68+
queue.addFirst(s);
69+
boolean[] visited = new boolean[N];
70+
visited[s] = true;
71+
while (!queue.isEmpty()) {
72+
int u = queue.pollLast();
73+
for (int i = 0; i < nexts.get(u).size(); i++) {
74+
Edge e = edges.get(nexts.get(u).get(i));
75+
int v = e.to;
76+
if (!visited[v] && e.available > 0) {
77+
visited[v] = true;
78+
depth[v] = depth[u] + 1;
79+
queue.addFirst(v);
80+
}
81+
}
82+
}
83+
return visited[t];
84+
}
85+
86+
private int dfs(int s, int t, int r) {
87+
if (s == t || r == 0) {
88+
return r;
89+
}
90+
int f = 0;
91+
int flow = 0;
92+
for (; cur[s] < nexts.get(s).size(); cur[s]++) {
93+
int ei = nexts.get(s).get(cur[s]);
94+
Edge e = edges.get(ei);
95+
Edge o = edges.get(ei ^ 1);
96+
if (depth[e.to] == depth[s] + 1 && (f = dfs(e.to, t, Math.min(e.available, r))) != 0) {
97+
e.available -= f;
98+
o.available += f;
99+
flow += f;
100+
r -= f;
101+
if (r <= 0) {
102+
break;
103+
}
104+
}
105+
}
106+
return flow;
107+
}
108+
109+
}
110+
111+
public static void main(String[] args) {
112+
Scanner cin = new Scanner(System.in);
113+
int cases = cin.nextInt();
114+
for (int i = 1; i <= cases; i++) {
115+
int n = cin.nextInt();
116+
int s = cin.nextInt();
117+
int t = cin.nextInt();
118+
int m = cin.nextInt();
119+
Dinic dinic = new Dinic(n);
120+
for (int j = 0; j < m; j++) {
121+
int from = cin.nextInt();
122+
int to = cin.nextInt();
123+
int weight = cin.nextInt();
124+
dinic.addEdge(from, to, weight);
125+
dinic.addEdge(to, from, weight);
126+
}
127+
int ans = dinic.maxFlow(s, t);
128+
System.out.println("Case " + i + ": " + ans);
129+
}
130+
cin.close();
131+
}
132+
133+
}

0 commit comments

Comments
 (0)