POJ3259-JAVA语言描述-Bellman_Ford算法
来源:互联网 发布:c语言基础知识txt下载 编辑:程序博客网 时间:2024/06/06 05:35
读懂题意真难,时间都浪费在理解题意上面了。然后,发现,,这又是一个水题!分分钟撸完了!!!!
引用这里的解释吧我就不多加赘述了!
给你们解释下输入案例数据也许会更清晰的哟!下面看代码,注释很详细!!
若有bug,不足,望悉心指出,thanks!!!!
import java.io.BufferedInputStream;import java.util.Scanner;/* * 输入:2 //农场个数3 3 1 //田地 路径 虫洞 分别的个数1 2 2 //田地路径 起点 终点 权值1 3 42 3 13 1 3 //虫洞路径 起点 终点 权值(记得,事实上市负的)3 2 1//田地 路径 虫洞 分别的个数1 2 3//田地路径 起点 终点 权值2 3 43 1 8//虫洞路径 起点 终点 权值(记得,事实上市负的) * * */public class POJ3259 {static int M = 6000, N = 500 + 20, Inf = 1000000000 + 10;static edge_3295 e[] = new edge_3295[M];static void start() {for (int i = 0; i < M; i++) {e[i] = new edge_3295();}}public static void main(String[] args) {// TODO Auto-generated method stubint d[] = new int[N];start();Scanner sc = new Scanner(new BufferedInputStream(System.in));int F = sc.nextInt();int n, m, w, s, t, l, top;while (F-- != 0) {//有多少组数据,while 循环读入 //要读入的数据有规律 第一行是 图的节点数 和 边数,但是注意咯 ,这里的边都是可以往返的,所以咯有了下面的top = 0;n = sc.nextInt();m = sc.nextInt();w = sc.nextInt();for (int i = 0; i < m; i++) {s = sc.nextInt();t = sc.nextInt();l = sc.nextInt();e[top++].set(s, t, l);//注意咯上面说的下面 是这里e[top++].set(t, s, l);//这里对 s 和 t or t和s 进行了两次保存,就是因为线路是可以反向的}for (int i = 0; i < w; i++) {s = sc.nextInt();t = sc.nextInt();l = sc.nextInt();e[top++].set(s, t, -l);}if (Bellman_Ford(n, top, d, 0, e)) {System.out.println("NO");} else {System.out.println("YES");}}}private static boolean Bellman_Ford(int n, int m, int[] d, int s, edge_3295[] e) {//Bellman_Ford 核心代码,模板,这里我就不多解释了// TODO Auto-generated method stubfor (int i = 0; i <= n; i++) {d[i] = Inf;}d[s] = 0;int check;for (int i = 0; i < n - 1; i++) {check = 0;for (int j = 0; j < m; j++) {if (d[e[j].t] > d[e[j].s] + e[j].l) {d[e[j].t] = d[e[j].s] + e[j].l;check=1;}}if (check == 0) {break;}}for (int i = 0; i < m; i++) {if (d[e[i].t] > d[e[i].s] + e[i].l) {return false;}}return true;}}class edge_3295 {int s;int t;int l;public void set(int _s, int _t, int _l) {this.s = _s;this.t = _t;this.l = _l;}}
样例的输出为:
NO
YES
0 0
- POJ3259-JAVA语言描述-Bellman_Ford算法
- poj3259 Bellman_ford算法
- Bellman_Ford POJ3259
- poj3259(Bellman_ford)
- poj3259 Bellman_Ford
- poj3259 Bellman_ford
- poj3259 bellman_ford 算法 判环 最短路
- poj3259( spfa 和 bellman_ford)
- POJ3259 Wormholes 解题报告--bellman_ford
- POJ3259 Wormholes SPFA 或者 bellman_ford
- Bellman_ford算法
- Bellman_ford算法
- Bellman_ford算法
- Bellman_Ford 算法
- Bellman_Ford算法
- Bellman_Ford算法
- Bellman_Ford算法
- Bellman_Ford算法
- spark发行版笔记2
- hdu1078 FatMouse and Cheese
- scala基础与实践上
- POJ-1456 Supermarket (贪心)
- 进程 vs. 线程
- POJ3259-JAVA语言描述-Bellman_Ford算法
- poj3136 Milking Time
- 帧、数据报、数据包的区别和联系
- iOS开发 ☞ 推送通知(Push Notification)
- 【AKOJ】1125小写字母变为大写字母
- adapter总结
- IMF SPARK 源代码发行定制班 预习课程 Spark框架源码的调试 (2) 从master worker main入口进行调试
- hdu2859 Phalanx
- 自动加载下一页