hdu 3416 Marriage Match IV dijkstra + isap


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

题意:求有多少条最独立的最短路(即每条最短路经过的边都不一样)。

设有st->....a ->b -> ....ed的一条最短路,求出st到a的dis1,ed到b的dis2,若有dis1+dis(a->b)+dis2 == dis(st->ed),则a->b肯定是一条最短路上面的边。然后在最短路的边上面跑一发最大流就是答案

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <iomanip>
///cout << fixed << setprecision(13) << (double) x << endl;
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
#define asd puts("sdfsdfsdfsdfsdfsdf");
typedef long long ll;
typedef __int64 LL;
const int inf = 0x3f3f3f3f;
const int M = 100010, N = 1010;

struct node{
    int v, w, nxt;
}e[M], ee[M], E[M<<1];

struct DIS{
    int id, x;
    bool operator < (const DIS &rhs) const {
        return x < rhs.x;
    }
}dis[N], dis1[N];

int head1[N], head2[N], head[N];
int cnt1, cnt2, cnt;
int n, m;
int st, ed;
queue <DIS> q;

void init()
{
    cnt1 = cnt2 = cnt = 0;
    memset( head1, -1, sizeof( head1 ) );
    memset( head2, -1, sizeof( head2 ) );
    memset( head, -1, sizeof( head ) );
}

void add( int u, int v, int w, int op )
{
    if( op == 1 ) {
        e[cnt1].w = w;
        e[cnt1].v = v;
        e[cnt1].nxt = head1[u];
        head1[u] = cnt1++;
    }
    else if( op == 0 ) {
        ee[cnt2].w = w;
        ee[cnt2].v = v;
        ee[cnt2].nxt = head2[u];
        head2[u] = cnt2++;
    }
    else if( op == -1 ) {
        //printf("u: %d v: %d\n", u, v);
        E[cnt].v = v;
        E[cnt].w = w;
        E[cnt].nxt = head[u];
        head[u] = cnt++;

        E[cnt].v = u;
        E[cnt].w = 0;
        E[cnt].nxt = head[v];
        head[v] = cnt++;
    }
}

void dijkstra( )
{
    for( int i = 1; i <= n; ++i ) {
        dis[i].id = i;
        dis[i].x = inf;
    }
    while( !q.empty() ) q.pop();
    dis[st].x = 0;
    q.push( dis[st] );
    while( !q.empty() ) {
        DIS tmp = q.front();
        q.pop();
        int u = tmp.id, x = tmp.x;
        for( int i = head1[u]; ~i; i = e[i].nxt ) {
            int v = e[i].v, w = e[i].w;
            if( x + w < dis[v].x ) {
                dis[v].x = x + w;
                q.push( dis[v] );
            }
        }
    }
}

void rev_dijkstra( )
{
    for( int i = 1; i <= n; ++i ) {
        dis1[i].id = i;
        dis1[i].x = inf;
    }
    while( !q.empty() ) q.pop();
    dis1[ed].x = 0;
    q.push( dis1[ed] );
    while( !q.empty() ) {
        DIS tmp = q.front();
        q.pop();
        int u = tmp.id, x = tmp.x;
        for( int i = head2[u]; ~i; i = ee[i].nxt ) {
            int v = ee[i].v, w = ee[i].w;
            if( x + w < dis1[v].x ) {
                dis1[v].x = x + w;
                q.push( dis1[v] );
            }
        }
    }
}

void build()
{
    int dis_st_ed = dis[ed].x;
    for( int u = 1; u <= n; ++u ) {
        for( int i = head1[u]; ~i; i = e[i].nxt ) {
            int v = e[i].v, w = e[i].w;
            if( dis[u].x + w + dis1[v].x == dis_st_ed ) {
                add( u, v, 1, -1 );
            }
        }
    }
}

int dep[N];
int gap[N];
int cur[N];
int s[N], top;
queue <int> Q;

void rev_bfs()
{
    while( !Q.empty() ) Q.pop();
    memset( dep, -1, sizeof( dep ) );
    memset( gap, 0, sizeof( gap ) );
    dep[ed] = 0;
    gap[0] = 1;
    Q.push( ed );
    while( !Q.empty() ) {
        int u = Q.front();
        Q.pop();
        for( int i = head[u]; ~i; i = E[i].nxt ) {
            int v = E[i].v;
            if( ~dep[v] )
                continue;
            dep[v] = dep[u] + 1;
            gap[dep[v]]++;
            Q.push( v );
        }
    }
}

int isap()
{
    memcpy( cur, head, sizeof cur );
    rev_bfs();
    int flow = 0, u = st, i, nv = n + 1;
    top = 0;
    while( dep[st] < nv ) {
        if( u == ed ) {
            int minn = inf, tmp;
            for( i = 0; i < top; ++i ) {
                if( minn > E[s[i]].w ) {
                    minn = E[s[i]].w;
                    tmp = i;
                }
            }
            for( i = 0; i < top; ++i ) {
                E[s[i]].w -= minn;
                E[s[i]^1].w += minn;
            }
            flow += minn;
            top = tmp;
            u = E[s[top]^1].v;
        }
        for( i = cur[u]; ~i; i = E[i].nxt ) {
            int v = E[i].v;
            if( E[i].w > 0 && dep[u] == dep[v] + 1 ) {
                cur[u] = i;
                break;
            }
        }
        if( ~i ) {
            s[top++] = i;
            u = E[i].v;
        }
        else {
            if( 0 == (--gap[dep[u]]) )
                break;
            int minn = nv;
            for( i = head[u]; ~i; i = E[i].nxt ) {
                int v = E[i].v;
                if( E[i].w > 0 && minn > dep[v] ) {
                    minn = dep[v];
                    cur[u] = i;
                }
            }
            dep[u] = minn + 1;
            gap[dep[u]]++;
            if( u != st ) {
                u = E[s[--top]^1].v;
            }
        }
    }
    return flow;
}

int main()
{
    int tt;
    for( scanf("%d", &tt); tt--; ) {
        init();
        scanf("%d%d", &n, &m);
        for( int i = 1, u, v, w; i <= m; ++i ) {
            scanf("%d%d%d", &u, &v, &w);
            if( u == v )
                continue;
            add( u, v, w, 1 );
            add( v, u, w, 0 );  //rev
        }
        scanf("%d%d", &st, &ed);
        dijkstra();
        rev_dijkstra();
        build( );
        int ans = isap();
        printf("%d\n", ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值