hdu 3416(最大流+最短路)

时间:2023-01-01 22:48:29

Marriage Match IV

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

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条边,现在一个人想从 A点走到 B点,每次都要走最短路,但是最短路上的每一段路都只能走一回,这次走了下次就不能再走这里了,问A->B最多有多少种走法?
题解:假设 u 和 v 是最短路上的点,那么建一幅新图,我们就在 u - v 之间连一条容量为 1的边就好了,然后从A—>B做最大流,但是,如何判断 u - v是最短路上的点呢?所以我们从A点做一次SPFA,求出A点到每一点的距离,然后反向建图,从B点也做相同的操作,如果 dis[A][u] + edge[u][v] + dis1[B][v] = dis[A][B],那么 u - v就是最短路上的点了。边要开200000,因为Dinic算法要建反向边,所以开两倍.
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = ;
const int N = ;
const int M = ;
struct Edge{
int v,w,next;
}edge[M];
int head[N];
int level[N];
int tot;
void init()
{
memset(head,-,sizeof(head));
tot=;
}
void addEdge(int u,int v,int w,int &k)
{
edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
edge[k].v = u,edge[k].w=,edge[k].next=head[v],head[v]=k++;
}
int BFS(int src,int des)
{
queue<int>q;
memset(level,,sizeof(level));
level[src]=;
q.push(src);
while(!q.empty())
{
int u = q.front();
q.pop();
if(u==des) return ;
for(int k = head[u]; k!=-; k=edge[k].next)
{
int v = edge[k].v;
int w = edge[k].w;
if(level[v]==&&w!=)
{
level[v]=level[u]+;
q.push(v);
}
}
}
return -;
}
int dfs(int u,int des,int increaseRoad){
if(u==des||increaseRoad==) {
return increaseRoad;
}
int ret=;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v,w=edge[k].w;
if(level[v]==level[u]+&&w!=){
int MIN = min(increaseRoad-ret,w);
w = dfs(v,des,MIN);
if(w > )
{
edge[k].w -=w;
edge[k^].w+=w;
ret+=w;
if(ret==increaseRoad){
return ret;
}
}
else level[v] = -;
if(increaseRoad==) break;
}
}
if(ret==) level[u]=-;
return ret;
}
int Dinic(int src,int des)
{
int ans = ;
while(BFS(src,des)!=-) ans+=dfs(src,des,INF);
return ans;
}
struct Edge1{
int v,w,next;
}edge1[M],edge2[M];
int head1[N],head2[N];
int tot1,tot2;
int n,m,a,b;
void addEdge1(int u,int v,int w,int &k){
edge1[k].v = v,edge1[k].w=w,edge1[k].next = head1[u],head1[u]=k++;
}
void addEdge2(int u,int v,int w,int &k){
edge2[k].v = v,edge2[k].w=w,edge2[k].next = head2[u],head2[u]=k++;
}
void init1(){
memset(head1,-,sizeof(head1));
tot1 = ;
}
void init2(){
memset(head2,-,sizeof(head2));
tot2 = ;
}
int low[][N];
bool vis[N];
void spfa(int s,int t,int flag,int *head,Edge1 edge[]){
for(int i=;i<=n;i++){
low[flag][i] = INF;
vis[i] = false;
}
low[flag][s] = ;
queue<int >q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v,w=edge[k].w;
if(low[flag][v]>low[flag][u]+w){
low[flag][v] = low[flag][u]+w;
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
init1();
init2();
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(u==v) continue;
addEdge1(u,v,w,tot1);
addEdge2(v,u,w,tot2); ///反向边
}
scanf("%d%d",&a,&b);
spfa(a,b,,head1,edge1); ///a->b 第一遍
spfa(b,a,,head2,edge2); ///b->a 第二遍
init();
for(int i=;i<=n;i++){
for(int j=head1[i];j!=-;j=edge1[j].next){
int v = edge1[j].v,w=edge1[j].w;
if(low[][i]+low[][v]+w==low[][b]){
addEdge(i,v,,tot);
}
}
}
if(low[][b]==INF){
printf("0\n");
continue;
}
int max_flow = Dinic(a,b);
printf("%d\n",max_flow);
}
return ;
}