HDU 3416 Marriage Match IV(最短路,网络流)

时间:2021-10-18 03:47:19

题面

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?

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.

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

题解

题目大意:

有一个人,特别爱撩妹,现在他在A城市,妹子们在B城市,每次他会从A城市沿着最短的路径到达B城市,并且和一个妹子约会,他每条路只能够走一次,问他最多能够和几个妹子约会?

题解:

首先要确定所有的最短路径上的路,直接用SPFA即可解决(怎么弄自己想)

然后重新连接最短路上的路径,流量为1,求出源点到汇点的最大流即可

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 2000
#define MAXL 300100
#define INF 1000000000
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-'){t=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*t;
}
struct Line
{
int v,next,w;
}e[MAXL],E[MAXL];
struct edge
{
int v,next,w,fb;
}ee[MAXL];
int hh[MAX],cntt;
int S,T,N,M;
int h[MAX],cnt;
int H[MAX];
inline void Add(int u,int v,int w)
{
e[cnt]=(Line){v,h[u],w};
E[cnt]=(Line){u,H[v],w};
h[u]=H[v]=cnt++;
}
int dis1[MAX],dis2[MAX];
bool vis[MAX];
int level[MAX];
void SPFA1()
{
for(int i=1;i<=N;++i)vis[i]=false;
for(int i=1;i<=N;++i)dis1[i]=INF;
dis1[S]=0;
queue<int> Q;while(!Q.empty())Q.pop();
Q.push(S);
while(!Q.empty())
{
int u=Q.front();Q.pop();
vis[u]=false;
for(int i=h[u];i;i=e[i].next)
{
int v=e[i].v,w=e[i].w;
if(dis1[v]>dis1[u]+w)
{
dis1[v]=dis1[u]+w;
if(!vis[v])
{
vis[v]=true;
Q.push(v);
}
}
}
}
}
void SPFA2()
{
for(int i=1;i<=N;++i)vis[i]=false;
for(int i=1;i<=N;++i)dis2[i]=INF;
dis2[T]=0;
queue<int> Q;while(!Q.empty())Q.pop();
Q.push(T);
while(!Q.empty())
{
int u=Q.front();Q.pop();
vis[u]=false;
for(int i=H[u];i;i=E[i].next)
{
int v=E[i].v,w=E[i].w;
if(dis2[v]>dis2[u]+w)
{
dis2[v]=dis2[u]+w;
if(!vis[v])
{
vis[v]=true;
Q.push(v);
}
}
}
}
}
inline void ReAdd(int u,int v,int w)
{
ee[cntt]=(edge){v,hh[u],w,cnt+1};
hh[u]=cntt++;
ee[cntt]=(edge){u,hh[v],0,cnt-1};
hh[v]=cntt++;
}
inline void ReBuild()
{
for(int i=1;i<=N;++i)
{
for(int j=h[i];j;j=e[j].next)
{
if(dis1[i]+e[j].w+dis2[e[j].v]==dis1[T])
ReAdd(i,e[j].v,1);
}
}
}
inline bool BFS()
{
for(int i=1;i<=N;++i)level[i]=0;
level[S]=1;
queue<int> Q;while(!Q.empty())Q.pop();
Q.push(S);
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=hh[u];i;i=ee[i].next)
{
int v=ee[i].v;
if(ee[i].w&&!level[v])
{
level[v]=level[u]+1;
Q.push(v);
}
} }
return level[T];
}
int DFS(int u,int f)
{
if(u==T||f==0)return f;
int re=0;
for(int i=hh[u];i;i=ee[i].next)
{
int v=ee[i].v;
if(ee[i].w&&level[v]==level[u]+1)
{
int d=DFS(v,min(f,ee[i].w));
f-=d;re+=d;
ee[i].w-=d;ee[ee[i].fb].w+=d;
}
}
return re;
}
inline int Dinic()
{
int re=0;
while(BFS())
re+=DFS(S,INF);
return re;
}
int main()
{
int TT=read();
while(TT--)
{
cnt=cntt=1;
N=read();M=read();
for(int i=1;i<=N;++i)h[i]=H[i]=hh[i]=0;
for(int i=1;i<=M;++i)
{
int a=read(),b=read(),c=read();
if(a!=b)
Add(a,b,c);
}
S=read();T=read();
SPFA1();
SPFA2();
ReBuild();
printf("%d\n",Dinic());
}
return 0;
}