HDU 3416 Marriage Match IV
最短路+最大流
#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的更多相关文章
- 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 ...
- HDU 3416 Marriage Match IV dij+dinic
题意:给你n个点,m条边的图(有向图,记住一定是有向图),给定起点和终点,问你从起点到终点有几条不同的最短路 分析:不同的最短路,即一条边也不能相同,然后刚开始我的想法是找到一条删一条,然后光荣TLE ...
- (中等) 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 ...
- HDU 3416 Marriage Match IV(最短路,网络流)
题面 Do not sincere non-interference. Like that show, now starvae also take part in a show, but it tak ...
- HDU 3416 Marriage Match IV 【最短路】(记录路径)+【最大流】
<题目链接> 题目大意: 给你一张图,问你其中没有边重合的最短路径有多少条. 解题分析: 建图的时候记得存一下链式后向边,方便寻找最短路径,然后用Dijkstra或者SPFA跑一遍最短路, ...
- HDU 3416 Marriage Match IV(ISAP+最短路)题解
题意:从A走到B,有最短路,问这样不重复的最短路有几条 思路:先来讲选有效边,我们从start和end各跑一次最短路,得到dis1和dis2数组,如果dis1[u] + dis2[v] + cost[ ...
- HDU 3416 Marriage Match IV (Dijkstra+最大流)
题意:N个点M条边的有向图,给定起点S和终点T,求每条边都不重复的S-->T的最短路有多少条. 分析:首先第一步需要找出所有可能最短路上的边.怎么高效地求出呢?可以这样:先对起点S,跑出最短路: ...
随机推荐
- asp 301代码
<% if request.ServerVariables("HTTP_HOST")="ksfmjy.com" then Response.Status= ...
- jarring type lambda
object IntStateMonad extendsMonad[({type IntState[A] = State[Int, A]})#IntState] {...}This syntax ca ...
- HDU 5572 An Easy Physics Problem (计算几何+对称点模板)
HDU 5572 An Easy Physics Problem (计算几何) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5572 Descripti ...
- Oracle SQL 关键字
1.UID返回标识当前用户的唯一整数SQL> show userUSER 为"GAO"SQL> select username,user_id from dba_use ...
- Pthon修炼5
函数 一.自定义函数 def:表示函数的关键字 函数名:函数的名称 函数体:函数中进行一系列的逻辑计算 参数:为函数体提供数据 返回值:执行完函数return 一个值 例:def count(x ...
- javaScript 比较数字大小
当你需要通过js来比较数字大小的时候千万不要这样 a>b,这样有可能会把a,b当作字符串来比较,最好用函数来转换下 例如: function sorterFunc(a,b){ var aNum= ...
- C#使用LitJson解析JSON(转)
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - Decembe ...
- 伪分布重新格式化hdfs
重新格式化hdfs系统的方法: (1)查看hdfs-ste.xml: <span style="font-size:18px;"><property> &l ...
- Oracle数据库初探
一.安装oracle数据库 步骤:转载一个很不错的文档:http://www.linuxidc.com/Linux/2015-02/113222.htm 注意点:安装的时候会check相关依赖,有些可 ...
- github使用方法(一)
github是目前流行的代码托管网站. github.com是一个网站,它为你提供一个远程版本库(你和你的协作者的工作成果最终提交在这里):同时它也是一个共享平台,你可以在这里找到数不尽的源码. 关于 ...