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 ...
随机推荐
- kubernetes 集群的安装部署
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 首先kubernetes得官方文档我自己看着很乱,信息很少, ...
- 开源项目-网上公开http代理爬取、简单分类
爬取网上公开免费代理(http/socks),解析入库,可满足需要切换IP的场景(爬虫.投票等)需求. 项目地址: https://github.com/Jwnie/proxyservice 1.采用 ...
- WinFom中经典小游戏(含源码)
最近整理了若干经典的小游戏,无聊时可以打发时间.程序本身不大,练手非常不错,主要是GDI编程,主界面地址如下图所示 源码下载方式 1,关注微信公众号:小特工作室(也可直接扫描签名处二维码) 2,发送: ...
- js “top、clientTop、scrollTop、offsetTop…”
当要做一些与位置相关的插件或效果的时候,像top.clientTop.scrollTop.offsetTop.scrollHeight.clientHeight.offsetParent...看到这么 ...
- Bootstrap表单验证
主要用过两个: jqBootstrapValidation: https://github.com/ReactiveRaven/jqBootstrapValidation bootstrapValid ...
- java中的Collection集合类
随着1998年JDK 1.2的发布,同时新增了常用的Collections集合类,包含了Collection和Map接口.而Dictionary类是在1996年JDK 1.0发布时就已经有了.它们都可 ...
- ActiveRecord的生命周期
ActiveRecord的生命周期,通过方法重写和插入我们需要的业务逻辑来达到我们对程序的控制. 示例: 1,beforeSave() public function beforeSave($inse ...
- 使用canvas编写时间轴插件
使用canvas编写时间轴插件 背景 项目中有一个视频广场的功能,需要一个时间轴类似视频播放中进度条功能一样显示录像情况,并且可以点击.拖动.放大缩小展示时间轴,获取到时间轴的某个时间.原来的时间轴是 ...
- DxPackNet 3.音频捕捉(录音)
用DxpackNet捕捉音频其实很简单 1.初始化控件 IDxMicrophCapture microphone; private void Form1_Load(object sender, Eve ...
- python环境jieba分词的安装
我的python环境是Anaconda3安装的,由于项目需要用到分词,使用jieba分词库,在此总结一下安装方法. 安装说明======= 代码对 Python 2/3 均兼容 * 全自动安装:`ea ...