【hdoj 5294】 Tricks Device 【最小割+最短路spfa】

本文探讨了如何通过构建特定图结构解决最短路径问题及其变种,即阻止最短路径所需的最少边数和允许的最大边数。文章通过具体实例介绍了网络流中的最小割概念,并给出了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang.
Unfortunately, Dumb Zhang masters the art of becoming invisible(奇门遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang.
Input
There are multiple test cases. Please process till EOF.
For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels.
In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs minute to pass channel i.
The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N.
Output
Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions.
Sample Input
8 9
1 2 2
2 3 2
2 4 1
3 5 3
4 5 4
5 8 1
1 6 2
6 7 5
7 8 1
Sample Output
2 6
给你N个点和M条无向边。现在A需要从点1追在点N的B,他必须走1->N的最短路才可以追上B,
问你1:B最少需要破坏几条路才可以阻断最短路;
2:B最多破坏多少条路A依旧有希望追上他。

知识点补充
1 什么是网络流的最小割
2 首先来解释割集 另一种含义
在一个有权图中,源点为Vs,汇点为Vt,从Vs到Vt有很多路径可以走,每条路径都包含若干条边对吧。这些边可能只属于一条路径,也可能同时出现在两条路径中。 如果拿掉这张图中的一些边,就无法从Vs到达Vt,这些边的组合就叫做 割集。
最小割的解释:
割集有很多,每一个割集中元素的权值之和成为割集容量。 所有割集容量中,最小的那个割集就叫做最小割。

思路:

问题1:SPFA跑一遍最短路,然后构建新图,把最短路的边加【这里用到了一种方法,很好】进新图边权值为1,在新图求最小割。转化成对立问题,求1 -> n的最大流。

问题2:在SPFA跑最短路的时候,用一个数组记录到达最短路途中每个点的所需要经过的最少边数。最后用 M - rec[ N ]就行了。

代码

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2000+10;
const int MAXM =1e5+10;
const int inf =0x3f3f3f3f;
/*--------------------------------*/
struct Ee{
    int from,to,val,nexts;
}ee[MAXM];
int hd[MAXN],tp;
int n,m;
void It(){
    memset(hd,-1,sizeof(hd));
    tp=0;
}
void addee(int a,int b,int c){
    Ee e={a,b,c,hd[a]};
    ee[tp]=e;hd[a]=tp++;
}
void input(){
    int a,b,c;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&a,&b,&c);
        addee(a,b,c);
        addee(b,a,c);
    }
}
int vis[MAXN],dis[MAXN];
int re[MAXN];   //re[i]记录最少经过几条边 可到达i  用来求第二个答案
void spfa(){
    queue<int>Q;
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(re,0,sizeof(re));
    vis[1]=1;dis[1]=0;Q.push(1);
    while(!Q.empty()){
        int now=Q.front();Q.pop();vis[now]=0;
        for(int i=hd[now];i!=-1;i=ee[i].nexts){
            Ee e=ee[i];
            if(dis[e.to]>dis[now]+e.val){
                dis[e.to]=dis[now]+e.val;
                re[e.to]=re[now]+1;   //更新最小边数 
                if(!vis[e.to]){
                    vis[e.to]=1;
                    Q.push(e.to);
                }
            }else if(dis[e.to]==dis[now]+e.val){
                re[e.to]=min(re[e.to],re[now]+1);  //更新最小边数 
            }
        }
    }
}

