package graph;
class MEdge {
public int u;
public int v;
public int w;
public MEdge(int u, int v, int w) {
this.u = u;
this.v = v;
this.w = w;
}
}
public class SingleSourceShortest {
public static void main(String args[]) {
MEdge[] E = new MEdge[10];
E[0] = new MEdge(0, 1, 3);
E[1] = new MEdge(0, 4, 5);
E[2] = new MEdge(1, 4, 2);
E[3] = new MEdge(4, 1, 1);
E[4] = new MEdge(3, 0, 3);
E[5] = new MEdge(4, 2, 4);
E[6] = new MEdge(1, 2, 6);
E[7] = new MEdge(2, 3, 2);
E[8] = new MEdge(3, 2, 7);
E[9] = new MEdge(4, 3, 6);
new SingleSourceShortest(5, E);
}
private int[] d;
private int[] pi;
private int[] f;
private int[] sequence;
private int[] color;// 0,1,2
private int infinite;
private int s;
private int num;
private MEdge[] E;
private int time;
public SingleSourceShortest(int num, MEdge[] E) {
this.time = 0;
this.num = num;
this.d = new int[num];
this.pi = new int[num];
this.f = new int[num];
this.color = new int[num];
this.sequence = new int[num];
this.infinite = 1000;
this.s = 0;
this.E = E;
if (bellmanFord()) {
print();
}
/*if (dagShortest()) {
print();
}*/
}
public boolean dagShortest() {
initializeSingleSource(s);
this.time = 0;
dfs(s);
/*
* for (int i = 0; i < f.length; i++) { System.out.println(f[i]); }
*/
for (int i = num; i >= 1; i--) {
for (int j = 1; j < i; j++) {
if (f[j] > f[j - 1]) {
int temp;
temp = sequence[j];
sequence[j] = sequence[j - 1];
sequence[j - 1] = temp;
// temp=f[j];
// f[j]=f[j-1];
// f[j-1]=temp;
}
}
}
// for(int i=0;i<num;i++){
// System.out.println(f[i]+" "+sequence[i]);
// }
int uu;
for (int i = 0; i < num; i++) {
uu = sequence[i];
// System.out.println(uu);
for (int j = 0; j < num; j++) {
if (E[j].u == uu) {
System.out.println("松弛" + E[j].u + " " + E[j].v + " "
+ E[j].w);
relax(E[j].u, E[j].v, E[j].w);
}
}
}
return true;
}
public void dfs(int s) {
color[s] = 1;
time++;
for (int i = 0; i < E.length; i++) {
if (E[i].u == s) {
if (color[E[i].v] == 0) {
// System.out.println(E[i].v);
dfs(E[i].v);
}
}
}
time++;
f[s] = time;
color[s] = 2;
}
public void print() {
int index = s + 1;
int temp;
for (int i = 1; i < num; i++) {
System.out.print("从" + s + "到" + index + "的最短路径是:" + index);
temp = pi[index];
while (temp != -1) {
System.out.print("<-" + temp);
temp = pi[temp];
}
System.out.println("*****权值为:" + d[index]);
index = (index + 1) % num;
}
}
public boolean bellmanFord() {
initializeSingleSource(s);
for (int i = 1; i < num; i++) {
for (int j = 0; j < E.length; j++) {
relax(E[j].u, E[j].v, E[j].w);
}
}
for (int i = 0; i < E.length; i++) {
if (d[E[i].v] > d[E[i].u] + E[i].w)
return false;
}
return true;
}
public void initializeSingleSource(int s) {
for (int i = 0; i < d.length; i++) {
d[i] = infinite;
pi[i] = -1;
color[i] = 0;
f[i] = -1;
sequence[i] = i;
}
d[s] = 0;
}
public void relax(int u, int v, int w) {
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
pi[v] = u;
}
}
}