Marriage Match IV HDU - 3416(最大流+最短路)

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?
Input
The first line is an integer T indicating the case number.(1<=T<=65)
For each case,there are two integer n and m in the first line ( 2<=n<=1000, 0<=m<=100000 ) ,n is the number of the city and m is the number of the roads.

Then follows m line ,each line have three integers a,b,c,(1<=a,b<=n,0<c<=1000)it means there is a road from a to b and it’s distance is c, while there may have no road from b to a. There may have a road from a to a,but you can ignore it. If there are two roads from a to b, they are different.

At last is a line with two integer A and B(1<=A,B<=N,A!=B), means the number of city A and city B.
There may be some blank line between each case.
Output
Output a line with a integer, means the chances starvae can get at most.
Sample Input
3
7 8
1 2 1
1 3 1
2 4 1
3 4 1
4 5 1
4 6 1
5 7 1
6 7 1
1 7

6 7
1 2 1
2 3 1
1 3 3
3 4 1
3 5 1
4 6 1
5 6 1
1 6

2 2
1 2 1
1 2 2
1 2
Sample Output
2
1
1
给定一张有向图,求最短路径的条数,源点S和终点T在最后一行给出。
大概思路是:
正向反向分别求一次SPFA,然后将正向图遍历一遍,对于每一条边(u,v)->c,如果正向最短路dis1[u]+反向最短路dis2[v]+边权值c=正向最短路dis1[T],则这条路径就是最短路的一条路径,将其进行网络流建边,从u到v,容量为1,最后求一遍S到T的最大流即可。
这里用的ISAP求最大流

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=1e5+5;
const int maxm=1e5+5;
const int INF=0x3f3f3f3f;
int n,m,head1[maxn],head2[maxn],ne1,ne2,out[maxn],vis[maxn],dis1[maxn],dis2[maxn],s,ans;
int head[maxn],cur[maxn],dep[maxn],ne3,pre[maxn],num[maxn],maxflow;
int S,T;
struct edge
{
	int to,cost,next;
}e1[maxm],e2[maxm],e[maxm*2];
void add1(int u,int v,int w)
{
	e1[ne1].to=v;
	e1[ne1].cost=w;
	e1[ne1].next=head1[u];
	head1[u]=ne1++;
	e2[ne2].to=u;
	e2[ne2].cost=w;
	e2[ne2].next=head2[v];
	head2[v]=ne2++;
}
void add2(int u,int v,int w)
{
	e[ne3].to=v;
	e[ne3].cost=w;
	e[ne3].next=head[u];
	head[u]=ne3++;
	e[ne3].to=u;
	e[ne3].cost=0;
	e[ne3].next=head[v];
	head[v]=ne3++;
}
void init()
{
	ne1=0;
	memset(head1,-1,sizeof(head1));
	ne2=0;
	memset(head2,-1,sizeof(head2));
	memset(dis1,0x3f,sizeof(dis1));
	memset(dis2,0x3f,sizeof(dis2));
	memset(head,-1,sizeof(head));
	ne3=0;
	memset(num,0,sizeof(num));
	maxflow=0;
}
void spfa(int x,int f)
{
	memset(vis,0,sizeof(vis));
	queue<int> que;
	while(!que.empty()) que.pop();
	que.push(x);
	vis[x]=1;
	if(f) dis1[x]=0;
	else dis2[x]=0;
	while(!que.empty())
	{
		int u=que.front();
		que.pop();
		vis[u]=0;
		if(f==1)
		{
			for(int i=head1[u];i!=-1;i=e1[i].next)
			{
				int v=e1[i].to;
				if(dis1[v]>dis1[u]+e1[i].cost)
				{
					dis1[v]=dis1[u]+e1[i].cost;
					if(!vis[v])
					{
						vis[v]=1;
						que.push(v);
					}
				}
			}
		}
		else
		{
			for(int i=head2[u];i!=-1;i=e2[i].next)
			{
				int v=e2[i].to;
				if(dis2[v]>dis2[u]+e2[i].cost)
				{
					dis2[v]=dis2[u]+e2[i].cost;
					if(!vis[v])
					{
						vis[v]=1;
						que.push(v);
					}
				}
			}
		}
	}
}

queue<int> q;
void bfs(int t)
{
	while(!q.empty()) q.pop();
	for(int i=1;i<=n;i++) cur[i]=head[i];
	for(int i=1;i<=n;i++) dep[i]=n;
	dep[t]=0;
	q.push(t);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			if(dep[e[i].to]==n&&e[i^1].cost)
			{
				dep[e[i].to]=dep[u]+1;
				q.push(e[i].to);
			}
		}
	}
}
int addflow(int s,int t)
{
	int ans=INF,u=t;
	while(u!=s)
	{
		ans=min(ans,e[pre[u]].cost);
		u=e[pre[u]^1].to;
	}
	u=t;
	while(u!=s)
	{
		e[pre[u]].cost-=ans;
		e[pre[u]^1].cost+=ans; 
		u=e[pre[u]^1].to;
	}
	return ans;
}
void ISAP(int s,int t)
{
	int u=s;
	bfs(t);
	for(int i=1;i<=n;i++) num[dep[i]]++;
	while(dep[s]<n)
	{
		if(u==t) 
		{
			maxflow+=addflow(s,t);
			u=s;
		}
		int flag=0;
		for(int &i=cur[u];i!=-1;i=e[i].next)
		{
			if(dep[u]==dep[e[i].to]+1&&e[i].cost)
			{
				flag=1;
				pre[e[i].to]=i;
				u=e[i].to;
				break;
			}
		}
		if(!flag)
		{
			int minn=n-1;
			for(int i=head[u];i!=-1;i=e[i].next)
			{
				if(e[i].cost)
				{
					minn=min(minn,dep[e[i].to]);
				}
			}
			if((--num[dep[u]])==0) break;
			dep[u]=minn+1;
			num[dep[u]]++;
			cur[u]=head[u];
			if(u!=s) u=e[pre[u]^1].to;
		}
	}
}
void Find()
{
	while(!q.empty()) q.pop();
	memset(vis,0,sizeof(vis));
	vis[S]=1;
	q.push(S);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head1[u];i!=-1;i=e1[i].next)
		{
			int v=e1[i].to;
			if(dis1[u]+dis2[v]+e1[i].cost==dis1[T])
			{
				add2(u,v,1);
			}
			if(!vis[v])
			{
				vis[v]=1;
				q.push(v);
			}
		}
	}
}
int main()
{
	int tt;
	scanf("%d",&tt);
	while(tt--)
	{
		init();
		scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			add1(x,y,z);
		}
		scanf("%d%d",&S,&T);
		spfa(S,1);
		spfa(T,0);
		Find();
		ISAP(S,T);
		printf("%d\n",maxflow);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值