Home > other >  Consult POJ1201 Intervals (difference restraint system)
Consult POJ1201 Intervals (difference restraint system)

Time:10-08

My practice:
Vector analog chain table structure + SPFA algorithm of figure, the sample test is correct, but the test is a Wrong Answer, why?
The problem of boundary data is preliminary thinking:? But how to find out the boundary data? Or is my SPFA algorithm is wrong?
O what a great god help give advice or comments?
I code:
 # include 
#include
#include
#include
#include

#include
#include
#include


#include
#include
#include
#include//infinity, infinitesimal INT_MAX, INT_MIN
#include//DBL_MAX - DBL_MAX


using namespace std;

Const int N=50020;
Struct E {
Int next;
int cost;
};
Vector Edge [N];//graph adjacency list
Int dis [N].//dis [I] 0 to the shortest distance I
Bool vis [N].//access sign
Bool spfa (int numnode);//numnode node
total numberVoid addEdge (int a, b int, int) c {/edge/add
E E;
Ost of e.c. with our fabrication:=c;
E.n ext=b;
Edge [a]. Push_back (e);
}
Int main (int arg c, char * * argv) {
int n;
Cin> n;
int a,b,c;
E E;
Int max_v=1;
for(int i=0; iEdge [I]. The clear ();
}
While (n -) {
Cin> A> b> c;
A +=10;//in the original image processing, in order to avoid the ai - 1==1, we have made all the ai and bi from gain 10.
B +=10;//so we formed a has a point [9, max_v] directed graph (in fact, the difference restraint system)
Max_v=Max (max_v, b);//to find the maximum
AddEdge (b, a - 1 - c);//edge (b, a 1)
}

For (int I=10; i<=max_v; I++) {
AddEdge (I, I - 1, 0);
AddEdge (I - 1, I, 1);
AddEdge (0, I, 0);
}
AddEdge (0,9,0);
//the total number of 0 ~ max_v, does it include super source point?
Int numnode=max_v + 1;
Spfa (numnode);
Int ret=dis] [numnode - 1 - dis [9].
Coutreturn 0;
}
Bool spfa (int numnode) {
Queue q;
for(int i=0; iDis [I]=INT_MAX;
Vis [I]=false;
}
Dis [0]=0;
Vis [0]=true;
Q.p ush (0);
while(! Q.e mpty ()) {
Int u=q.f ront ();
Q.p op ();
Vis [u]=false;
for(int i=0; iBool update=false;
Int v=edge [u] [I] next;
Int c=edge [u] [I] cost;
If (dis [u]!=INT_MAX & amp; & Dis [v] & gt; Dis [u] + c) {//
Dis [v]=dis [u] + c;
if(! Vis [v]) {
Q.p ush (v);
Vis [v]=true;
The update=true;
/* if (+ + [v] CNT & gt; Numnode) {
return false;
} */
}
}
if(! The update) {
break;
}
}
}
return true;
}
  • Related