最短路+最大流

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=+;
const int MAXN=+;
const int INF=0x7FFFFFFF; struct Edge
{
int from,to,cap,flow;
};
vector<Edge>edges;
vector<int>G[maxn];
struct EE1
{
int from,to,w;
} ee1[MAXN];
struct EE2
{
int from,to,w;
} ee2[MAXN];
vector<EE1>SPFAG1[maxn];
vector<EE2>SPFAG2[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
int U[MAXN],V[MAXN],C[MAXN];
int FF1[maxn],Dis1[maxn],FF2[maxn],Dis2[maxn];
int n,m,s,t,N,M; //求出层次网络
bool BFS()
{
memset(vis,,sizeof(vis));
queue<int>Q;
Q.push(s);
d[s]=;
vis[s]=;
while(!Q.empty())
{
int x=Q.front();
Q.pop();
for(int i=; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} //加边
void AddEdge(int from,int to,int cap)
{
Edge r;
r.from=from;
r.to=to;
r.cap=cap;
r.flow=;
edges.push_back(r);
Edge d;
d.from=to;
d.to=from;
d.cap=;
d.flow=;
edges.push_back(d);
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} //每个阶段来一次DFS增广
int DFS(int x,int a)
{
if(x==t||a==) return a;
int flow=,f;
for(int i=cur[x]; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
return flow;
} //多个阶段,多次建立层次网络。
int Maxflow(int ss,int tt)
{
int flow=;
while(BFS())
{
memset(cur,,sizeof(cur));
flow+=DFS(ss,INF);
}
return flow;
} void SPFA1()
{
for(int i=; i<=N; i++) Dis1[i]=INF;
queue<int>Q;
Q.push(s);
FF1[s]=;
Dis1[s]=;
while(!Q.empty())
{
int h=Q.front();
Q.pop();
FF1[h]=;
for(int i=; i<SPFAG1[h].size(); i++)
{
EE1 edge=SPFAG1[h][i];
if(Dis1[h]+edge.w<Dis1[edge.to])
{
Dis1[edge.to]=Dis1[h]+edge.w;
if(FF1[edge.to]==)
{
FF1[edge.to]=;
Q.push(edge.to);
}
}
}
}
} void SPFA2()
{
for(int i=; i<=N; i++) Dis2[i]=INF;
queue<int>Q;
Q.push(t);
FF2[t]=;
Dis2[t]=;
while(!Q.empty())
{
int h=Q.front();
Q.pop();
FF2[h]=;
for(int i=; i<SPFAG2[h].size(); i++)
{
EE2 edge=SPFAG2[h][i];
if(Dis2[h]+edge.w<Dis2[edge.to])
{
Dis2[edge.to]=Dis2[h]+edge.w;
if(FF2[edge.to]==)
{
FF2[edge.to]=;
Q.push(edge.to);
}
}
}
}
} int main()
{
int TT;
scanf("%d",&TT);
while(TT--)
{
scanf("%d%d",&N,&M);
edges.clear();
for(int i=; i<maxn; i++) G[i].clear();
for(int i=; i<maxn; i++) SPFAG1[i].clear();
for(int i=; i<maxn; i++) SPFAG2[i].clear();
for(int i=; i<=M; i++)
{
scanf("%d%d%d",&U[i],&V[i],&C[i]);
ee1[i].from=U[i];
ee1[i].to=V[i];
ee1[i].w=C[i];
ee2[i].from=V[i];
ee2[i].to=U[i];
ee2[i].w=C[i];
SPFAG1[U[i]].push_back(ee1[i]);
SPFAG2[V[i]].push_back(ee2[i]);
}
scanf("%d%d",&s,&t);
SPFA1();SPFA2();
for(int i=; i<=M; i++)
if(Dis1[U[i]]+Dis2[V[i]]+C[i]==Dis1[t])
AddEdge(U[i],V[i],);
printf("%d\n",Maxflow(s,t));
}
return ;
}

HDU 3416 Marriage Match IV的更多相关文章

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

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

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

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

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

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

  4. HDU 3416 Marriage Match IV dij+dinic

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

  5. (中等) HDU 3416 Marriage Match IV,SPFA+SAP。

    Description Do not sincere non-interference. Like that show, now starvae also take part in a show, b ...

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

    题面 Do not sincere non-interference. Like that show, now starvae also take part in a show, but it tak ...

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

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

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

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

  9. HDU 3416 Marriage Match IV (Dijkstra+最大流)

    题意:N个点M条边的有向图,给定起点S和终点T,求每条边都不重复的S-->T的最短路有多少条. 分析:首先第一步需要找出所有可能最短路上的边.怎么高效地求出呢?可以这样:先对起点S,跑出最短路: ...

随机推荐

  1. WPF Image Binding Uri Source 失败解决办法

    在ListView 的ListItem里动态绑定Image. 首先代码写的是没有问题的.但最后运行却无法显示图片.先看代码: 1. XAML部分 代码如下: <ListView x:Name=& ...

  2. c#简单易用的短信发送服务 悠逸企业短信服务

     悠逸企业短信发送服务,是一种比较简单易操作的短信发送服务,使用POST的方式,请求相应地址就可以实现短信发送功能 1 /// <summary> /// 短信发送服务 /// </ ...

  3. tfs 清除缓存,在需要时

    C:\Users\xxx\AppData\Local\Microsoft\Team Foundation\5.0

  4. FD.io VPP 技术Neutron VNF vRouter 实现

    在OpenStack Neutron中主要有三种网络设备,路由器(Router),负载均衡器(LB)以及VPN,其中Router作为基础网络设备起到连接子网到子网.内网到外网的作用.不同子网之间的访问 ...

  5. html音视频标签

    音视频标签是html5标签,分别为<audio></audio>和<video></video>,这两个标签用法大致相同,且都仅在IE9及以上版本和其他 ...

  6. Applet签名

    applet签名 1.生成密匙库 keytool -genkey -keystore mytest.store -alias mytest -validity 365 -keystore 密匙库 -a ...

  7. pull类型消息中间件-消息发布者(一)

    消息集群架构 对于发送方来说的关键几要素 topic 消息的主题,由用户定义.类似于知乎的话题,Producer发送消息的时候需要指定发送到某一个topic下面,Consumer从某一个topic下面 ...

  8. Xssf配合metaspolit使用

    安装xssf download:  svn export http://xssf.googlecode.com/svn/trunk /home/User/xssf install: svn expor ...

  9. 设计模式 单例模式(Singleton) [ 转载2 ]

    设计模式 单例模式(Singleton) [ 转载2 ] @author java_my_life 单例模式的结构 单例模式的特点: 单例类只能有一个实例. 单例类必须自己创建自己的唯一实例. 单例类 ...

  10. Storm中-Worker Executor Task的关系

    Storm在集群上运行一个Topology时,主要通过以下3个实体来完成Topology的执行工作:1. Worker(进程)2. Executor(线程)3. Task 下图简要描述了这3者之间的关 ...