- List的contains方法,当涉及自定义的类对象时,需要重写equals方法;
package collections;
import java.util.ArrayList;
import java.util.List;
class Node{
private int count;
Node(int count){
this.count = count;
}
// @Override
@Override
public boolean equals(Object obj) {
boolean equal = false;
if (obj instanceof Node) {
equal = this.count == ((Node) obj).count;
}
return equal;
}
}
public class ListFeatures {
public static void main(String[] args){
List<Integer> lists = new ArrayList<>();
lists.add(1);
lists.add(2);
lists.add(3);
System.out.println(lists);
System.out.println(lists.contains(1));
List<Node> countList = new ArrayList<>();
countList.add(new Node(1));
countList.add(new Node(2));
countList.add(new Node(3));
System.out.println(countList.contains(new Node(2)));
}
}
- 数据存储自定义类对象时并调用sort方法,需要实现Comparator<Weight>
package graph;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import static java.util.Arrays.sort;
class Weight {
int left;
int right;
int weight;
public Weight(int left, int right, int weight) {
this.left = left;
this.right = right;
this.weight = weight;
}
}
class MyComparator implements Comparator<Weight> {
@Override
public int compare(Weight o1, Weight o2) {
return o1.weight - o2.weight;
}
}
public class Kruskal {
static int N = 200010;
static int M = 400010;
static Weight[] weights = new Weight[N];
static int idx = 0;
static int[] p = new int[N];
static int sum = 0;
static int count = 0;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] line = reader.readLine().split(" ");
int n = Integer.parseInt(line[0]), m = Integer.parseInt(line[1]);
for (int i = 1; i <= n; i++) {
p[i] = i;
}
while (m-- > 0) {
String[] list = reader.readLine().split(" ");
int a = Integer.parseInt(list[0]), b = Integer.parseInt(list[1]), c = Integer.parseInt(list[2]);
idx++;
weights[idx] = new Weight(a, b, c);
}
sort(weights, 1, idx, new MyComparator());
for (int i = 1; i <= idx; i++) {
Weight tmp = weights[i];
int left = weights[i].left;
int right = weights[i].right;
int we = weights[i].weight;
int pLeft = find(left);
int pRight = find(right);
if (pLeft != pRight) {
sum += we;
p[pLeft] = pRight;
count++;
} else {
continue;
}
if (count == (n - 1)) {
break;
}
}
if (count == (n - 1)) System.out.println(sum);
else System.out.println("impossible");
}
private static int find(int left) {
if (p[left] != left) p[left] = find(p[left]);
return p[left];
}
}
package graph;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
class Node{
int dis;
int id;
public Node(int dis, int id){
this.dis = dis;
this.id = id;
}
public int getDis() {
return dis;
}
public int getId(){
return id;
}
public void setDis(int dis){
this.dis = dis;
}
public void setId(int id){
this.id = id;
}
}
public class Dijkstra2 {
static final int Max_length = 0x3f3f3f3f;
static int n, idx;
static int N = 100010;
static int []h = new int[N], e = new int[N], ne = new int[N], w = new int[N];
static int []dis = new int[N];
static PriorityQueue<Node> que = new PriorityQueue<Node>(new Comparator<Node>() {
public int compare(Node o1, Node o2) {
return o1.getDis() - o2.getDis();
}
});
static boolean[ ] st = new boolean[N];
public static void main(String[]args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String []line = reader.readLine().split("\\s");
n = Integer.parseInt(line[0]);
int m = Integer.parseInt(line[1]);
while(m-->0){
String []line1 = reader.readLine().split("\\s");
int a = Integer.parseInt(line1[0]), b = Integer.parseInt(line1[1]), c = Integer.parseInt(line1[2]);
add(a,b,c);
}
bfs();
}
private static void bfs() {
for(int i =1;i<=n;i++){
dis[i] = Max_length;
}
dis[1] = 0;
que.offer(new Node(0,1));
while(que.size()>0){
Node tmp = que.poll();
int distance = tmp.getDis();
int id = tmp.getId();
if(st[id]) continue;
st[id] = true;
for(int i =h[id]; i!=0;i = ne[i]){
int j = e[i];
if(dis[j]>distance+w[i]) {
dis[j] = distance+w[i];
que.offer(new Node(dis[j],j));
}
}
}
if (dis[n] == 0x3f3f3f3f) System.out.println("-1");
else System.out.println(dis[n]);
}
private static void add(int a, int b, int c) {
idx++;
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx;
}
}
网友评论