2015 多校赛 第一场 1007 (hdu 5294)

总算今天静下心来学算法。。

 

Description

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 li(0<li<=100) 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条边。规定 1 为起点,n 为终点。问,若走最短路(可能不止一条),最少删去几条边使得起点与终点不连通,最多删去几条边使得仍然连通。
 
思路:
对于第一个问题,先跑一遍最短路,然后用最短路重新构图。构图方式有二:一是判 d[i]-d[j]==e[i][j],是则加入新图。二是最短路结点的儿子或者父亲,然后依次加入新图。之后,跑一遍最大流求最小割即可。
对于第二个问题,只要在跑最短路的时候,用 num 数组记录维护到当前结点经过的边数即可。初始化num[1]=0;
注意有重边、多边等情况,用邻接表存图。
 
分别用 EdmondsKarp 和 Dinic 做了遍。
前者的复杂度上界为O(nm*m),后者为O(n*nm)。
但大概数据比较和谐,前者也过了。
 
重新构图的时候注意对应点和边。比如最后才找到的bug:
for(int i=1;i<=n;i++){
    for(int j=0;j<g[i].size();j++){
        int tmp=d[i]-d[g[i][j]];
        if(tmp<0) tmp=-tmp;
        if(tmp==e[i][j])
            addEdge(i,g[i][j],e[i][j]);
    }
}            

举个例子:i 结点在最短路中前于 j 结点,e[i][j] 表示从 j 到 i 到有向边权值,则 d[i]-d[j]<0,则判了 d[j]-d[i]==e[i][j],错误。这在某些情况下回使得新建的图多了一条边。

正确的姿势如下:
for(int i=1;i<=n;i++){
    for(int j=0;j<g[i].size();j++){
        if(d[i]+e[i][j]==d[j])
            addEdge(i,g[i][j],e[i][j]);
    }
}

 

最小割的定义和求法:

定义:设点集S,T,称将起点在 S 中,终点在 T 中 的所有边删除后将使得 s 无法到达 t 的的集合划分(S,T)称为一个 s-t 割。(s 在 S 中 t 在 T 中)

在求得最大流后,寻找增广路过程中标记的点为集合 S,其余点为 T ,即得最小割。

最小割中的边数即为第一问的答案。(详情见代码)

 

对数组设置上界的时候:memset(n_g,0x3f,sizeof(n_g));//每个数都为0x3f3f3f3f。
以下两份代码进一步优化的途径:
1.用数组完成邻接表存图,避免 vector 的 clear 和 push_back;
2.求最短路时,用优先队列优化Dijkstra,或者用spfa算法。
3.用数组模拟队列,这里没有必要模拟环形队列。
其它有待补充。
 
EdmondsKarp:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define maxn 2015
#define inf 0x3f3f3f3f
struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
vector<Edge>E;
vector<int>g[maxn],e[maxn],G[maxn];
int n,m,M,a,b,c,d[maxn],vis[maxn],num[maxn],bet[maxn],p[maxn];
void init(){
    for(int i=0;i<=n;i++)
        g[i].clear(),e[i].clear(),G[i].clear();
    E.clear();
    memset(vis,0,sizeof(vis));
}
void addEdge(int from,int to,int cap){
    E.push_back(Edge(from,to,cap,0));
    E.push_back(Edge(to,from,0,0));
    M=E.size();
    G[from].push_back(M-2);
    G[to].push_back(M-1);
}
void maxflow(int s,int t){
    for(;;){
        memset(bet,0,sizeof(bet));
        queue<int>q;
        q.push(s);
        bet[s]=inf;
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<G[x].size();i++){
                Edge &ee=E[G[x][i]];
                if(!bet[ee.to]&&ee.cap>ee.flow){
                    p[ee.to]=G[x][i];
                    bet[ee.to]=min(bet[x],ee.cap-ee.flow);
                    q.push(ee.to);
                }
            }
            if(bet[t]) break;
        }
        if(!bet[t]) break;
        for(int u=t;u!=s;u=E[p[u]].from){
            E[p[u]].flow+=bet[t];
            E[p[u]^1].flow-=bet[t];
        }
    }
}
void Dijk(){
    memset(d,0x3f,sizeof(d));
    memset(num,0x3f,sizeof(num));
    d[1]=num[1]=0;
    for(int i=1;i<=n;i++){
        int u=-1;
        for(int j=1;j<=n;j++) if(!vis[j]){
            if(u==-1||d[j]<d[u]) u=j;
        }
        vis[u]=1;
        for(int j=0;j<g[u].size();j++) if(!vis[g[u][j]]){
            int tmp=d[u]+e[u][j];
            if(tmp==d[g[u][j]]) num[g[u][j]]=min(num[u]+1,num[g[u][j]]);
            if(tmp<d[g[u][j]]){
                d[g[u][j]]=tmp;
                num[g[u][j]]=num[u]+1;
            }
        }
    }
}
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //freopen("test.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)){
        init();
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&a,&b,&c);
            g[a].push_back(b),e[a].push_back(c);
            g[b].push_back(a),e[b].push_back(c);
        }
        Dijk();
        int ans1=0,ans2=m-num[n];
        for(int i=1;i<=n;i++){
            for(int j=0;j<g[i].size();j++){
                if(e[i][j]+d[i]==d[g[i][j]])
                    addEdge(i,g[i][j],e[i][j]);
            }
        }
        maxflow(1,n);
        for(int i=0;i<E.size();i++){
            Edge &ee=E[i];
            if(bet[ee.from]&&!bet[ee.to]&&ee.cap>0) ans1++;
        }
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}
View Code