struct Edge {
    int from,to,cap,flow,nexts;     //  最大流模板 
}edge[MAXM];
int head[MAXN],top;
void init(){
    memset(head,-1,sizeof(head));
    top=0;
}
void addedge(int a,int b,int c){
    Edge e={a,b,c,0,head[a]};
    edge[top]=e;head[a]=top++;

    e={b,a,0,0,head[b]};
    edge[top]=e;head[b]=top++;
}
void getmap(){   // 构建新图,将最短路中的边加到新图中 ** 这个方法很巧妙的将最短路中的边加入新图 
    for(int i=1;i<=n;i++){
        for(int j=hd[i];j!=-1;j=ee[j].nexts){
            Ee e=ee[j];
            if(dis[i]+e.val==dis[e.to]) 
            addedge(i,e.to,1);
        }
    }
}
//int vis[MAXN],dis[MAXN]; 前面定义过了
int cur[MAXN];
bool bfs(int st,int ed){
    queue<int>Q;
    memset(dis,-1,sizeof(dis));
    memset(vis,0,sizeof(vis));
    Q.push(st);vis[st]=1;dis[st]=1;
    while(!Q.empty()){
        int now=Q.front();Q.pop();
        for(int i=head[now];i!=-1;i=edge[i].nexts){
            Edge e=edge[i];
            if(!vis[e.to]&&e.cap-e.flow>0){
                dis[e.to]=dis[now]+1;
                vis[e.to]=1;
                if(e.to==ed) return 1;
                Q.push(e.to);
            }
        }
    }
    return 0;
}
int dfs(int now,int a,int ed){
    if(now==ed||a==0) return a;
    int f,flow=0;
    for(int& i=cur[now];i!=-1;i=edge[i].nexts){
        Edge &e=edge[i];
        if(dis[e.to]==dis[now]+1&&((f=dfs(e.to,min(e.cap-e.flow,a),ed))>0 )){
            e.flow+=f;
            flow+=f;
            edge[i^1].flow-=f;
            a-=f;
            if(a==0) break;
        }
    }
    return flow;
}
int max_flow(int le,int ri){

    int maxflow=0;
    while(bfs(le,ri)){
        memcpy(cur,head,sizeof(head));
        maxflow+=dfs(le,inf,ri);
    }
    return maxflow;
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        init();
        It();
        input();
        spfa();
        getmap();
        printf("%d %d\n",max_flow(1,n),m-re[n]);
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 2000+11;
const int M = 60000*2+11;
const int mod = 1e9+7;
const int inf =0x3f3f3f3f;

struct Edge {
    int from,to,cap,flow,next;
};

Edge edge[M];
int head[N],top;
void init(){
    memset(head,-1,sizeof(head));
    top=0;
}
void addedge(int a,int b,int c){
    Edge e={a,b,c,0,head[a]};
    edge[top]=e;head[a]=top++;
}
bool vis[N];int dis[N];int les[N];
void spfa(int st,int ed){
    memset(vis,false,sizeof(vis));
    memset(dis,inf,sizeof(dis));
    memset(les,inf,sizeof(les));
    queue<int>Q;
    Q.push(st);vis[st]=true;dis[st]=les[st]=0;
    while(!Q.empty()){
        int now=Q.front();Q.pop();vis[now]=false;
        for(int i=head[now];i!=-1;i=edge[i].next){
            Edge e=edge[i];
            if(dis[e.to]>dis[now]+e.cap){
                dis[e.to]=dis[now]+e.cap;
                les[e.to]=les[now]+1;
                if(!vis[e.to]){
                    vis[e.to]=true;
                    Q.push(e.to);
                }else if(dis[e.to]==dis[now]+e.cap)
                    les[e.to]=min(les[e.to],les[now]+1);
            }
        }
    }
    //printf("%d %d\n",dis[ed],les[ed]);
}

struct Dinic{
    Edge edge[M];
    int head[N],top;
    void init(){
        memset(head,-1,sizeof(head));
        top=0;
    }
    void addedge(int a,int b,int c){
        Edge e={a,b,c,0,head[a]};
        edge[top]=e;head[a]=top++;
        Edge ee={b,a,0,0,head[b]};
        edge[top]=ee;head[b]=top++;
    }
    bool vis[N]; int dis[N];
    bool bfs(int st,int ed){
        queue<int>Q;
        memset(vis,false,sizeof(vis));
        Q.push(st);vis[st]=true;dis[st]=0;
        while(!Q.empty()){
            int now=Q.front();Q.pop();
            for(int i=head[now];i!=-1;i=edge[i].next){
                Edge e=edge[i];
                if(vis[e.to]) continue;
                if(e.cap-e.flow>0){
                    dis[e.to]=dis[now]+1;
                    vis[e.to]=true;
                    Q.push(e.to);
                    if(e.to==ed) return true;
                }
            }
        }
        return false ;
    }
    int cur[N];
    int dfs(int now, int a,int ed){
        if(now==ed||a==0) return a;
        int flow=0;
        for(int& i=cur[now];i!=-1;i=edge[i].next){
            Edge& e=edge[i];
            if(dis[now]+1==dis[e.to]){
                int f=dfs(e.to,min(a,e.cap-e.flow),ed);
                if(f>0){
                    e.flow+=f;
                    edge[i^1].flow-=f;
                    flow+=f;
                    a-=f;
                    if(!a) break;
                }
            }
        }
        return flow;
    }
    int Max_flow(int st,int ed){
        int ans=0;
        while(bfs(st,ed)){
            memcpy(cur,head,sizeof(head));
            ans+=dfs(st,inf,ed);
        }
        return ans;
    }
}dic;

int main(){
     int n,m;
     while(scanf("%d%d",&n,&m)!=EOF){
         init(); int mm=m; int a,b,c;
         while(mm--){
            scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,c);
            addedge(b,a,c);
         }
         spfa(1,n);
         dic.init();
         for(int j=1;j<=n;j++){
            for(int i=head[j];i!=-1;i=edge[i].next){
                 Edge e=edge[i];
                 if(dis[e.to]==dis[j]+e.cap)
                    dic.addedge(j,e.to,1);
            }
         }
         int ans=dic.Max_flow(1,n);
         printf("%d %d\n",ans,m-les[n]);
     }
return 0 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值