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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tony5t4rk/article/details/81946986

文章地址:http://henuly.top/?p=789

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

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

题目链接

题目要求最短路径的数量,用Dijkstra跑一边单源最短路,对于图上一条有向边U->V若Dis[U](最短路)+Value(有向边权值)=Dis[V](最短路)则此边在最短路中,将此边流量设置为1添加进另一个图中,最后在新图中跑最大流即可。

AC代码:

#include <bits/stdc++.h>
typedef std::pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;

struct Edge {
    int V, Dis;
    Edge(int _V = 0, int _Dis = 0): V(_V), Dis(_Dis) {}
};

struct Link {
    int V, Weight, Next;
    Link(int _V = 0, int _Weight = 0, int _Next = 0): V(_V), Weight(_Weight), Next(_Next) {}
};

int TCase;
int N, E;

int Dis[maxn];
std::vector<Edge> Adj[maxn];

Link edges[1000005];
int Head[maxn];
int Tot;
int Depth[maxn];
int Current[maxn];

void Addedge(int U, int V, int Weight) {
    Adj[U].push_back(Edge {V, Weight});
}

void Dijkstra(int Start, int End) {
    std::priority_queue<PII, std::vector<PII>, std::greater<PII> > Que;
    memset(Dis, INF, sizeof(Dis));
    Dis[Start] = 0;
    Que.push(std::make_pair(0, Start));
    while (!Que.empty()) {
        PII Keep = Que.top();
        Que.pop();
        int V = Keep.second;
        if (Dis[V] < Keep.first) {
            continue;
        }
        for (int i = 0; i <int(Adj[V].size()); ++i) {
            Edge Temp = Adj[V][i];
            if (Dis[Temp.V] > Dis[V] + Temp.Dis) {
                Dis[Temp.V] = Dis[V] + Temp.Dis;
                Que.push(std::make_pair(Dis[Temp.V], Temp.V));
            }
        }
    }
}

void Init() {
    Tot = 0;
    memset(Head, -1, sizeof(Head));
}

void AddEdge(int U, int V, int Weight, int ReverseWeight = 0) {
    edges[Tot].V = V;
    edges[Tot].Weight = Weight;
    edges[Tot].Next = Head[U];
    Head[U] = Tot++;
    edges[Tot].V = U;
    edges[Tot].Weight = ReverseWeight;
    edges[Tot].Next = Head[V];
    Head[V] = Tot++;
}

bool Bfs(int Start, int End) {
    memset(Depth, -1, sizeof(Depth));
    std::queue<int> Que;
    Depth[Start] = 0;
    Que.push(Start);
    while (!Que.empty()) {
        int Vertex = Que.front();
        Que.pop();
        for (int i = Head[Vertex]; i != -1; i = edges[i].Next) {
            if (Depth[edges[i].V] == -1 && edges[i].Weight > 0) {
                Depth[edges[i].V] = Depth[Vertex] + 1;
                Que.push(edges[i].V);
            }
        }
    }
    return Depth[End] != -1;
}

int Dfs(int Vertex, int End, int NowFlow) {
    if (Vertex == End || NowFlow == 0) {
        return NowFlow;
    }
    int UsableFlow = 0, FindFlow;
    for (int &i = Current[Vertex]; i != -1; i = edges[i].Next) {
        if (edges[i].Weight > 0 && Depth[edges[i].V] == Depth[Vertex] + 1) {
            FindFlow = Dfs(edges[i].V, End, std::min(NowFlow - UsableFlow, edges[i].Weight));
            if (FindFlow > 0) {
                edges[i].Weight -= FindFlow;
                edges[i ^ 1].Weight += FindFlow;
                UsableFlow += FindFlow;
                if (UsableFlow == NowFlow) {
                    return NowFlow;
                }
            }
        }
    }
    if (!UsableFlow) {
        Depth[Vertex] = -2;
    }
    return UsableFlow;
}

int Dinic(int Start, int End) {
    int MaxFlow = 0;
    while (Bfs(Start, End)) {
        for (int i = 1; i <= N; ++i) {
            Current[i] = Head[i];
        }
        MaxFlow += Dfs(Start, End, INF);
    }
    return MaxFlow;
}

int main(int argc, char *argv[]) {
    scanf("%d", &TCase);
    for (int Case = 1, S, T; Case <= TCase; ++Case) {
        Init();
        scanf("%d%d", &N, &E);
        for (int i = 1; i <= N; ++i) {
            Adj[i].clear();
        }
        std::vector<int> U(E + 1, 0), V(E + 1, 0), C(E + 1, 0);
        for (int i = 1; i <= E; ++i) {
            scanf("%d%d%d", &U[i], &V[i], &C[i]);
            Addedge(U[i], V[i], C[i]);
        }
        scanf("%d%d", &S, &T);
        Dijkstra(S, T);
        if (Dis[T] == INF) {
            printf("%d\n", 0);
            continue;
        }
        for (int i = 1; i <= E; ++i) {
            if (Dis[U[i]] + C[i] == Dis[V[i]]) {
                AddEdge(U[i], V[i], 1);
            }
        }
        printf("%d\n", Dinic(S, T));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Tony5t4rk/article/details/81946986
今日推荐