#include #include #include #include #include using namespace std; #define clr(x)memset(x,0,sizeof(x)) #define min(a,b)(a)<(b)?(a):(b) const int INF=INT_MAX; const int maxn=1005; const int maxm=1000000; struct node { int from,to,next,c; }e[maxm]; int tot; int head[maxn]; void add(int s,int u,int f1,int f2) { e[tot].from=s; e[tot].to=u; e[tot].c=f1; e[tot].next=head[s]; head[s]=tot++; e[tot].from=u; e[tot].to=s; e[tot].c=f2; e[tot].next=head[u]; head[u]=tot++; } int q[maxn]; int cnt[maxn]; int d[maxn]; int low[maxn]; int cur[maxn]; int maxflow(int s,int t,int n) { int *front=q,*rear=q; for(int i=1;i<=n;i++) { d[i]=n; cnt[i]=0; } cnt[n]=n-1; cnt[0]++; d[t]=0; *rear++=t; while(front0) { d[e[i].to]=d[v]+1; cnt[n]--; cnt[d[e[i].to]]++; *rear++=e[i].to; } } } int flow=0, u=s, top=0; low[0]=INF; for(int i=1;i<=n;i++) cur[i]=head[i]; while(d[s]0&&d[u]==d[e[i].to]+1) { low[top+1]=min(low[top],e[i].c); q[++top]=i; u=e[i].to; break; } } if(i!=-1) { if(u==t) { int minf=low[top]; for(int p=1,i;p<=top;++p) { i=q[p]; e[i].c-=minf; e[i^1].c+=minf; } flow+=minf; u=s; low[0]=INF; top=0; } } else { int old_du=d[u]; cnt[old_du]--; d[u]=n-1; for(int i=head[u];i!=-1;i=e[i].next) if(e[i].c>0&&d[u]>d[e[i].to]) d[u]=d[e[i].to]; cnt[++d[u]]++; if(d[u]dis; int d1[maxn]; int d2[maxn]; void dijkstra(int u,int*d,int*hed,EDGE*ed,int&t) { int i; in.xu = u; in.di = 0; d[u] = 0; dis.push(in); while(!dis.empty()) { x = dis.top(); dis.pop(); for(i = hed[x.xu]; i; i = ed[i].next){ in.xu = ed[i].to; in.di = x.di+ed[i].w; if(in.di < d[in.xu]){ d[in.xu] = in.di; dis.push(in); } } } } int main() { int T, i; int n, m, st, en; scanf("%d",&T); while(T--) { scanf("%d %d",&n, &m); clr(head2); clr(head3); tt = 1; t3 = 1; int a,b,c; while(m--) { scanf("%d %d %d",&a, &b, &c); add2(a,b,c); add3(b,a,c); } scanf("%d %d",&st,&en); for(i = 1; i <= n; i++) d2[i] = d1[i] = INF; dijkstra(st,d1,head2,edge,tt); dijkstra(en,d2,head3,ee,t3); memset(head,-1,sizeof(head)); tot = 0; for(i = 1; i < tt; i++) { if(d1[edge[i].from]+d2[edge[i].to]+edge[i].w == d1[en]) add(edge[i].from,edge[i].to,1,0); } int res = maxflow(st,en,n); printf("%d\n",res); } return 0; }