HDU 3416

Marriage Match IV

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

Total Submission(s): 2470    Accepted Submission(s): 742

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

题意:

给出一张无向图,要求图上的不相交得最短路条数。

思路:

先求出单源最短路,从终点往起点倒着搜出最短路上的边(可以用双向链表),然后另这些边的容量为1建新图,跑一遍最大流即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

#define N 1010
#define M 101000
const int INF = 1000000000;

int n, m, sp, tp, cnte, cnte2, head[N], lowdis[N], head2[N], dep[N], tail[N];
bool vis[N];

class Edge
{
public:
    int u, v, w, next, next2;
};
Edge edge1[M*2], edge2[M*2];

void addEdge1(int u, int v, int w)
{
    edge1[cnte].u = u; edge1[cnte].v = v;
    edge1[cnte].w = w; edge1[cnte].next = head[u];
    head[u] = cnte;
    edge1[cnte].next2 = tail[v];
    tail[v] = cnte++;
}
void addEdge2(int u, int v, int w)
{
    edge2[cnte2].u = u; edge2[cnte2].v = v;
    edge2[cnte2].w = w; edge2[cnte2].next = head2[u];
    head2[u] = cnte2++;

    edge2[cnte2].v = u; edge2[cnte2].u = v;
    edge2[cnte2].w = 0; edge2[cnte2].next = head2[v];
    head2[v] = cnte2++;
}

int spfa()
{
    queue<int> q;
    for(int i = 1; i <= n; i++)
    {
        vis[i] = 0;
        lowdis[i] = INF;
    }
    vis[sp] = 1; lowdis[sp] = 0;
    q.push(sp);
    while(!q.empty())
    {
        int cur = q.front();
        q.pop(); vis[cur] = 0;
        for(int i = head[cur]; i != -1; i = edge1[i].next)
        {
            int v = edge1[i].v;
            if(lowdis[v] > lowdis[cur]+edge1[i].w)
            {
                lowdis[v] = lowdis[cur]+edge1[i].w;

                if(!vis[v])
                {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    return lowdis[tp] != INF;
}

void dfs(int cur)
{
    for(int i = tail[cur]; i != -1; i = edge1[i].next2)
    {
        int u = edge1[i].u;
        if(lowdis[u]+edge1[i].w == lowdis[cur])
        {
            addEdge2(u, cur, 1);
            if(!vis[u])
            {
                vis[u] = 1;
                dfs(u);
            }
        }
    }
}

bool bfs()
{
    memset(vis, 0, sizeof vis);
    memset(dep, -1, sizeof dep);
    queue<int> q;
    q.push(sp);
    vis[sp] = 1;
    dep[sp] = 1;
    while(!q.empty())
    {
        int cur = q.front();
        q.pop();

        for(int i = head2[cur]; i != -1; i = edge2[i].next)
        {
            int v = edge2[i].v;
            if(!vis[v] && edge2[i].w > 0)
            {
                dep[v] = dep[cur]+1;
                vis[v] = 1;
                q.push(v);
            }
        }
    }

    return dep[tp] != -1;
}

int dfs2(int cur, int flow)
{
    if(cur == tp) return flow;
    int res = 0;
    for(int i = head2[cur]; i != -1 && flow > res; i = edge2[i].next)
    {
        int v = edge2[i].v;
        if(edge2[i].w > 0 && dep[v] == dep[cur]+1)
        {
            int x = min(edge2[i].w, flow-res);
            int f = dfs2(v, x);
            edge2[i].w-=f;
            edge2[i^1].w+=f;
            res += f;
        }
    }

    if(!res) dep[cur] = -1;
    return res;
}

int dinic()
{
    int res = 0;
    while(bfs())
    {
        int t;
        while(t = dfs2(sp, INF))
        {
            res += t;
        }
    }
    return res;
}

int main()
{
	//freopen("C:\\Users\\Admin\\Desktop\\in.txt", "r", stdin);
    int t; scanf("%d", &t);
    while(t--)
    {
        cnte = 0;
        cnte2 = 0;
        memset(head, -1, sizeof head);
        memset(head2, -1, sizeof head2);
        memset(tail, -1, sizeof tail);
        int u, v, w;
        scanf("%d%d", &n, &m);
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &u, &v, &w);
            addEdge1(u, v, w);
        }
        scanf("%d%d", &sp, &tp);
        int ans;
        if(!spfa()) ans = 0;
        else
        {
            dfs(tp);
            ans = dinic();
        }
        printf("%d\n", ans);
    }
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-29 22:39:20

HDU 3416的相关文章

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

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

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(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 (最短路+最大流)

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(最短路+网络流之最大流)

题目地址: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

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 3081 hdu 3277 hdu 3416 Marriage Match II III IV //灵活运用最大流量

3081 意甲冠军: n女生选择不吵架,他甚至男孩边(他的朋友也算.并为您收集过程).2二分图,一些副作用,有几个追求完美搭配(每场比赛没有重复的每一个点的比赛) 后.每次增广一单位,(一次完美匹配),再改动起点还有终点的边流量,继续增广.直到达不到完美匹配为止.网上非常多是用二分做的,我认为不是必需. .. (网上传播跟风真严重.. . 非常多人都不是真正懂最大流算法的... ) 3277 : 再附加一条件,每一个女孩能够最多与k个自己不喜欢的男孩. 求有几种完美匹配(同上). 我认为:求出上

(中等) 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

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] 是否成立