hdu 3416 Marriage Match IV 【图论-网络流-最短路+最大流(spfa + Dinic)】

6 篇文章 0 订阅
5 篇文章 0 订阅
                                Marriage Match IV
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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分别出发求最短路dist1,dist2.
注意从B求得额时候要反向。
如果dist1[a]+dist2[b]+c==dist1[B].那么这条边就是有用的。。

AC代码:

# include <cstdio>
# include <iostream>
# include <queue>
# include <cstring>
# include <algorithm>

using namespace std;

# define MAXN 1005
# define MAXM 300005
# define INF 1 << 29

struct EDGE
{
    int to;
    int w;
    int next;
}edge[MAXM], g[MAXM];

int tot;
int gog;
int ghead[MAXN];
int head[MAXN];
int dis[MAXN];
int graph[MAXN][MAXN];
int us[MAXM];
int vs[MAXM];
int wss[MAXM];
int vis[MAXN];
int cnt[MAXN];
int dist[MAXN];
int dist1[MAXN];
int dist2[MAXN];

int min(int a, int b)
{
    return a > b ? b : a;
}

void Init()
{
    memset(ghead, -1, sizeof(ghead));
    gog = 0;
}

void DinicInit()
{
    memset(head, -1, sizeof(head));
    tot = 0;
}

void Addedge(int u, int v, int w)
{
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].next = head[u];
    head[u] = tot++;

    edge[tot].to = u;
    edge[tot].w = 0;
    edge[tot].next = head[v];
    head[v] = tot++;
}

bool Bfs(int s, int t)
{
    memset(dis, 0, sizeof(dis));
    queue <int> que;
    dis[s] = 1;
    que.push(s);
    while (!que.empty())
    {
        int u = que.front(); que.pop();

        for (int i = head[u]; i + 1; i = edge[i].next)
        {
            int v = edge[i].to;
            if (!dis[v] && edge[i].w > 0)
            {
                dis[v] = dis[u] + 1;
                if (v == t)
                {
                    return true;
                }
                que.push(v);
            }
        }
    }
    return false;
}

int Dfs(int u, int t, int f)
{
    if (u == t)
    {
        return f;
    }

    int cost = 0;
    for (int i = head[u]; i + 1; i = edge[i].next)
    {
        int v = edge[i].to;
        int w = edge[i].w;
        if (dis[v] == dis[u] + 1 && w > 0)
        {
            int d = Dfs(v, t, min(w, f - cost));
            if (d > 0)
            {
                edge[i].w -= d;
                edge[i ^ 1].w += d;

                cost += d;
                if (cost == f)
                {
                    break;
                }
                else
                {
                    dis[v] = -1;
                }
            }
        }
    }
    return cost;
}

int Dinic(int s, int t)
{
    int maxflow = 0;
    while (Bfs(s, t))
    {
        maxflow += Dfs(s, t, INF);
    }
    return maxflow;
}

void BuildGraph(int u, int v, int w)
{
    g[gog].to = v;
    g[gog].w = w;
    g[gog].next = ghead[u];
    ghead[u] = gog++;
}

bool spfa(int start, int n)
{
    int i;
    for (i = 1; i <= n; i++)
    {
        if (i == start)
        {
            dist[i] = 0;
            vis[i] = 1;
            cnt[i] = 1;
        }
        else
        {
            dist[i] = INF;
            vis[i] = 0;
            cnt[i] = 0;
        }
    }
    queue <int>que;
    que.push(start);
    while (!que.empty())
    {
        int u = que.front(); que.pop();
        vis[u] = 0;
        for (i = ghead[u]; i + 1; i = g[i].next)
        {
            int v = g[i].to;
            if (dist[v] > dist[u] + g[i].w)
            {
                dist[v] = dist[u] + g[i].w;
                if (!vis[v])
                {
                    vis[v] = 1;
                    que.push(v);
                    if (++cnt[v] > n)
                    {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}

int main(void)
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int i, n, m, start, end;
        scanf("%d %d", &n, &m);
        for (i = 0; i < m; i++)
        {
            scanf("%d %d %d", &us[i], &vs[i], &wss[i]);
        }
        scanf("%d %d", &start, &end);
        Init();
        for (i = 0; i < m; i++)
        {
            BuildGraph(us[i], vs[i], wss[i]);
        }
        spfa(start, n);
        memcpy(dist1, dist, sizeof(dist));
        Init();
        for (i = 0; i < m; i++)
        {
            BuildGraph(vs[i], us[i], wss[i]);
        }
        spfa(end, n);
        memcpy(dist2, dist, sizeof(dist));

        DinicInit();
        for (i = 0; i < m; i++)
        {
            int u = us[i];
            int v = vs[i];
            int w = wss[i];
            if (u != v && dist1[u] + dist2[v] + w == dist1[end])
            {
                Addedge(u, v, 1);
            }
        }

        printf("%d\n", Dinic(start, end));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值