Take the water problem to practice Java ability, Topic link: http://poj.org/problem?id=2387
The result is a write-up. I really feel Java is not good (should be I too food tat)
1 Importjava.math.*;2 ImportJava.io.*;3 ImportJava.util.*;4 5 classvec{6 intT,w,next;7 Vec () {8T=0;w=0;next=0;9 }Ten } One A classedge{ - Static intmaxn=1005; - Static intmaxm=1000005; the Static intinf=1000000009; - int[]head=New int[MAXN]; - int[]d=New int[MAXN]; -VEC []e=NewVEC[MAXM]; + intNE; - Edge () { +Ne=0; A for(inti=0;i<maxn;i++){ atHead[i]=-1; -d[i]=inf; - } - for(inti=0;i<maxm;i++){ -e[i]=NewVec (); - } in } - to voidAdd_edge (intSintTintW) { +ne++; -e[ne].t=T; thee[ne].w=W; *e[ne].next=Head[s]; $head[s]=NE;Panax Notoginseng } - } the + Public classMain { A Public Static voidMain (string[] args) { theScanner cin=NewScanner (system.in); + intn,m; - while(Cin.hasnext ()) { $M=cin.nextint (); n=cin.nextint (); $Edge g=NewEdge (); - for(inti=0;i<m;i++){ - ints,t,w; theS=cin.nextint (); T=cin.nextint (); w=cin.nextint (); - G.add_edge (S, T, W);Wuyi G.add_edge (t, S, W); the } -G.d[1]=0; WuDeque<integer> q=NewLinkedlist<integer>(); -Q.add (1); About while(!Q.isempty ()) { $ intnow=Q.pop (); - for(intI=g.head[now];i!=-1;i=G.e[i].next) { - intv=g.e[i].t; - if(g.d[v]>g.d[now]+G.E[I].W) { Ag.d[v]=g.d[now]+G.E[I].W; + Q.add (v); the } - } $ } the System.out.println (G.d[n]); the } the } the -}
"Water" Java hackers-poj 2387