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    Accepted Submission(s): 608

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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-12
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3ffffff
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

using namespace std;

const int maxn = 2010;

int cnt;
int n, m;
int cur[maxn], head[maxn];
int dis[maxn], gap[maxn];
int aug[maxn], pre[maxn];

struct node
{
    int v, w;
    int next;
} f[510000];

int head1[maxn];
int head2[maxn];
int cnt1;
int cnt2;
struct node1
{
    int u, v, w;
    int next;
};
node1 pp[500010], ff[500010];
void init()
{
    cnt = 0;
    cnt1 = 0;
    cnt2 = 0;
    memset(head1, -1, sizeof(head1));
    memset(head, -1, sizeof(head));
    memset(head2, -1, sizeof(head2));
}

void add1(int u, int v, int w)
{
    pp[cnt1].u = u;
    pp[cnt1].v = v;
    pp[cnt1].w = w;
    pp[cnt1].next = head1[u];
    head1[u] = cnt1++;
}
void add2(int u, int v, int w)
{
    ff[cnt2].u = u;
    ff[cnt2].v = v;
    ff[cnt2].w = w;
    ff[cnt2].next = head2[u];
    head2[u] = cnt2++;
}
void add(int u, int v, int w)
{
    f[cnt].v = v;
    f[cnt].w = w;
    f[cnt].next = head[u];
    head[u] = cnt++;

    f[cnt].v = u;
    f[cnt].w = 0;
    f[cnt].next = head[v];
    head[v] = cnt++;
}

int SAP(int s, int e, int n)
{
    int max_flow = 0, v, u = s;
    int id, mindis;
    aug[s] = INF;
    pre[s] = -1;
    memset(dis, 0, sizeof(dis));
    memset(gap, 0, sizeof(gap));
    gap[0] = n;
    for (int i = 0; i <= n; ++i)  cur[i] = head[i];/// 初始化当前弧为第一条弧
    while (dis[s] < n)
    {
        bool flag = false;
        if (u == e)
        {
            max_flow += aug[e];
            for (v = pre[e]; v != -1; v = pre[v]) /// 路径回溯更新残留网络
            {
                id = cur[v];
                f[id].w -= aug[e];
                f[id^1].w += aug[e];
                aug[v] -= aug[e]; /// 修改可增广量,以后会用到
                if (f[id].w == 0) u = v; /// 不回退到源点,仅回退到容量为0的弧的弧尾
            }
        }
        for (id = cur[u]; id != -1; id = f[id].next)/// 从当前弧开始查找允许弧
        {
            v = f[id].v;
            if (f[id].w > 0 && dis[u] == dis[v] + 1) /// 找到允许弧
            {
                flag = true;
                pre[v] = u;
                cur[u] = id;
                aug[v] = min(aug[u], f[id].w);
                u = v;
                break;
            }
        }
        if (flag == false)
        {
            if (--gap[dis[u]] == 0) break; ///gap优化,层次树出现断层则结束算法
            mindis = n;
            cur[u] = head[u];
            for (id = head[u]; id != -1; id = f[id].next)
            {
                v = f[id].v;
                if (f[id].w > 0 && dis[v] < mindis)
                {
                    mindis = dis[v];
                    cur[u] = id; /// 修改标号的同时修改当前弧
                }
            }
            dis[u] = mindis + 1;
            gap[dis[u]]++;
            if (u != s) u = pre[u]; /// 回溯继续寻找允许弧
        }
    }
    return max_flow;
}
int d1[maxn];
int vis[maxn];
queue<int>fp;
void Spfa1(int s)
{
    for(int i = 0; i <= n; i++) d1[i] = INF;

   while(!fp.empty()) fp.pop();
    memset(vis, 0, sizeof(vis));
    vis[s] = 1;
    fp.push(s);
    d1[s] = 0;
    while(!fp.empty())
    {
        int x = fp.front();
        vis[x] = 0;
        fp.pop();
        for(int i = head1[x]; i != -1; i = pp[i].next)
        {
            int v = pp[i].v;
            if(d1[v] > d1[x]+pp[i].w)
            {
                d1[v] = d1[x]+pp[i].w;
                if(!vis[v])
                {
                    fp.push(v);
                    vis[v] = 1;
                }
            }
        }
    }

}
int d2[maxn];
void Spfa2(int s)
{
    for(int i = 0; i <= n; i++) d2[i] = INF;
    while(!fp.empty()) fp.pop();
    memset(vis, 0, sizeof(vis));
    vis[s] = 1;
    fp.push(s);
    d2[s] = 0;
    while(!fp.empty())
    {
        int x = fp.front();
        fp.pop();
        vis[x] = 0;
        for(int i = head2[x]; i != -1; i = ff[i].next)
        {
            int v = ff[i].v;
            if(d2[v] > d2[x]+ff[i].w)
            {
                d2[v] = d2[x]+ff[i].w;
                if(!vis[v])
                {
                    fp.push(v);
                    vis[v] = 1;
                }
            }
        }
    }

}

int main()
{
    int K;
    cin >>K;
    while(K--)
    {
        init();
        scanf("%d %d",&n, &m);
        int x, y;
        int u, v, w;
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d %d",&u, &v, &w);
            add1(u, v, w);
            add2(v, u, w);
        }
        scanf("%d %d",&x, &y);
        Spfa1(x);
        Spfa2(y);
        for(int i = 0; i < cnt1; i++)
            if(d1[pp[i].u]+d2[pp[i].v]+pp[i].w == d1[y]) add(pp[i].u, pp[i].v, 1);
        int ans = SAP(x, y, n);
        cout<<ans<<endl;
    }
    return 0;
}

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

时间: 2024-10-12 20:09:03

HDU 3416 Marriage Match IV(spfa+最大流)的相关文章

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

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

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

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

(中等) HDU 3416 Marriage Match IV,SPFA+SAP。

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

hdu 3416 Marriage Match IV 【 最短路 最大流 】

求边不可重复的最短路条数 先从起点到终点用一次dijkstra,再从终点到起点用一次dijkstra,来判断一条边是否在最短路上 如果在,就将这条边的两个端点连起来,容量为1 再跑一下dinic(),最大流就是不可重复的最短路条数 还是想不到怎么建图啊------ 每次做网络流的题目--- 诶---该怎么建图啊--- 想了一会儿----啊--不会啊--- 搜一下题解吧--- 啊,原来这样连边啊--- 啊,原来需要---floyd / 并查集 /dijkstra /------ 啊---快,粘一粘

hdu 3081 hdu 3277 hdu 3416 Marriage Match II III IV //最大流的灵活运用

3081 题意: n个女孩选择没有与自己吵过架的男孩有连边(自己的朋友也算,并查集处理),2分图,有些边,求有几种完美匹配(每次匹配每个点都不重复匹配) 我是建二分图后,每次增广一单位,(一次完美匹配),再修改起点还有终点的边流量,继续增广,直到达不到完美匹配为止.网上很多是用二分做的,我觉得没必要...(网上传播跟风真严重...很多人都不是真正懂最大流算法的...) 3277 : 再附加一条件,每个女孩可以最多与k个自己不喜欢的男孩.求有几种完美匹配(同上). 我觉得:求出上题答案,直接ans