设为首页 收藏本站
查看: 726|回复: 0

[经验分享] (中等) HDU 3416 Marriage Match IV,SPFA+SAP。

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-21 08:11:07 | 显示全部楼层 |阅读模式
  Description




  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?


  

  题意就是求在最短路的基础上有几条路可以到达,但是每条路之间边彼此不能重合。。。

  然后就是先求出最短路来,然后把所以 lowcost[v]==lowcost+cost[v] 的边留下,然后再求最大流就好了。。。。。。



代码如下:



DSC0000.gif DSC0001.gif


#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=5010;
const int MaxM=500005;
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=0;
for(int i=1;i<=N;++i)
{
head=-1;
vis=0;
}
}
void addEdge(int u,int v,int c)
{
E[Ecou].to=v;
E[Ecou].cost=c;
E[Ecou].next=head;
head=Ecou++;
}
void SPFA(int lowcost[],int N,int start)
{
queue <int> que;
int u,v,c;
for(int i=1;i<=N;++i)
lowcost=INF;
lowcost[start]=0;
que.push(start);
vis[start]=1;
while(!que.empty())
{
u=que.front();
que.pop();
vis=0;
for(int i=head;i!=-1;i=E.next)
{
v=E.to;
c=E.cost;
if(lowcost[v]>lowcost+c)
{
lowcost[v]=lowcost+c;
if(!vis[v])
{
que.push(v);
vis[v]=1;
}
}
}
}
}
}
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=0;
for(int i=1;i<=N;++i)
{
head=-1;
gap=dis=0;
}
}
void addEdge(int u,int v,int c,int rc=0)
{
E[Ecou].to=v;
E[Ecou].cap=c;
E[Ecou].flow=0;
E[Ecou].next=head;
head=Ecou++;
E[Ecou].to=u;
E[Ecou].cap=rc;
E[Ecou].flow=0;
E[Ecou].next=head[v];
head[v]=Ecou++;
}
void update(int remm)
{
int u=T;
while(u!=S)
{
E[pre].flow+=remm;
E[pre^1].flow-=remm;
u=E[pre^1].to;
}
}
int SAP(int N)
{
for(int i=1;i<=N;++i)
cur=head;
int u,v,ret=0,remm=INF,mindis;
u=S;
pre[S]=-1;
gap[0]=N;
while(dis[S]<N)
{
loop:
for(int i=cur;i!=-1;i=E.next)
{
v=E.to;
if(E.cap-E.flow && dis==dis[v]+1)
{
pre[v]=i;
cur=i;
u=v;
if(u==T)
{
for(int i=pre;i!=-1;i=pre[E[i^1].to])
remm=min(remm,E.cap-E.flow);
ret+=remm;
update(remm);
u=S;
remm=INF;
}
goto loop;
}
}
mindis=N-1;
for(int i=head;i!=-1;i=E.next)
if(E.cap-E.flow && mindis>dis[E.to])
{
cur=i;
mindis=dis[E.to];
}
if(--gap[dis]==0)
break;
dis=mindis+1;
++gap[dis];
if(u!=S)
u=E[pre^1].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=1;u<=N;++u)
for(int i=head;i!=-1;i=E.next)
if(lowcost[E.to]==lowcost+E.cost)
second::addEdge(u,E.to,1);
}
{
using namespace second;
printf("%d\n",SAP(N));
}
}
return 0;
}
View Code  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-116427-1-1.html 上篇帖子: [Step By Step]在SAP Business Objects Data Services中使用Date Generation生成日期维度数据并导入到S 下篇帖子: SAP中手工配置Delivery打印message
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表