以及Dinic:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define maxn 2015
#define inf 0x3f3f3f3f
struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
vector<Edge>E;
vector<int>g[maxn],e[maxn],G[maxn];
int n,m,M,a,b,c,d[maxn],vis[maxn],num[maxn],cur[maxn],dis[maxn];
void init(){
    for(int i=0;i<=n;i++)
        g[i].clear(),e[i].clear(),G[i].clear();
    E.clear();
    memset(vis,0,sizeof(vis));
}
void addEdge(int from,int to,int cap){
    E.push_back(Edge(from,to,cap,0));
    E.push_back(Edge(to,from,0,0));
    M=E.size();
    G[from].push_back(M-2);
    G[to].push_back(M-1);
}
bool bfs(){
    memset(vis,0,sizeof(vis));
    queue<int>q;
    q.push(1);
    dis[1]=0;
    vis[1]=1;
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=0;i<G[x].size();i++){
            Edge &ee=E[G[x][i]];
            if(!vis[ee.to]&&ee.cap>ee.flow){
                vis[ee.to]=1;
                dis[ee.to]=dis[x]+1;
                q.push(ee.to);
            }
        }
    }
    return vis[n];
}
int dfs(int x,int a){
    if(x==n||a==0) return a;
    int flow=0,f;
    for(int &i=cur[x];i<G[x].size();i++){
        Edge &ee=E[G[x][i]];
        if(dis[x]+1==dis[ee.to]&& (f=dfs(ee.to,min(a,ee.cap-ee.flow)))>0){
            ee.flow+=f;
            E[G[x][i]^1].flow-=f;
            flow+=f;
            a-=f;
            if(a==0) break;
        }
    }
    return flow;
}
void maxflow(){
    while(bfs()){
        memset(cur,0,sizeof(cur));
        dfs(1,inf);
    }
}
void Dijk(){
    memset(d,0x3f,sizeof(d));
    memset(num,0x3f,sizeof(num));
    d[1]=num[1]=0;
    for(int i=1;i<=n;i++){
        int u=-1;
        for(int j=1;j<=n;j++) if(!vis[j]){
            if(u==-1||d[j]<d[u]) u=j;
        }
        vis[u]=1;
        for(int j=0;j<g[u].size();j++) if(!vis[g[u][j]]){
            int tmp=d[u]+e[u][j];
            if(tmp==d[g[u][j]]) num[g[u][j]]=min(num[u]+1,num[g[u][j]]);
            if(tmp<d[g[u][j]]){
                d[g[u][j]]=tmp;
                num[g[u][j]]=num[u]+1;
            }
        }
    }
}
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //freopen("test.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)){
        init();
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&a,&b,&c);
            g[a].push_back(b),e[a].push_back(c);
            g[b].push_back(a),e[b].push_back(c);
        }
        Dijk();
        int ans1=0,ans2=m-num[n];
        for(int i=1;i<=n;i++){
            for(int j=0;j<g[i].size();j++){
                if(d[i]+e[i][j]==d[g[i][j]])
                    addEdge(i,g[i][j],e[i][j]);
            }
        }
        maxflow();
        for(int i=0;i<E.size();i++){
            Edge &ee=E[i];
            if(ee.from==ee.to) continue;
            if(vis[ee.from]&&!vis[ee.to]&&ee.cap>0) ans1++;
        }
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}
View Code

 

 
posted @ 2015-08-04 20:18  轶辰  阅读(141)  评论(0编辑  收藏  举报