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 ...
随机推荐
- Redis Sentinel安装与部署,实现redis的高可用
前言 对于生产环境,高可用是避免不了要面对的问题,无论什么环境.服务,只要用于生产,就需要满足高可用:此文针对的是redis的高可用. 接下来会有系列文章,该系列是对spring-session实现分 ...
- Windows使用问题总结
1 电脑休眠恢复之后无法识别Wifi无线网络 首先,重启电脑:其次,打开网络和共享中心,点击更改适配器设置:最后,在对应的无线网络连接图标上点击鼠标右键,属性,配置,电源选项,允许计算机关闭此设备以节 ...
- 洛谷P1854 花店橱窗布置 分析+题解代码
洛谷P1854 花店橱窗布置 分析+题解代码 蒟蒻的第一道提高+/省选-,纪念一下. 题目描述: 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定 ...
- docker swarm集群搭建
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: swarm是docker原生的集群管理软件,与kuberne ...
- HBuilder常用快捷键
切换tab: Ctrl+Tab全部保存: Ctrl+Shift+S 激活代码助手: Alt+/显示方法参数提示: Alt+Shift+?转到定义: Ctrl+Alt+D 开启关闭注释整行: Ctrl+ ...
- 树莓派系列教程:1.环境与系统,无显示器无键盘无网线联网并使用PuTTy与VNC图形界面远程登录
本文所需物品清单: Raspberry Pi 3 Model B 主板.SD卡与读卡器(用于烧录系统) 资料整理来源在文尾 需要下载的资源与工具: 推荐系统-Raspbian 树莓派官方深度定制的硬件 ...
- 浅谈session,cookie,sessionStorage,localStorage的区别及应用场景
浏览器的缓存机制提供了可以将用户数据存储在客户端上的方式,可以利用cookie,session等跟服务端进行数据交互. 一.cookie和session cookie和session都是用来跟踪浏览器 ...
- 第十九章 Django的ORM映射机制
第十九章 Django的ORM映射机制 第一课 Django获取多个数据以及文件上传 1.获取多选的结果(checkbox,select/option)时: req.POST.getlist('fav ...
- AssetBundle实现服务器下载并从本地读取
废话不多说 直接上代码. 从服务器下载的, 很简单 private IEnumerator Start() { byte[] ab = null; int len = 0; WWW www =nul ...
- ACM==迷茫
写给迷茫的自己~~ 从家里来学校一周多了,没做几个题,也没学习新的算法,就这样迷茫地无所事事.有时我就在想我是不是真的喜欢算法?曾经自己定下的竞赛目标要置之不理吗? 我高中毕业于一个普通高中,在上大学 ...