Description
Do not sincere non-interference。
Like that show, now starvae also take part in a show, but it take place between city A and B. Starvae is in city A and girls are in city B. Every time starvae can get to city B and make a data with a girl he likes. But there are two problems with it, one is starvae must get to B within least time, it's said that he must take a shortest path. Other is no road can be taken more than once. While the city starvae passed away can been taken more than once.
So, under a good RP, starvae may have many chances to get to city B. But he don't know how many chances at most he can make a data with the girl he likes . Could you help starvae?
Like that show, now starvae also take part in a show, but it take place between city A and B. Starvae is in city A and girls are in city B. Every time starvae can get to city B and make a data with a girl he likes. But there are two problems with it, one is starvae must get to B within least time, it's said that he must take a shortest path. Other is no road can be taken more than once. While the city starvae passed away can been taken more than once.
So, under a good RP, starvae may have many chances to get to city B. But he don't know how many chances at most he can make a data with the girl he likes . Could you help starvae?
Input
The first line is an integer T indicating the case number.(1<=T<=65)
For each case,there are two integer n and m in the first line ( 2<=n<=1000, 0<=m<=100000 ) ,n is the number of the city and m is the number of the roads.
Then follows m line ,each line have three integers a,b,c,(1<=a,b<=n,0<c<=1000)it means there is a road from a to b and it's distance is c, while there may have no road from b to a. There may have a road from a to a,but you can ignore it. If there are two roads from a to b, they are different.
At last is a line with two integer A and B(1<=A,B<=N,A!=B), means the number of city A and city B.
There may be some blank line between each case.
For each case,there are two integer n and m in the first line ( 2<=n<=1000, 0<=m<=100000 ) ,n is the number of the city and m is the number of the roads.
Then follows m line ,each line have three integers a,b,c,(1<=a,b<=n,0<c<=1000)it means there is a road from a to b and it's distance is c, while there may have no road from b to a. There may have a road from a to a,but you can ignore it. If there are two roads from a to b, they are different.
At last is a line with two integer A and B(1<=A,B<=N,A!=B), means the number of city A and city B.
There may be some blank line between each case.
Output
Output a line with a integer, means the chances starvae can get at most.
Sample Input
3 7 8 1 2 1 1 3 1 2 4 1 3 4 1 4 5 1 4 6 1 5 7 1 6 7 1 1 7 6 7 1 2 1 2 3 1 1 3 3 3 4 1 3 5 1 4 6 1 5 6 1 1 6 2 2 1 2 1 1 2 2 1 2
Sample Output
2 1 1
题意:求最短路的条数。每条边只能用一次,每个点可以经过多次。
解题思路:从源点求最短路,然后建边最大流。
代码:
/* ***********************************************
Author :xianxingwuguan
Created Time :2014/1/17 20:30:25
File Name :4.cpp
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn=10000;
int head[2000],tol,dep[2000],n;
struct node
{
int next,to,from,cap;
}edge[300000];
void add(int u,int v,int cap)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].from=v;
edge[tol].to=u;
edge[tol].cap=0;
edge[tol].next=head[v];
head[v]=tol++;
}
int bfs(int s,int t)
{
int que[maxn],front=0,rear=0;
memset(dep,-1,sizeof(dep));
dep[s]=0;que[rear++]=s;
while(front!=rear)
{
int u=que[front++];front%=maxn;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap>0&&dep[v]==-1)
{
dep[v]=dep[u]+1;
que[rear++]=v;
rear%=maxn;
if(v==t)return 1;
}
}
}
return 0;
}
int dinic(int s,int t)
{
int i,res=0,top;
int Stack[maxn],cur[maxn];
while(bfs(s,t))
{
memcpy(cur,head,sizeof(head));
int u=s;top=0;
while(1)
{
if(u==t)
{
int min=inf,loc;
for(int i=0;i<top;i++)
if(min>edge[Stack[i]].cap)
{
min=edge[Stack[i]].cap;
loc=i;
}
for(int i=0;i<top;i++)
{
edge[Stack[i]].cap-=min;
edge[Stack[i]^1].cap+=min;
}
res+=min;
top=loc;
u=edge[Stack[top]].from;
}
for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)
if(edge[i].cap&&dep[u]+1==dep[edge[i].to])break;
if(cur[u]!=-1)
{
Stack[top++]=cur[u];
u=edge[cur[u]].to;
}
else
{
if(top==0)break;
dep[u]=-1;
u=edge[Stack[--top]].from;
}
}
}
return res;
}
int head1[3000],tot;
struct NODE
{
int next,to,val;
}edge1[3000000];
void add1(int u,int v,int val)
{
edge1[tot].to=v;
edge1[tot].next=head1[u];
edge1[tot].val=val;
head1[u]=tot++;
}
int dis[20000],vis[20000];
void spfa(int s)
{
fill(dis,dis+n+1,inf);
memset(vis,0,sizeof(vis));
queue<int> q;
vis[s]=1;
dis[s]=0;
q.push(s);
while(!q.empty())
{
int u=q.front();
vis[u]=0;
q.pop();
for(int i=head1[u];i!=-1;i=edge1[i].next)
{
int v=edge1[i].to;
if(dis[v]>dis[u]+edge1[i].val)
{
dis[v]=dis[u]+edge1[i].val;
if(vis[v]==0)
{
vis[v]=0;
q.push(v);
}
}
}
}
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int T,m,f,i,j,k,s,t;
scanf("%d",&T);
while(T--)
{
memset(head1,-1,sizeof(head1));tot=0;
memset(head,-1,sizeof(head));tol=0;
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d%d",&i,&j,&k);
if(i==j)continue;
add1(i,j,k);
}
scanf("%d%d",&s,&t);
spfa(s);
// for(i=1;i<=n;i++)cout<<dis[i]<<" ";puts("");
for(i=1;i<=n;i++)
{
for(j=head1[i];j!=-1;j=edge1[j].next)
{
k=edge1[j].to;
if(dis[k]==dis[i]+edge1[j].val)
add(i,k,1);
}
}
// cout<<"tol:"<<tol<<endl;
printf("%d\n",dinic(s,t));
}
return 0;
}