HDU 3416 Marriage Match IV(最短路,网络流)
题面
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(最短路,网络流)的更多相关文章
- HDU 3416 Marriage Match IV (最短路建图+最大流)
(点击此处查看原题) 题目分析 题意:给出一个有n个结点,m条单向边的有向图,问从源点s到汇点t的不重合的最短路有多少条,所谓不重复,意思是任意两条最短路径都不共用一条边,而且任意两点之间的边只会用一 ...
- HDU 3416 Marriage Match IV (最短路径,网络流,最大流)
HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...
- hdu 3416 Marriage Match IV (最短路+最大流)
hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...
- HDU 3416 Marriage Match IV (求最短路的条数,最大流)
Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...
- Marriage Match IV(最短路+网络流)
Marriage Match IV http://acm.hdu.edu.cn/showproblem.php?pid=3416 Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 3416 Marriage Match IV(ISAP+最短路)题解
题意:从A走到B,有最短路,问这样不重复的最短路有几条 思路:先来讲选有效边,我们从start和end各跑一次最短路,得到dis1和dis2数组,如果dis1[u] + dis2[v] + cost[ ...
- HDU 3416 Marriage Match IV 【最短路】(记录路径)+【最大流】
<题目链接> 题目大意: 给你一张图,问你其中没有边重合的最短路径有多少条. 解题分析: 建图的时候记得存一下链式后向边,方便寻找最短路径,然后用Dijkstra或者SPFA跑一遍最短路, ...
- hdu 3416 Marriage Match IV 【 最短路 最大流 】
求边不可重复的最短路条数 先从起点到终点用一次dijkstra,再从终点到起点用一次dijkstra,来判断一条边是否在最短路上 如果在,就将这条边的两个端点连起来,容量为1 再跑一下dinic(), ...
- HDU 3416 Marriage Match IV dij+dinic
题意:给你n个点,m条边的图(有向图,记住一定是有向图),给定起点和终点,问你从起点到终点有几条不同的最短路 分析:不同的最短路,即一条边也不能相同,然后刚开始我的想法是找到一条删一条,然后光荣TLE ...
随机推荐
- sparksql工程小记
最近做一个oracle项目迁移工作,跟着spark架构师学着做,进行一些方法的总结. 1.首先,创建SparkSession对象(老版本为sparkContext) val session = Spa ...
- 腾讯IVWEB前端工程化工具feflow思考与实践
本篇文章主要介绍腾讯IVWEB团队从0到1在工程化的思考和实践.feflow的全称是Front-end flow(前端工作流),致力于提升研发效率和规范的工程化解决方案.愿景是通过feflow,可以使 ...
- Go生成easyjson文件
[生成easyjson文件] cd services/api_adapter/aliafp #先删除已有的aliafp_easyjson.go文件,并且把除了aliafp.go以外的其他文件移动到 ...
- php 下载保存文件保存到本地的两种实现方法
这里的下载,指的是 弹出下载提示框. 第一种: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php function downfile() { $filename ...
- Linux下绝对经典的命令
1.使用远程终端时,可以使用如下命令: screen tmux 2.下载文件可以使用如下命令: curl wget 3.压缩解压缩可以使用: tar .zip.rar 4.使用抓包工具 tcpdump ...
- 使用matlab生成sine波mif文件
使用matlab生成sine波mif文件 作者:lee神 在使用altera 的FPGA中的rom过程中常常会使用到.mif文件或.hex文件.对于初学者,无论mif还是hex都是很令人疑惑的东西,这 ...
- MongoDB入门系列(四):权限管理
一.概述 本篇文章主要介绍如何创建用户和角色相关概念,同时对角色的添加和删除做了相关介绍. 版本:3.6.2 二.角色相关概念 1.数据库用户角色 read:该角色拥有数据的只读权限,系统集合以及sy ...
- VB 如何调用 c++ DLL?
``` ```Class MainWindow 'ByVal 值传递 ByRef 引用传递 'Function 有返回值 Sub 无返回值 'C语言数据类型在VisualBasic中声明为调用时使用的 ...
- NJU 1010 Air
思路:把那张图打表(吐血...),然后就按照规则输出就行. AC代码 #include <cstdio> #include <cmath> #include <cctyp ...
- postman模拟HttpPost请求的方法
开始想装postman的Google浏览器插件的,但是发现应用商店无法搜索,下载的拖进扩展也装不上... 于是找到了这个绿色版的Postman桌面程序!有需要的可以下载,点击下载:http://dow ...