Marriage Match IV

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<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 1e3 + 10, M = 1e5 + 10;

int head[N], ver[M << 1], Next[M << 1], edge[M << 1], tot;
int hc[N], vc[M << 1], nc[M << 1], ec[M << 1], tc;
int d[N], dc[N], A, B, n, m;
bool v[M];
namespace DINIC {
    int head[N], ver[M << 1], edge[M << 1], Next[M << 1], d[N];
    int s, t, tot = 1, maxflow;
    queue<int> q;

    inline void init() {
        tot = 1, maxflow = 0;
        ms(head);
        while (!q.empty())q.pop();
    }

    inline void add(int x, int y, int z) {
        ver[++tot] = y, edge[tot] = z, Next[tot] = head[x], head[x] = tot;
        ver[++tot] = x, edge[tot] = 0, Next[tot] = head[y], head[y] = tot;
    }

    bool bfs() {
        ms(d);
        while (!q.empty())q.pop();
        q.push(s);
        d[s] = 1;
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            reps(x)
                if (edge[i] && !d[ver[i]]) {
                    q.push(ver[i]);
                    d[ver[i]] = d[x] + 1;
                    if (ver[i] == t)return true;
                }
        }
        return false;
    }

    int dinic(int x, int flow) {
        if (x == t)return flow;
        int rest = flow, k;
        for (int i = head[x]; i && rest; i = Next[i])
            if (edge[i] && d[ver[i]] == d[x] + 1) {
                k = dinic(ver[i], min(rest, edge[i]));
                if (!k)d[ver[i]] = 0;
                edge[i] -= k, edge[i ^ 1] += k, rest -= k;
            }
        return flow - rest;
    }

    int solve() {
        int flow = 0;
        while (bfs())while (flow = dinic(s, INF))maxflow += flow;
        return maxflow;
    }
}

inline void add(int x, int y, int z) {
    ver[++tot] = y;
    Next[tot] = head[x];
    edge[tot] = z;
    head[x] = tot;
}

inline void add_c(int x, int y, int z) {
    vc[++tc] = y;
    nc[tc] = hc[x];
    ec[tot] = z;
    hc[x] = tc;
}

inline void init() {
    tot = tc = 0;
    ms(head), ms(hc);
}

inline void read() {
    n = qr(), m = qr();
    repi(i, 1, m) {
        int x = qr(), y = qr(), z = qr();
        if (x == y)continue;
        add(x, y, z), add_c(y, x, z);
    }
    A = qr(), B = qr();
}

inline void dijkstra() {
    ms(v);
    memset(dc, 0x3f, sizeof(dc));
    priority_queue<pii > q;
    dc[B] = 0, q.push({0, B});
    while (!q.empty()) {
        int x = q.top().se;
        q.pop();
        if (v[x])continue;
        v[x] = true;
        for (int i = hc[x]; i; i = nc[i]) {
            int y = vc[i], z = ec[i];
            if (dc[y] > dc[x] + z) {
                dc[y] = dc[x] + z;
                q.push({-dc[y], y});
            }
        }
    }
}

inline void bfs() {
    ms(v);
    queue<int> q;
    d[A] = 0, q.push(A);
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        reps(x) {
            int y = ver[i], z = edge[i];
            if (v[i])continue;
            v[i] = true;
            if (d[x] + z + dc[y] == dc[A]) {
                DINIC::add(x, y, 1);
                d[y] = d[x] + z, q.push(y);
            }
        }
    }
}

int main() {
    int T = qr();
    while (T--) {
        init(), DINIC::init();
        read();
        DINIC::s = A, DINIC::t = B;
        dijkstra();
        bfs();
        pi(DINIC::solve());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_sky123_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值