【最短路Dijstla+最大流Dinic】HDU - 3416 O - Marriage Match IV

O - Marriage Match IV  HDU - 3416 

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条线,告诉你u->v的花费是w
问你s->e有几条最短路,走过的路不能再走
先跑一个dijstla,得到s到每个点的最短距离
输入是保存各个边的值,要是跑完最短路后
d[u]+w==d[v]就代表这条路是最短路中的,把它放进最大流的建图,流量为1
最后从s到e跑一次最大流即可

#include <bits/stdc++.h>
using namespace std;
const int maxn=2005;
const int maxm=1e5+7;
const int INF=0x3f3f3f3f;
struct node1
{
    int to,cost,next;
}tree[maxm];

int head[maxn];
int d[maxn];
int cnt;
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    memset(d,INF,sizeof(d));
}

void add(int u,int v,int w)
{
    tree[cnt].to=v;
    tree[cnt].cost=w;
    tree[cnt].next=head[u];
    head[u]=cnt++;
}

struct node2
{
    int u,v,w;
}bian[maxm];

void dijstla(int s)
{
    typedef pair<int,int> p;
    priority_queue <p,vector<p>,greater<p> > Q;
    d[s]=0;
    Q.push(p(0,s));
    while(!Q.empty())
    {
        p t=Q.top();
        Q.pop();
        int x=t.second;
        if(d[x]<t.first) continue;
        for(int i=head[x];i!=-1;i=tree[i].next)
        {
            int y=tree[i].to,z=tree[i].cost;
            if(d[y]>d[x]+z)
            {
                d[y]=d[x]+z;
                Q.push(p(d[y],y));
            }
        }
    }

}

struct edge
{
    int to,cap,rev; //用于表示边的结构体(终点,容量,反向边)
};

vector <edge> G[maxn]; //图的邻接表表示
int level[maxn];   //顶点到源点的距离标号
int iter[maxn];    //当前弧,在其之前的边已经没有用了

void add_edge(int from,int to,int cap)
{
    G[from].push_back((edge){to,cap,G[to].size()});
    G[to].push_back((edge){from,0,G[from].size()-1});
}

void bfs(int s)  //通过bfs计算从源点出发的距离标号
{
    memset(level,-1,sizeof(level));
    queue <int> q;
    level[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int v=q.front();q.pop();
        for(int i=0;i<G[v].size();i++)
        {
            edge &e=G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                q.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f)  //通过dfs寻找增广路
{
    if(v==t) return f;
    for(int &i=iter[v];i<G[v].size();i++)
    {
        edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to])
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t)
{
    int flow=0;
    for(;;)
    {
        bfs(s); //计算层次图
        if(level[t]<0) return flow; //找不到s-t路径
        memset(iter,0,sizeof(iter)); //初始化当前弧
        int f;
        while((f=dfs(s,t,INF))>0)  //更新最大流
            flow+=f;
    }
    return flow;
}


int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        for(int i=0;i<maxn;i++) G[i].clear();
        int n,m;
        int num=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            if(u==v) continue;
            bian[++num].u=u,bian[num].v=v,bian[num].w=w;
            add(u,v,w);
        }
        int s,e;
        scanf("%d%d",&s,&e);
        dijstla(s);
        for(int i=1;i<=num;i++)
        {
            int u=bian[i].u;
            int v=bian[i].v;
            int w=bian[i].w;
            if(d[u]+w==d[v]) add_edge(u,v,1);
        }
        printf("%d\n",max_flow(s,e));
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值