您的位置:首页 > 其它

hdu3416 Marriage Match IV(最短路+最大流)

2018-01-12 11:01 239 查看


Marriage Match IV

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4743    Accepted Submission(s): 1420


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

题意:有n个城市,m条边,每条边表示u到v花费时间w,问从s到t花费时间最小的情况下有多少种方案

问花费时间最小,我们可以先求出以s为到t的最短时间然后把无用边删去,再求一下最大流即可

关于删边:记最小花费为cost

可以先以s为源点,求最短路,此时数组记为dis1

再以t为源点,求最短路,此时数组记为dis2

当dis1[u]+e[u][i]+dis2[i]==cost时,这条边为可用边

最后这题比较卡时间(还是kuangbin的
e441
sap板子快啊)

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
const int N=2000+20;
const int M=2000000+20;
int top;
int h
,pre
,g
,first
,cur
;//h[i]记录每个节点的高度,pre[i]记录前驱,g[i]表示距离为i个节点数有多少个
struct node
{
int v,next,cap;
} E[M];
void init()
{
mem(first,-1);
top=0;
}
void add_edge(int u,int v,int c)
{
E[top].v=v;E[top].cap=c;E[top].next=first[u];first[u]=top++;
E[top].v=u;E[top].cap=0;E[top].next=first[v];first[v]=top++;
}
int sap(int start,int end,int nodenum)
{
memset(h,0,sizeof(h));
memset(g,0,sizeof(g));
memcpy(cur,first,sizeof(first));
int u=pre[start]=start,maxflow=0,aug=-1;
g[0]=nodenum;
while(h[start]<nodenum)
{
loop:
for(int &i=cur[u];i!=-1;i=E[i].next)
{
int v=E[i].v;
if(E[i].cap&&h[u]==h[v]+1)
{
if(aug==-1||aug>E[i].cap)
aug=E[i].cap;
pre[v]=u;
u=v;
if(v==end)
{
maxflow+=aug;
for(u=pre[u];v!=start;v=u,u=pre[u])
{
E[cur[u]].cap-=aug;
E[cur[u]^1].cap+=aug;
}
aug=-1;
}
goto loop;
}
}
int mindis=nodenum;
for(int i=first[u];i!=-1;i=E[i].next)
{
int v=E[i].v;
if(E[i].cap&&mindis>h[v])
{
cur[u]=i;
mindis=h[v];
}
}
if((--g[h[u]])==0)break;
g[h[u]=mindis+1]++;
u=pre[u];
}
return maxflow;
}

struct point
{
int x,w;
point() {}
point(int tx,int tw)
{
x=tx,w=tw;
};
bool friend operator <(point a,point b)
{
return a.w>b.w;
}

};
struct edge
{
int v,w,next;
} e[M];
int kcase,m,k,n,x[M],y[M],w[M],z,s,t,head
,tt,c
,vis
;
struct solve
{
int dis
;
void init()
{
mem(head,-1);
tt=0;
}
void add_edge(int u,int v,int w)
{
e[tt].v=v;
e[tt].w=w;
e[tt].next=head[u];
head[u]=tt++;
}
bool spfa(int s)
{
priority_queue<point>q;
for(int i=1; i<=n; i++)
c[i]=0,vis[i]=0,dis[i]=INF;
q.push(point(s,0));
vis[s]=1;
dis[s]=0;
c[s]++;
while(!q.empty())
{
point u=q.top();
q.pop();
vis[u.x]=false;
for(int i=head[u.x];i!=-1;i=e[i].next)
{

int v=e[i].v;
if(dis[v]>dis[u.x]+e[i].w)
{
dis[v]=dis[u.x]+e[i].w;
if(!vis[v])
{
c[v]++;
if(c[v]>n)return false;
vis[v]=1;
q.push(point(e[i].v,dis[v]));
}
}
}
}
return true;
}
};
int main()
{
scanf("%d",&kcase);
while(kcase--)
{

scanf("%d%d",&n,&m);
solve a,b;
a.init();
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&x[i],&y[i],&w[i]);
a.add_edge(x[i],y[i],w[i]);
}
scanf("%d%d",&s,&t);
a.spfa(s);

b.init();
for(int i=1; i<=m; i++)
b.add_edge(y[i],x[i],w[i]);
b.spfa(t);
init();
for(int i=1; i<=m; i++)
if(x[i]!=y[i]&&a.dis[x[i]]+b.dis[y[i]]+w[i]==a.dis[t])
add_edge(x[i],y[i],1);
printf("%d\n",sap(s,t,n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: