AC 代码
package _03___图论;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
public class _02_POJ_3259 {
// t译文:农夫约翰在探索他的许多农场,发现了一些惊人的虫洞。虫洞是很奇特的,因为它是一个单向通道,
// 可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径,W(1≤W≤200)
// 个虫洞。作为一个狂热的时间旅行FJ的爱好者,他要做到以下几点:开始在一个区域,通过一些路径和虫洞旅行,他要回到
// 最开时出发的那个区域出发前的时间。也许他就能遇到自己了:)。为了帮助FJ找出这是否是可以或不可以,他会为你提供F个农
//场的完整的映射到(1≤F≤5)。所有的路径所花时间都不大于10000秒,所有的虫洞都不大于万秒的时间回溯。
//
// 输入
// 第1行:一个整数F表示接下来会有F个农场说明。
// 每个农场第一行:分别是三个空格隔开的整数:N,M和W
// 第2行到M+1行:三个空格分开的数字(S,E,T)描述,分别为:需要T秒走过S和E之间的双向路径。两个区域可能由一个以上的路径来连接。
// 第M +2到M+ W+1行:三个空格分开的数字(S,E,T)描述虫洞,描述单向路径,S到E且回溯T秒。
// 输出
// F行,每行代表一个农场
// 每个农场单独的一行,” YES”表示能满足要求,”NO”表示不能满足要求。
// NO
// YES
// 哈哈,要想回到原点。。。虫洞可以让 你回到过去,就相当于一个负权。。。然后判断是否存在负环,即走走走。。。然后又走回去了,,,
//1.定义图
//list数组,图的邻接表
public static List<Node>[] G;
//2.定义d数组,表示距离
public static int d[];
//3.定义p数组,表示上一个的路径
public static int p[];
//4.定义cnt数组,表示每个点被加入队列的次数,用于检查负环
public static int cnt[];
//5.定义inq数组,用于记录某点是否加入队列
public static boolean inq[];
//6.队列
public static Queue<Integer> queue;
//7.图的点数
public static int N;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int F = sc.nextInt();
for (int i = 0; i < F; i++) {
//给N赋值
N = sc.nextInt();
int M = sc.nextInt();
int W = sc.nextInt();
//初始化图的数组
G = new List[N+1];
for (int j = 0; j < N+1; j++)G[j] = new ArrayList<Node>();
int u,v,w;
for (int j = 0; j < M; j++) {
u = sc.nextInt();
v = sc.nextInt();
w = sc.nextInt();
G[u].add(new Node(u,v,w));
G[v].add(new Node(v,u,w));
}
for (int j = 0; j < W; j++) {
u = sc.nextInt();
v = sc.nextInt();
w = sc.nextInt();
G[u].add(new Node(u,v,0-w));
}
System.out.println(bellean_ford(1)?"NO":"YES");
}
}
public static boolean bellean_ford(int s) {
//初始化数组
d = new int[N+1];
Arrays.fill(d, Integer.MAX_VALUE);
d[s] = 0;
p = new int[N+1];
cnt = new int[N+1];
inq = new boolean[N+1];
queue = new LinkedList<Integer>();
queue.offer(s);
inq[s] = true;
while(!queue.isEmpty()){
int u = queue.poll();
inq[u] = false;
for (int i = 0; i < G[u].size(); i++) {
//取出起点为u的每一个点
Node node = G[u].get(i);
if(d[u]<Integer.MAX_VALUE&&d[node.to]>d[u]+node.dist) {
d[node.to]=d[u]+node.dist;
p[node.to]=u;
//node.to点没有加入队列
if(!inq[node.to]) {
queue.offer(node.to);
inq[node.to] = true;
if(++cnt[node.to]>N)return false;
}
}
}
}
return true;
}
}
class Node{
int from;
int to;
int dist;
public Node(int from,int to,int dist) {
this.from = from;
this.to = to;
this.dist = dist;
}
}
使用模板
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
public class 模板 {
//1.定义图
//list数组,图的邻接表
public static List<Node>[] G;
//2.定义d数组,表示距离
public static int d[];
//3.定义p数组,表示上一个的路径
public static int p[];
//4.定义cnt数组,表示每个点被加入队列的次数,用于检查负环
public static int cnt[];
//5.定义inq数组,用于记录某点是否加入队列
public static boolean inq[];
//6.队列
public static Queue<Integer> queue;
//7.图的点数
public static int N;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//给N赋值
N = sc.nextInt();
//初始化图的数组
G = new List[N+1];
for (int j = 0; j < N+1; j++)G[j] = new ArrayList<Node>();
}
public static boolean bellean_ford(int s) {
//初始化数组
d = new int[N+1];
Arrays.fill(d, Integer.MAX_VALUE);
d[s] = 0;
p = new int[N+1];
cnt = new int[N+1];
inq = new boolean[N+1];
queue = new LinkedList<Integer>();
queue.offer(s);
inq[s] = true;
while(!queue.isEmpty()){
int u = queue.poll();
inq[u] = false;
for (int i = 0; i < G[u].size(); i++) {
//取出起点为u的每一个点
Node node = G[u].get(i);
if(d[u]<Integer.MAX_VALUE&&d[node.to]>d[u]+node.dist) {
d[node.to]=d[u]+node.dist;
p[node.to]=u;
//node.to点没有加入队列
if(!inq[node.to]) {
queue.offer(node.to);
inq[node.to] = true;
if(++cnt[node.to]>N)return false;
}
}
}
}
return true;
}
}
class Node{
int from;
int to;
int dist;
public Node(int from,int to,int dist) {
this.from = from;
this.to = to;
this.dist = dist;
}
}