#include struct node { node* next; int time; }; struct points { int num; node* first; node* last; points() { num=0; } }; void main(void) { void findtime(points*, int**, int, int, int); int meettime(points a, points b); int a,i,n,m,p,q,j,k,s,t,l,meet; cin>>a; for(i=0;i>n>>m>>p>>q; int** streams = new int*[n]; for(j=0;j>s>>t>>l; streams[s-1][t-1]=l; } timea[p-1].first =new node(); timea[p-1].first->time=0; timeb[q-1].first = new node(); timeb[q-1].first->time = 0; timea[p-1].last = timea[p-1].first; timeb[q-1].last = timeb[q-1].first; timea[p-1].num++; timeb[q-1].num++; findtime(timea, streams,p-1,n,0); findtime(timeb, streams,q-1,n,0); meet = -1; int b; for(j=0;j 0) { if(time[i].num==0) { time[i].first=new node(); time[i].first->time = t+streams[cur][i]; time[i].last=time[i].first; } else { time[i].last->next = new node(); time[i].last->next->time = t + streams[cur][i]; time[i].last=time[i].last->next; } time[i].num++; findtime(time, streams, i,n,t + streams[cur][i]); } } } int meettime(points a, points b) { int time = -1; int i,j, cur; node* cura = a.first; node* curb; for(i = 0; i < a.num; i++) { curb = b.first; for(j = 0; j < b.num; j++) { if (cura->time == curb->time && (time == -1 || cura->time < time)) time = cura->time; if(j+1 == b.num) break; curb=curb->next; } if(i+1 == a.num) break; cura=cura->next; } return time; }