HDU3416 Marriage Match IV(spfa+最大流SAP)

Marriage Match IV

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

Total Submission(s): 2391    Accepted Submission(s): 722

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

[email protected]

Source

HDOJ Monthly Contest – 2010.06.05

题意:有n个city,m条有向路,问一个人从A城市到B城市按最短的路走共有多少条路可走,每条边只能走一次,即每条边只属于一条路。

解题:先用SPFA把所有的点到B的最短距离算出D[],用于启发式找一条增广流路,从u走到的每一个到v ,如果是正向流而非回流,则必须满足D[v]+edg[u->v].d+pathd(A-->u)==mindis(A-->B).

#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define captype int

const int MAXN = 1010;   //点的总数
const int MAXM = 400010;    //边的总数
const int INF = 1<<30;
struct EDG{
    int to,next;
    captype cap,flow;
    int d;
} edg[MAXM];
int eid,head[MAXN];
int gap[MAXN];  //每种距离(或可认为是高度)点的个数
int dis[MAXN];  //每个点到终点eNode 的最短距离
int cur[MAXN];  //cur[u] 表示从u点出发可流经 cur[u] 号边
int pre[MAXN];
int D[MAXN], vist[MAXN], mindis;

void init(){
    eid=0;
    memset(head,-1,sizeof(head));
}
//有向边 三个参数,无向边4个参数
void addEdg(int u,int v,int d,captype rc=0){
    edg[eid].to=v; edg[eid].next=head[u];  edg[eid].d=d;
    edg[eid].cap=1; edg[eid].flow=0; head[u]=eid++;

    edg[eid].to=u; edg[eid].next=head[v];  edg[eid].d=INF;
    edg[eid].cap=rc; edg[eid].flow=0; head[v]=eid++;
}
captype maxFlow_sap(int sNode,int eNode, int n){//n是包括源点和汇点的总点个数,这个一定要注意
    memset(gap,0,sizeof(gap));
    memset(dis,0,sizeof(dis));
    memcpy(cur,head,sizeof(head));
    memset(vist,-1,sizeof(vist));
    pre[sNode] = -1;
    gap[0]=n;
    captype ans=0;  //最大流
    vist[sNode]=0;
    int u=sNode ;
    while(dis[sNode]<n){   //判断从sNode点有没有流向下一个相邻的点
        if(u==eNode){   //找到一条可增流的路
            captype Min=INF ;
            int inser;
            for(int i=pre[u]; i!=-1; i=pre[edg[i^1].to])    //从这条可增流的路找到最多可增的流量Min
            if(Min>edg[i].cap-edg[i].flow){
                Min=edg[i].cap-edg[i].flow;
                inser=i;
            }
            for(int i=pre[u]; i!=-1; i=pre[edg[i^1].to]){
                edg[i].flow+=Min;
                edg[i^1].flow-=Min;  //可回流的边的流量
            }
            ans+=Min;
            u=edg[inser^1].to;
            continue;
        }
        bool flag = false;  //判断能否从u点出发可往相邻点流
        int v;
        for(int i=cur[u]; i!=-1; i=edg[i].next){
            v=edg[i].to;
            if(edg[i].cap>0&&vist[u]+edg[i].d+D[v]!=mindis)//如果是正流,则必须保证是最短的一条路,如果是逆流,表明v点己是在最短路上
                    continue;
            vist[v]=mindis-D[v];
            if( edg[i].cap-edg[i].flow>0 && dis[u]==dis[v]+1){
                flag=true;
                cur[u]=pre[v]=i;
                break;
            }
        }
        if(flag){
            u=v;
            continue;
        }
        //如果上面没有找到一个可流的相邻点,则改变出发点u的距离(也可认为是高度)为相邻可流点的最小距离+1
        int Mind= n;
        for(int i=head[u]; i!=-1; i=edg[i].next){
            v=edg[i].to;
            if(edg[i].cap>0&&vist[u]+edg[i].d+D[v]!=mindis)
                    continue;
            vist[v]=mindis-D[v];
            if( edg[i].cap-edg[i].flow>0 && Mind>dis[v]){
                Mind=dis[v];
                cur[u]=i;
            }
        }

        gap[dis[u]]--;
        if(gap[dis[u]]==0) return ans;  //当dis[u]这种距离的点没有了,也就不可能从源点出发找到一条增广流路径
                                        //因为汇点到当前点的距离只有一种,那么从源点到汇点必然经过当前点,然而当前点又没能找到可流向的点,那么必然断流
        dis[u]=Mind+1;//如果找到一个可流的相邻点,则距离为相邻点距离+1,如果找不到,则为n+1
        gap[dis[u]]++;
        if(u!=sNode) u=edg[pre[u]^1].to;  //退一条边
    }
    return ans;
}
void spfa(int s,int t,int n){
    queue<int>q;
    int inq[MAXN]={0};
    for(int i=1; i<=n; i++)
        D[i]=INF;
    D[t]=0;
    q.push(t);
    while(!q.empty()){
        int u=q.front(); q.pop();
        inq[u]=0;
        for(int i=head[u]; i!=-1; i=edg[i].next)
        if(edg[i].d==INF&&D[edg[i].to]>D[u]+edg[i^1].d){
            D[edg[i].to]=D[u]+edg[i^1].d;
            if(inq[edg[i].to]==0)
                inq[edg[i].to]=1,q.push(edg[i].to);
        }
    }
}
int main(){
    int _case;
    int n,m,u,v,d,S,T;
    scanf("%d",&_case);
    while(_case--){

        scanf("%d%d",&n,&m);
        init();
        while(m--){
            scanf("%d%d%d",&u,&v,&d);
            addEdg(u,v,d);
        }
        scanf("%d%d",&S,&T);
        spfa(S,T,n);
        mindis=D[S];//printf("#%d ",mindis);
        int ans=maxFlow_sap(S,T,n);
        printf("%d\n",ans);
    }
}
时间: 2024-10-15 05:15:06

