Marriage Match IV (hdu 3416 网络流+spfa最短路)

Marriage Match IV

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2275    Accepted Submission(s): 691

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

Author

[email protected]

Source

HDOJ Monthly Contest – 2010.06.05

Recommend

lcy   |   We have carefully selected several similar problems for you:  3376 3491 1569 3418 3422

题意:有n个城市m条边,告诉起点和终点,问从起点到终点不走重复边的最短路有多少条。

思路:求最短路的条数可以用最大流,建图时要去掉不是最短路上的边,判断某条边是不是最短路上的边:如果满足dist1[u]  + dist2[v] + E1[i].len = dist1[End]则可以说明该边是最短路上的边,其中 dist1[]  为各点到起点的最短距离,dist2[] 为各点到终点的最短距离。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define MAXN 1005
#define MAXM 300005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

struct Edge
{
    int to,next,cap,flow;
}edge[MAXM];

int n,m,start,End;
int tol,tol1,tol2;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];

struct EDGE
{
    int u,v,len,next;
}E1[MAXM],E2[MAXM];

int head1[MAXN],dist1[MAXN],inq[MAXN];
int head2[MAXN],dist2[MAXN];

void init()
{
    tol=0;tol1=0;tol2=0;
    memset(head,-1,sizeof(head));
    memset(head1,-1,sizeof(head1));
    memset(head2,-1,sizeof(head2));
}

void add1(int u,int v,int w)
{
    E1[tol1].u=u; E1[tol1].v=v; E1[tol1].len=w; E1[tol1].next=head1[u]; head1[u]=tol1++;
}

void add2(int u,int v,int w)
{
    E2[tol2].u=u; E2[tol2].v=v; E2[tol2].len=w; E2[tol2].next=head2[u]; head2[u]=tol2++;
}

//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u];
    edge[tol].flow=0; head[u]=tol++;
    edge[tol].to=u; edge[tol].cap=rw; edge[tol].next=head[v];
    edge[tol].flow=0; head[v]=tol++;
}

void spfa(int s,int *dist,int *head,EDGE *E)  //spfa求最短路
{
    memset(inq,0,sizeof(inq));
    queue<int>Q;
    Q.push(s);
    inq[s]=1;
    dist[s]=0;
    while (!Q.empty())
    {
        int u=Q.front(); Q.pop();
        inq[u]=0;
        for (int i=head[u];i+1;i=E[i].next)
        {
            int v=E[i].v;
            if (dist[v]>dist[u]+E[i].len)
            {
                dist[v]=dist[u]+E[i].len;
                if (!inq[v])
                {
                    inq[v]=1;
                    Q.push(v);
                }
            }
        }
    }
}

//输入参数:起点,终点,点的总数
//点的编号没有影响,只要输入点的总数
int sap(int start,int End,int N)
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    int u=start;
    pre[u]=-1;
    gap[0]=N;
    int ans=0;
    while (dep[start]<N)
    {
        if (u==End)
        {
            int Min=INF;
            for (int i=pre[u];i!=-1;i=pre[edge[i^1].to])
                if (Min>edge[i].cap-edge[i].flow)
                    Min=edge[i].cap-edge[i].flow;
            for (int i=pre[u];i!=-1;i=pre[edge[i^1].to])
            {
                edge[i].flow+=Min;
                edge[i^1].flow-=Min;
            }
            u=start;
            ans+=Min;
            continue;
        }
        bool flag=false;
        int v;
        for (int i=cur[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].to;
            if (edge[i].cap-edge[i].flow && dep[v]+1==dep[u])
            {
                flag=true;
                cur[u]=pre[v]=i;
                break;
            }
        }
        if (flag)
        {
            u=v;
            continue;
        }
        int Min=N;
        for (int i=head[u];i!=-1;i=edge[i].next)
            if (edge[i].cap-edge[i].flow && dep[edge[i].to]<Min)
            {
                Min=dep[edge[i].to];
                cur[u]=i;
            }
        gap[dep[u]]--;
        if (!gap[dep[u]]) return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if (u!=start) u=edge[pre[u]^1].to;
    }
    return ans;
}

int main()
{
    int i,j,t,u,v,w;
    scanf("%d",&t);
    while (t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for (i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            if (u==v) continue;  //去自环
            add1(u,v,w);
            add2(v,u,w);
        }
        scanf("%d%d",&start,&End);
        memset(dist1,INF,sizeof(dist1));
        memset(dist2,INF,sizeof(dist2));
        spfa(start,dist1,head1,E1);
        spfa(End,dist2,head2,E2);
        for (i=0;i<tol1;i++)  //建图
        {
            if (dist1[E1[i].u]+dist2[E1[i].v]+E1[i].len==dist1[End])
                addedge(E1[i].u,E1[i].v,1);
        }
        printf("%d\n",sap(start,End,n));
    }
    return 0;
}
时间: 2024-10-13 04:52:13

Marriage Match IV (hdu 3416 网络流+spfa最短路)的相关文章

O - Marriage Match IV - hdu 3416(最短路+最大流)

题目大意:在城市A的男孩想去城市B的女孩,不过他去城市B必须走最短路,并且走过的路不可以再走,问他最多能看这个女孩多少次.   分析:因为这个男孩直走最短路,所以我们必须求出来所有最短路径上的路,怎么判断一条路是否属于最短路经上的呢?其实比较容易的,只要先求出来从A到达所有点的最短路distA[x], 然后再求出来所有点到B的最短路distB[y](添加反边从B开始即可求出),如果x-y之间有一条路,那么只需要判断distA[x]+distB[y]+w(x,y) == distA[B] 是否成立

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(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    Accept

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

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

Marriage Match IV(最短路+网络流)

Marriage Match IV http://acm.hdu.edu.cn/showproblem.php?pid=3416 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6081    Accepted Submission(s): 1766 Problem Description Do not sincere non-inter

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

HDU3605:Marriage Match IV

Marriage Match IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6230    Accepted Submission(s): 1804 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3416 Description: Do not sincere non-interfe