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

题目大意:在城市A的男孩想去城市B的女孩,不过他去城市B必须走最短路,并且走过的路不可以再走,问他最多能看这个女孩多少次。

 

分析:因为这个男孩直走最短路,所以我们必须求出来所有最短路径上的路,怎么判断一条路是否属于最短路经上的呢?其实比较容易的,只要先求出来从A到达所有点的最短路distA[x], 然后再求出来所有点到B的最短路distB[y](添加反边从B开始即可求出),如果x-y之间有一条路,那么只需要判断distA[x]+distB[y]+w(x,y) == distA[B] 是否成立即可。然后把这条边加入新图中,流量置为1(因为只可以走1次),求出来从源点到汇点的最大流就是所求结果。

注意:可能会爆栈。

下面是AC代码。

========================================================================================================================

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
using namespace std;

const int MAXN = 1e6+7;
const int MAXM = 1005;
const int oo = 1e9+7;
///分别存正向图,反向图,和重构图
struct Edge{int v, val, next;}ss[MAXN], ee[MAXN], edge[MAXN<<1];
int Hs[MAXM], He[MAXM], Head[MAXM], cnt_s, cnt_e, cnt;
int DistS[MAXM], DistE[MAXM];///分别是从源点到达每点的距离,和每点到达源点的距离
int Layer[MAXM];///分层

void InIt(int N)
{
    cnt = cnt_e = cnt_s = 0;

    memset(Hs, -1, sizeof(Hs));
    memset(He, -1, sizeof(He));
    memset(Head, -1, sizeof(Head));

    for(int i=1; i<=N; i++)
        DistE[i] = DistS[i] = oo;
}
void AddEdge(Edge e[], int head[], int &ct, int u, int v, int val)
{
    e[ct].v = v;
    e[ct].val = val;
    e[ct].next = head[u];
    head[u] = ct++;
}
void spfa(Edge e[], int head[], int dist[], int start)
{///求最短路
    stack<int> sta;
    bool instack[MAXM] = {0};
    dist[start] = 0;
    sta.push(start);

    while(sta.size())
    {
        int u = sta.top();sta.pop();
        instack[u] = false;

        for(int j=head[u]; j!=-1; j=e[j].next)
        {
            int v = e[j].v;

            if(dist[v] > dist[u]+e[j].val)
            {
                dist[v] = dist[u] + e[j].val;

                if(instack[v] == false)
                {
                    instack[v] = true;
                    sta.push(v);
                }
            }
        }
    }
}
void BuildGraph(int u, int MinLen)
{///遍历所有的边,把输入最短路的边加入新图中
    for(int j=Hs[u]; j!=-1; j=ss[j].next)
    {
        int v = ss[j].v;

        if(v != -1)
        {
            if(DistS[u] + DistE[v] + ss[j].val == MinLen)
            {
                AddEdge(edge, Head, cnt, u, v, 1);
                AddEdge(edge, Head, cnt, v, u, 0);
            }

            ss[j].v = -1;

            BuildGraph(v, MinLen);
        }
    }
}
bool BFS(int start, int End)
{
    memset(Layer, 0, sizeof(Layer));
    Layer[start] = 1;
    queue<int> Q;
    Q.push(start);

    while(Q.size())
    {
        int u = Q.front();Q.pop();

        if(u == End)return true;

        for(int j=Head[u]; j!=-1; j=edge[j].next)
        {
            int v = edge[j].v;

            if(Layer[v] == false && edge[j].val)
            {
                Layer[v] = Layer[u] + 1;
                Q.push(v);
            }
        }
    }

    return false;
}
int DFS(int u, int MaxFlow, int End)
{
    if(u == End)return MaxFlow;

    int uflow = 0;

    for(int j=Head[u]; j!=-1; j=edge[j].next)
    {
        int v = edge[j].v;

        if(Layer[u]+1 == Layer[v] && edge[j].val)
        {
            int flow = min(MaxFlow-uflow, edge[j].val);
            flow = DFS(v, flow, End);

            edge[j].val -= flow;
            edge[j^1].val += flow;
            uflow += flow;

            if(uflow == MaxFlow)
                break;
        }
    }

    if(uflow == 0)
        Layer[u] = 0;

    return uflow;
}
int Dinic(int start, int End)
{
    int MaxFlow = 0;

    while(BFS(start, End) == true)
        MaxFlow += DFS(start, oo, End);

    return MaxFlow;
}

int main()
{
    int T;

    scanf("%d", &T);

    while(T--)
    {
        int N, M, u, v, val;

        scanf("%d%d", &N, &M);
        InIt(N);

        while(M--)
        {
            scanf("%d%d%d", &u, &v, &val);

            if(u == v)continue;

            AddEdge(ss, Hs, cnt_s, u, v, val);
            AddEdge(ee, He, cnt_e, v, u, val);
        }

        int start, End;

        scanf("%d%d", &start, &End);

        spfa(ss, Hs, DistS, start);///求源点到所有点的距离
        spfa(ee, He, DistE, End);///求所有点到汇点的最短距离
        BuildGraph(start, DistS[End]);///构建新图

        printf("%d\n", Dinic(start, End));
    }

    return 0;
}
时间: 2024-12-09 20:49:19

O - Marriage Match IV - hdu 3416(最短路+最大流)的相关文章

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

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

Marriage Match IV Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 3416 64-bit integer IO format: %I64d      Java class name: Main Do not sincere non-interference.Like that show, now starvae also take part in

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

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

Marriage Match IV http://acm.hdu.edu.cn/showproblem.php?pid=3416 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6081    Accepted Submission(s): 1766 Problem Description Do not sincere non-inter

HDU3605:Marriage Match IV

Marriage Match IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6230    Accepted Submission(s): 1804 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3416 Description: Do not sincere non-interfe

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