最短路+最大流

#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. this的相关知识

    一如既往,直接上代码: function fn(name,age){ var obj=new Object(); obj.name=name; obj.age=age; obj.talk=functi ...

  2. python学习第一天内容整理

    .cnblogs_code { width: 500px } 一.python 的历史 (摘自百度百科,了解就ok) Python[1]  (英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn ...

  3. github使用方法(一)

    github是目前流行的代码托管网站. github.com是一个网站,它为你提供一个远程版本库(你和你的协作者的工作成果最终提交在这里):同时它也是一个共享平台,你可以在这里找到数不尽的源码. 关于 ...

  4. 难以记住的sql语句

    天,把这篇文章转移到这里,增强一下记忆,找起来也更方便. 导出: mysqldump -u username -p password -h hname dbname tblname > file ...

  5. prototype属性的理解

    1.对象:对象是JS的基本数据类型(原始类型(数字.字符串和布尔值),对象类型).对象是一种复合值:它将很多值(原始值或者其他对象)聚合在一起,可通过名字访问这些值. 2.三类JS对象和两类属性: 内 ...

  6. java web应用程序目录

    WEB-INF是用来存储服务端配置文件信息和在服务端运行的类文件的,它下面的东西不允许客户端直接访问的.

  7. 1、第一个SpringMVC程序

    1.创建如下项目结构 2.在src下的com.springmvc下创建User.java package com.springmvc; public class User { private Stri ...

  8. WireShark 抓取Telnet包

    用Python的Asyncore.dispatcher写了个小服务器,客户端使用telnet连接上去之后一直显示连接丢失,想抓下包看看 抓包结果如下: 服务器在192.168.1.102:8080 端 ...

  9. memo用法总结

    添加 mmo1.Lines.add('新加的一行');//追加一行文字 mmo1.Lines.Insert(1,'新插入一行');//在指定位置插入一行 删除 mmo1.Lines.Delete(1) ...

  10. C#正则分组实例

    static void Main(string[] args) { string str = "大家家家家家家家明天天天天天天天天玩得得得得得得得开心"; Regex reg = ...