(中等) HDU 3416 Marriage Match IV,SPFA+SAP。
Description
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?
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=;
const int MaxM=;
const int INF=10e8; namespace first
{ struct Edge
{
int to,next,cost;
}; Edge E[MaxM];
int head[MaxN],Ecou;
int vis[MaxN]; void init(int N)
{
Ecou=; for(int i=;i<=N;++i)
{
head[i]=-;
vis[i]=;
}
} void addEdge(int u,int v,int c)
{
E[Ecou].to=v;
E[Ecou].cost=c;
E[Ecou].next=head[u];
head[u]=Ecou++;
} void SPFA(int lowcost[],int N,int start)
{
queue <int> que;
int u,v,c; for(int i=;i<=N;++i)
lowcost[i]=INF;
lowcost[start]=; que.push(start);
vis[start]=; while(!que.empty())
{
u=que.front();
que.pop(); vis[u]=; for(int i=head[u];i!=-;i=E[i].next)
{
v=E[i].to;
c=E[i].cost; if(lowcost[v]>lowcost[u]+c)
{
lowcost[v]=lowcost[u]+c; if(!vis[v])
{
que.push(v);
vis[v]=;
}
}
}
}
} } namespace second
{ struct Edge
{
int to,next,cap,flow;
}; Edge E[MaxM];
int Ecou,head[MaxN];
int gap[MaxN],dis[MaxN],pre[MaxN],cur[MaxN];
int S,T; void init(int N,int _S,int _T)
{
S=_S;
T=_T;
Ecou=; for(int i=;i<=N;++i)
{
head[i]=-;
gap[i]=dis[i]=;
}
} void addEdge(int u,int v,int c,int rc=)
{
E[Ecou].to=v;
E[Ecou].cap=c;
E[Ecou].flow=;
E[Ecou].next=head[u];
head[u]=Ecou++; E[Ecou].to=u;
E[Ecou].cap=rc;
E[Ecou].flow=;
E[Ecou].next=head[v];
head[v]=Ecou++;
} void update(int remm)
{
int u=T; while(u!=S)
{
E[pre[u]].flow+=remm;
E[pre[u]^].flow-=remm;
u=E[pre[u]^].to;
}
} int SAP(int N)
{
for(int i=;i<=N;++i)
cur[i]=head[i]; int u,v,ret=,remm=INF,mindis; u=S;
pre[S]=-;
gap[]=N; while(dis[S]<N)
{
loop:
for(int i=cur[u];i!=-;i=E[i].next)
{
v=E[i].to; if(E[i].cap-E[i].flow && dis[u]==dis[v]+)
{
pre[v]=i;
cur[u]=i;
u=v; if(u==T)
{
for(int i=pre[u];i!=-;i=pre[E[i^].to])
remm=min(remm,E[i].cap-E[i].flow); ret+=remm;
update(remm);
u=S;
remm=INF;
} goto loop;
}
} mindis=N-;
for(int i=head[u];i!=-;i=E[i].next)
if(E[i].cap-E[i].flow && mindis>dis[E[i].to])
{
cur[u]=i;
mindis=dis[E[i].to];
} if(--gap[dis[u]]==)
break; dis[u]=mindis+; ++gap[dis[u]]; if(u!=S)
u=E[pre[u]^].to;
} return ret;
} } int lowcost[MaxN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T;
int N,M;
int A,B;
int a,b,c; scanf("%d",&T); while(T--)
{
scanf("%d %d",&N,&M); first::init(N); {
using namespace first; while(M--)
{
scanf("%d %d %d",&a,&b,&c); addEdge(a,b,c);
} scanf("%d %d",&A,&B); SPFA(lowcost,N,A); second::init(N,A,B); for(int u=;u<=N;++u)
for(int i=head[u];i!=-;i=E[i].next)
if(lowcost[E[i].to]==lowcost[u]+E[i].cost)
second::addEdge(u,E[i].to,);
} {
using namespace second; printf("%d\n",SAP(N));
}
} return ;
}
(中等) HDU 3416 Marriage Match IV,SPFA+SAP。的更多相关文章
- 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(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,跑出最短路: ...
- 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 (最短路径&&最大流)
/*题意: 有 n 个城市,知道了起点和终点,有 m 条有向边,问从起点到终点的最短路一共有多少条.这是一个有向图,建边的时候要注意!!解题思路:这题的关键就是找到哪些边可以构成最短路,其实之前做最短 ...
- HDU 3416 Marriage Match IV dij+dinic
题意:给你n个点,m条边的图(有向图,记住一定是有向图),给定起点和终点,问你从起点到终点有几条不同的最短路 分析:不同的最短路,即一条边也不能相同,然后刚开始我的想法是找到一条删一条,然后光荣TLE ...
随机推荐
- PHP中json序列化后中文的编码显示问题
在接口返回数据中,我们经常会返回json或者xml格式,php的json序列化函数json_encode非常好用,但是默认会把中文编码为ASCII码(注意,很多人认为这是乱码,其实不是),尤其在调试接 ...
- Video Cards
Video Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- 在Javascript中使用protobuf与c++进行通信
环境:Win7_64旗舰版,VS2013 最近在研究Webkit,已经编译成功,接下来就是Javascript与c++如何传输数据,立刻就想到了protobuf,但是谷歌不支持Javascript,百 ...
- iOS客户端开发与Web前端开发
转载自:http://blog.cnbang.net/tech/1813/不知不觉做iOS客户端开发已经半年多了,了解到iOS客户端开发与Web前端开发的一些异同,写一下. 版本升级.用户角度上看,客 ...
- JS-DOM元素灵活查找
用className选择元素 封装成函数 <title>无标题文档</title> <script> /* window.onload=function () { ...
- android sql Cursor
Cursor 是每行的集合. 使用 moveToFirst() 定位第一行. 你必须知道每一列的名称.你必须知道每一列的数据类型.Cursor 是一个随机的数据源. 所有的数据都是通过下标取得. Cu ...
- 注意:MainActivity的oncreate方法里不要再inflate布局了(MainActivity里的点击事件无响应)
activity_main已经通过setContentView(R.layout.activity_main);设置给MainActivity, 不要再inflate出新布局,然后findviewby ...
- 转 图片缓存之内存缓存技术LruCache,软引用
每当碰到一些大图片的时候,我们如果不对图片进行处理就会报OOM异常,这个问题曾经让我觉得很烦恼,后来终于得到了解决,那么现在就让我和大家一起分享一下吧.这篇博文要讲的图片缓存机制,我接触到的有两钟,一 ...
- javascript的事件冒泡,阻止事件冒泡和事件委托, 事件委托是事件冒泡的一个应用。
首先,弄明白js 当中,什么是事件,事件模型在js中是如何设计的.什么是事件冒泡? 什么是“事件冒泡”呢?假设这里有一杯水,水被用某种神奇的方式分成不同颜色的几层.这时,从最底层冒出了一个气泡,气泡会 ...
- Dice Possibility
Dice Possibility 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is possibility of rolling N dice and th ...