码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3416(最大流+最短路)

时间:2016-08-01 21:12:50      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

Marriage Match IV

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3368    Accepted Submission(s): 1001


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
 
题意:给定一幅有向图,里面有 n 个点,m条边,现在一个人想从 A点走到 B点,每次都要走最短路,但是最短路上的每一段路都只能走一回,这次走了下次就不能再走这里了,问A->B最多有多少种走法?
题解:假设 u 和 v 是最短路上的点,那么建一幅新图,我们就在 u - v 之间连一条容量为 1的边就好了,然后从A—>B做最大流,但是,如何判断 u - v是最短路上的点呢?所以我们从A点做一次SPFA,求出A点到每一点的距离,然后反向建图,从B点也做相同的操作,如果 dis[A][u] + edge[u][v] + dis1[B][v] = dis[A][B],那么 u - v就是最短路上的点了。边要开200000,因为Dinic算法要建反向边,所以开两倍.
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 999999999;
const int N = 1005;
const int M = 200005;
struct Edge{
    int v,w,next;
}edge[M];
int head[N];
int level[N];
int tot;
void init()
{
    memset(head,-1,sizeof(head));
    tot=0;
}
void addEdge(int u,int v,int w,int &k)
{
    edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
    edge[k].v = u,edge[k].w=0,edge[k].next=head[v],head[v]=k++;
}
int BFS(int src,int des)
{
    queue<int>q;
    memset(level,0,sizeof(level));
    level[src]=1;
    q.push(src);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        if(u==des) return 1;
        for(int k = head[u]; k!=-1; k=edge[k].next)
        {
            int v = edge[k].v;
            int w = edge[k].w;
            if(level[v]==0&&w!=0)
            {
                level[v]=level[u]+1;
                q.push(v);
            }
        }
    }
    return -1;
}
int dfs(int u,int des,int increaseRoad){
    if(u==des||increaseRoad==0) {
        return increaseRoad;
    }
    int ret=0;
    for(int k=head[u];k!=-1;k=edge[k].next){
        int v = edge[k].v,w=edge[k].w;
        if(level[v]==level[u]+1&&w!=0){
            int MIN = min(increaseRoad-ret,w);
            w = dfs(v,des,MIN);
            if(w > 0)
            {
                edge[k].w -=w;
                edge[k^1].w+=w;
                ret+=w;
                if(ret==increaseRoad){
                    return ret;
                }
            }
            else level[v] = -1;
            if(increaseRoad==0) break;
        }
    }
    if(ret==0) level[u]=-1;
    return ret;
}
int Dinic(int src,int des)
{
    int ans = 0;
    while(BFS(src,des)!=-1) ans+=dfs(src,des,INF);
    return ans;
}
struct Edge1{
    int v,w,next;
}edge1[M],edge2[M];
int head1[N],head2[N];
int tot1,tot2;
int n,m,a,b;
void addEdge1(int u,int v,int w,int &k){
    edge1[k].v = v,edge1[k].w=w,edge1[k].next = head1[u],head1[u]=k++;
}
void addEdge2(int u,int v,int w,int &k){
    edge2[k].v = v,edge2[k].w=w,edge2[k].next = head2[u],head2[u]=k++;
}
void init1(){
    memset(head1,-1,sizeof(head1));
    tot1 = 0;
}
void init2(){
    memset(head2,-1,sizeof(head2));
    tot2 = 0;
}
int low[2][N];
bool vis[N];
void spfa(int s,int t,int flag,int *head,Edge1 edge[]){
    for(int i=1;i<=n;i++){
        low[flag][i] = INF;
        vis[i] = false;
    }
    low[flag][s] = 0;
    queue<int >q;
    q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int k=head[u];k!=-1;k=edge[k].next){
            int v = edge[k].v,w=edge[k].w;
            if(low[flag][v]>low[flag][u]+w){
                low[flag][v] = low[flag][u]+w;
                if(!vis[v]){
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--){
        init1();
        init2();
        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;
            addEdge1(u,v,w,tot1);
            addEdge2(v,u,w,tot2); ///反向边
        }
        scanf("%d%d",&a,&b);
        spfa(a,b,0,head1,edge1); ///a->b 第一遍
        spfa(b,a,1,head2,edge2); ///b->a 第二遍
        init();
        for(int i=1;i<=n;i++){
            for(int j=head1[i];j!=-1;j=edge1[j].next){
                int v = edge1[j].v,w=edge1[j].w;
                if(low[0][i]+low[1][v]+w==low[0][b]){
                    addEdge(i,v,1,tot);
                }
            }
        }
        if(low[0][b]==INF){
            printf("0\n");
            continue;
        }
        int max_flow = Dinic(a,b);
        printf("%d\n",max_flow);
    }
    return 0;
}

 

hdu 3416(最大流+最短路)

标签:

原文地址:http://www.cnblogs.com/liyinggang/p/5727033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!