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

Marriage Match IV

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5225    Accepted Submission(s): 1549


 

Problem 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?

 

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

 题目大意:给你一个图,问给出的源点到给出的汇点中有多少条最短路,这些最短路之间不能有公共边

先从源点跑一遍最短路,再从汇点跑一遍最短路,如果有dis1[u]+dis2[v]+edge[i].val==dis[汇点]则说明这条路在最短路上,然后我们就用这条边来建图跑最大流。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxm=2e5+7;
const int maxn=10010;
const int inf=0x3f3f3f3f;
struct SPFA
{
	int from;
	int to;
	int val;
	int next;
}vec[maxm<<2];
struct Node
{
	int to;
	int capa;
	int next;
}edge[maxm<<2];
int cnt;
int head[maxn];
int head1[maxn];
int cnt1;
int dis[maxn];
int dis1[maxn];
int dis2[maxn];
bool vis[maxn];
int source,sink;
int u[maxm];
int v[maxm];
int w[maxm];
int dep[maxn];
void init()
{
	memset(head1,-1,sizeof(head1));
	memset(head,-1,sizeof(head));
	cnt=0;
	cnt1=0;
	return;
}
void add(int u,int v,int capa)
{
	edge[cnt].to=v;
	edge[cnt].capa=capa;
	edge[cnt].next=head[u];
	head[u]=cnt++;
	edge[cnt].to=u;
	edge[cnt].capa=0;
	edge[cnt].next=head[v];
	head[v]=cnt++;
	return;
}
void add1(int u,int v,int w)
{
	vec[cnt1].from=u;
	vec[cnt1].to=v;
	vec[cnt1].val=w;
	vec[cnt1].next=head1[u];
	head1[u]=cnt1++;
	return;
}
void spfa(int node)
{
	memset(dis,inf,sizeof(dis));
	memset(vis,false,sizeof(vis));
	dis[node]=0;
	vis[node]=true;
	queue<int> que;
	que.push(node);
	while(!que.empty())
	{
		int now=que.front();
		que.pop();
		vis[now]=false;
		for(int i=head1[now];~i;i=vec[i].next)
		{
			int v=vec[i].to;
			if(dis[v]>dis[now]+vec[i].val)
			{
				dis[v]=dis[now]+vec[i].val;
				if(!vis[v])
				{
					vis[v]=true;
					que.push(v);
				}
			}
		}
	}
	return;
}
bool bfs()
{
	queue<int> que;
	que.push(source);
	memset(dep,-1,sizeof(dep));
	dep[source]=0;
	while(!que.empty())
	{
		int node=que.front();
		que.pop();
		for(int i=head[node];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(edge[i].capa>0&&dep[v]==-1)
			{
				dep[v]=dep[node]+1;
				if(v==sink) return true;
				que.push(v);
			}
		}
	}
	return dep[sink]!=-1;
}
int dfs(int node,int minn)
{
	if(node==sink||minn==0)
	{
		return minn;
	}
	int r=0;
	for(int i=head[node];~i;i=edge[i].next)
	{
		int v=edge[i].to;
		if(edge[i].capa>0&&dep[v]==dep[node]+1)
		{
			int tmp=dfs(v,min(edge[i].capa,minn));
			if(tmp>0)
			{
				edge[i].capa-=tmp;
				edge[i^1].capa+=tmp;
				r+=tmp;
				minn-=tmp;
				if(!minn) break;
			}
		}
	}
	if(!r) dep[node]=-1;
	return r;
}
int dinic()
{
	int maxflow=0;
	while(bfs())
	{
		maxflow+=dfs(source,inf);
	}
	return maxflow;
}
int main()
{
	int test;
	scanf("%d",&test);
	while(test--)
	{
		init();
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d",&u[i],&v[i],&w[i]);
			add1(u[i],v[i],w[i]);
		}
		scanf("%d%d",&source,&sink);
		spfa(source);
		memcpy(dis1,dis,sizeof(dis));
		init();
		for(int i=0;i<m;i++)
		{
			add1(v[i],u[i],w[i]);
		}
		spfa(sink);
		memcpy(dis2,dis,sizeof(dis));
		for(int i=0;i<m;i++)
		{
			if(dis1[u[i]]+dis2[v[i]]+w[i]==dis1[sink])
			{
				add(u[i],v[i],1);
			}
		}
		printf("%d\n",dinic());
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/81353652
今日推荐