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

来源:互联网 发布:淘宝网店图片处理软件 编辑:程序博客网 时间:2024/05/19 18:44

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;}
阅读全文
0 0
原创粉丝点击