hdu 3416 Marriage Match IV【SPFA+最大流Dinic】

Marriage Match IV

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

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

 

 

Author

starvae@HDU

 

题目大意:

图中有n个点,m条有向边,然后给你起点和终点,问你从起点到终点的最短路数为多少条,需要保证每条路没有重复的边。


思路:


1、对于这样一个理论不难想到:

①我们求一遍单源最短路、

如果有:dis【from】+当前边权值==dis【to】,那么当前边就有可能是整条最短路径中的某一条边,那么当前边就有可能成为某一种走法的一条路径。

②那么我们就先求一遍单源最短路,然后枚举每一条边,如果这条边符合上述条件,那么将这条边加入网络,并设定其流量为1.


2、对于刚刚处理完的一个网络,能够保证从u到v随便怎么走都是一条最短路,那么我们直接跑最大流即可,此时最大流得到的值,就是最大最短路径数。


Ac代码:


#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
struct node2
{
    int v;int w;
}now;
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[1515155],ee[151555];
int divv[1500];
int head[1500];
int head2[1500];
int cur[1500];
int dis[1055];
int vis[1055];
int n,m;
int cont,ss,tt,cont2;
void add2(int from,int to,int w)
{
    ee[cont2].to=to;
    ee[cont2].from=from;
    ee[cont2].w=w;
    ee[cont2].next=head2[from];
    head2[from]=cont2++;
}
void add(int from,int to,int w)
{
    e[cont].from=from;
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void SPFA()
{
    for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f;
    dis[ss]=0;
    vis[ss]=1;
    queue<int >s;
    s.push(ss);
    while(!s.empty())
    {
        int u=s.front();
        s.pop();vis[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(vis[v]==0)
                {
                    s.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}
int makedivv()
{
    memset(divv,0,sizeof(divv));
    divv[ss]=1;
    queue<int >s;
    s.push(ss);
    while(!s.empty())
    {
        int u=s.front();s.pop();
        if(u==tt)return 1;
        for(int i=head2[u];i!=-1;i=ee[i].next)
        {
            int v=ee[i].to;
            int w=ee[i].w;
            if(w&&divv[v]==0)
            {
                divv[v]=divv[u]+1;
                s.push(v);
            }
        }
    }
    return 0;
}
int Dfs(int u,int maxflow,int tt)
{
    if(u==tt)return maxflow;
    int ret=0;
    for(int &i=cur[u];i!=-1;i=ee[i].next)
    {
        int v=ee[i].to;
        int w=ee[i].w;
        if(w&&divv[v]==divv[u]+1)
        {
            int f=Dfs(v,min(maxflow-ret,w),tt);
            ee[i].w-=f;
            ee[i^1].w+=f;
            ret+=f;
            if(ret==maxflow)return ret;
        }
    }
    return ret;
}
void Dinic()
{
    int ans=0;
    while(makedivv()==1)
    {
        memcpy(cur,head2,sizeof(head2));
        ans+=Dfs(ss,INF,tt);
    }
    printf("%d\n",ans);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cont=0;
        cont2=0;
        memset(head2,-1,sizeof(head2));
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            add(x,y,w);
        }
        scanf("%d%d",&ss,&tt);
        SPFA();
        for(int i=0;i<cont;i++)
        {
            int u=e[i].from;
            int v=e[i].to;
            int w=e[i].w;
            if(dis[u]+w==dis[v]&&w)
            {
                add2(u,v,1);
                add2(v,u,0);
            }
        }
        Dinic();
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值