题面

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;
}

HDU 3416 Marriage Match IV(最短路,网络流)的更多相关文章

  1. HDU 3416 Marriage Match IV (最短路建图+最大流)

    (点击此处查看原题) 题目分析 题意:给出一个有n个结点,m条单向边的有向图,问从源点s到汇点t的不重合的最短路有多少条,所谓不重复,意思是任意两条最短路径都不共用一条边,而且任意两点之间的边只会用一 ...

  2. HDU 3416 Marriage Match IV (最短路径,网络流,最大流)

    HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...

  3. hdu 3416 Marriage Match IV (最短路+最大流)

    hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...

  4. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  5. Marriage Match IV(最短路+网络流)

    Marriage Match IV http://acm.hdu.edu.cn/showproblem.php?pid=3416 Time Limit: 2000/1000 MS (Java/Othe ...

  6. HDU 3416 Marriage Match IV(ISAP+最短路)题解

    题意:从A走到B,有最短路,问这样不重复的最短路有几条 思路:先来讲选有效边,我们从start和end各跑一次最短路,得到dis1和dis2数组,如果dis1[u] + dis2[v] + cost[ ...

  7. HDU 3416 Marriage Match IV 【最短路】(记录路径)+【最大流】

    <题目链接> 题目大意: 给你一张图,问你其中没有边重合的最短路径有多少条. 解题分析: 建图的时候记得存一下链式后向边,方便寻找最短路径,然后用Dijkstra或者SPFA跑一遍最短路, ...

  8. hdu 3416 Marriage Match IV 【 最短路 最大流 】

    求边不可重复的最短路条数 先从起点到终点用一次dijkstra,再从终点到起点用一次dijkstra,来判断一条边是否在最短路上 如果在,就将这条边的两个端点连起来,容量为1 再跑一下dinic(), ...

  9. HDU 3416 Marriage Match IV dij+dinic

    题意:给你n个点,m条边的图(有向图,记住一定是有向图),给定起点和终点,问你从起点到终点有几条不同的最短路 分析:不同的最短路,即一条边也不能相同,然后刚开始我的想法是找到一条删一条,然后光荣TLE ...

随机推荐

  1. 记录:mac的浏览器访问任何域名、网址都跳转到本地127.0.0.1或固定网址

    新年上班第一天,刚开机就遇到了个小坑,问题是这样,打开浏览器,输入任何网址都跳转到本地的一个项目,该项目在本地Apache配置下,监听的端口是8888,本机访问的形式是127.0.0.1:8888. ...

  2. linux 下创建GRE隧道

    其他国家的互联网如同一个孤岛.要想访问国外网站异常的缓慢,甚至被和谐了.可以建立一条隧道来避免这种情况,下面说说GRE隧道如何建立. 1. GRE介绍 GRE隧道是一种IP-over-IP的隧道,是通 ...

  3. egametang框架服务端运行流程

    et框架的构建块主要由entity和componet组成,类似unity的组件.一个Entity可以挂载多个不同Component.Entity和Component的共同基类Disposer用于提供对 ...

  4. mysql中的范式与范式——读<<高性能mysql>>笔记一

    对于任何给定的数据库通常都有很多表示方法,从完全的范式化到完全的反范式化,以及两者的折中.在范式化的数据库中,每个事实数据会出现并且只出现一次.相反,在反范式化的数据库中,可能会存储在多个地方. 那什 ...

  5. Docker安装weblogic

    Docker容器安装weblogic详细教程 前提:已经安装后Docker,并且能正常使用 (1)获取镜像:  docker pull ismaleiva90/weblogic12 docker pu ...

  6. this指针随笔

    在类中,非常量成员函数中,this指针为指向非常量的常量指针Class* this const 在常量成员函数中,this指针为const class* this const,为指向常量的常量指针

  7. JMeter使用(Linux)

    JMeter是一个Java桌面应用程序,用户界面采用Swing Java Api实现,支持并发和多线程或者线程组的执行,对于配置负载测试和压力测试非常有用.Jmeter是开源.免安装的,只需要有jdk ...

  8. Storm实践

    1.Storm命令 在Linux中观直接输入Storm,不带任何参数信息,可以查看Storm命令. 参考这里 1.  activate 激活指定的拓扑Spout.语法:storm activate t ...

  9. Linux DHCP原理

    DHCP作用 DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络服务供应 ...

  10. 一个可以自由定制外观、支持拖拽消除的MaterialDesign风格Android BadgeView

    为了尊重作者,先放上链接:https://github.com/qstumn/BadgeView BadgeView 一个可以自由定制外观.支持拖拽消除的MaterialDesign风格Android ...