HDU3416 Marriage Match IV(spfa+最大流SAP)的相关文章

HDU 3416 Marriage Match IV(spfa+最大流)

题目的大体意思是:给你一些有向边让你求出给出的点s,t之间最短路的条数. 两边spfa从s到t,和从t到s然后求出在最短路上的点建一条容量为1的边,然后求出s到t的最大的流量,就是最短路的数目. PS:代码写的姿势不够优美. Marriage Match IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2051    Accept

hdu3416 Marriage Match IV 最短路+ 最大流

此题的大意:给定一幅有向图,求起点到终点(都是固定的)的不同的最短路有多少条.不同的最短路是说不能有相同的边,顶点可以重复.并且图含有平行边. 看了题以后,就想到暴力,但是暴力往往是不可取的.(暴力的最坏情况下的时间复杂度是O(n^3)).我说的暴力是求一次最短路以后,把最短路上的边全部去掉(权值设为INF). 最短路可以有很多条,但是最短路的值只有一个.根据这个,我们可以判断某条边是否在最短路上. 建图(单向边),求最短路,起点就是输入的起点.枚举每一条边,如果满足dist[i]+Map[i]

hdu3416 Marriage Match IV【最短路+最大流】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3416 题目描述:求起点到终点一共有多少完全不同的最短路径(最短路径之间不能有公共边) 思路:先用dijkstra求出起点到终点的最短路,然后用可以成为最短路径的边(dis[终]-dis[始]==权值)建立网络,容量为1.最后只需求出以起点为源点终点为汇点的最大流即可 #include <iostream> #include <ios> #include <iomanip>

hdu3416 Marriage Match IV(最短路+网络流)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3416 题意: 给出含n个点.m条有向边的图,每条边只能走一次,给出起点和终点,求起点到终点的最短路径有多少条. 思路: 题目要求是最短路径,当然需要求出最短路,用Dijkstra就可以了,然后我们需要构造网络流的图.将能组成最短路的边加入图中,容量设为1,注意能组成最短路的边是满足dis[u] + edge[i].dist == dis[v] 的边,其中u是边的起点,v是边的终点,dis[]保存的是

HDU - 3416 Marriage Match IV (最大流)

题目大意:有个人要从A城市去B城市,每条路只允许走一次,问能走几次最短路 解题思路:这题的话,难点就是怎么知道是不是最短路了 首先,先求出到B最短路,这也顺便求出了所有点到B的最短距离 接着求出到A的最短路 这样就能得到两个数组了,假设d1[u]代表u节点到A城市的最短路d2[v]代表v节点到城市B的最短距离 如果满足d1[u] + dis[u][v] + d2[v] == d1[v]的话,那么u,v这条路就属于最短路中的一条边了,那样就可以构边了 得将城市拆成两个点,容量为1 #include

Marriage Match IV (hdu 3416 网络流+spfa最短路)

Marriage Match IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2275    Accepted Submission(s): 691 Problem Description Do not sincere non-interference. Like that show, now starvae also take

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

题目地址:HDU 3416 这道题WA了一天半...最终才发现是我一直习惯性的将isap的表示上界的变量直接表示成sink+1了...但是在这道题里汇点sink不一定是最后一个点...sad... 这题可以有两种方法做,一种是求两次最短路,d1表示所有点到源点的最短距离,再求一次用d2表示所有点到汇点的最短距离.然后根据公式d1[u]+d2[v]+w==d1[sink]判断是否属于最短路中的一条边. 还有一种是只求一次最短路,直接用d[v]==d[u]+w来判断是否是可以到达源点的最短路,如果可

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

HDU 3416 Marriage Match IV 题目链接 题意:给一个有向图,给定起点终点,问最多多少条点可以重复,边不能重复的最短路 思路:边不能重复,以为着每个边的容量就是1了,最大流问题,那么问题只要能把最短路上的边找出来,跑一下最大流即可,判断一条边是否是最短路上的边,就从起点和终点各做一次dijstra,求出最短路距离后,如果一条边满足d1[u] + d2[v] + w(u, v) == Mindist,那么这条边就是了 代码: #include <cstdio> #inclu

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

hdu 3416 Marriage Match IV 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 a