关闭

奥妙重重的网络流

149人阅读 评论(0) 收藏 举报
分类:

这两个代码都是BZOJ1532的代码,仅仅是建图的差距就导致了4倍时间差。至今不懂为什么,先留下代码。

#include <bits/stdc++.h>
#define v edge[i].to
#define RepG(i,x) for(int i = head[x];~ i;i = edge[i].next)
#define fl edge[i].f
#define vfl edge[i ^ 1].f
#define Rep(i,n) for(int i = 1;i <= n;++ i)
using namespace std;
const int inf = 1 << 29;
const int N = 20005;
const int M = 400005;
int S = 0,T = N - 1,head[N],cnt,dep[N],q[N],ans,n;
struct Edge{int next,to,f;}edge[M];
void save(int a,int b,int c){edge[cnt]=(Edge){head[a],b,c},head[a] = cnt ++;edge[cnt] = (Edge){head[b],a,0},head[b] = cnt ++;}
bool bfs()
{
    int h,t,x;
    memset(dep,-1,sizeof(dep));
    dep[q[h = t = 0] = S] = 0;
    while(h <= t)
    {
        x = q[h ++];
        RepG(i,x)if(dep[v] == -1 && fl)dep[q[++ t] = v] = dep[x] + 1;
    }
    return dep[T] > 0;
}
int dfs(int x,int f)
{
    if(x == T)return f;
    int s = 0,ff;
    RepG(i,x)if(dep[v] == dep[x] + 1 && f && fl && (ff = dfs(v,min(f,fl))))
        s += ff,fl -= ff,vfl += ff,f -=ff;
    if(!s)dep[x] = -1;
    return s;
}
int mxf(){int s = 0;for(;bfs();s += dfs(S,inf));return s;}
int ax[N],ay[N],m;
void ReBuild(int ans)
{
    memset(head,-1,sizeof(head));cnt = 0;
    Rep(i,n)save(i,T,ans);
    Rep(i,m)
    {
        save(S,n + i, 1);
        save(n + i,ax[i], 1);
        save(n + i,ay[i], 1);
    }
} 
bool check(){return mxf() < m;}
int main()
{
    scanf("%d%d",&n,&m);
    Rep(i,m)scanf("%d%d",&ax[i],&ay[i]);
    int l = 0,r = 10000,mid;
    while(l < r)
    {
        mid = l + r >> 1;
        ReBuild(mid);
        if(check())l = mid + 1;
        else r = mid;
    }
    printf("%d\n",l);
    return 0;
}
//slow
#include <bits/stdc++.h>
#define v edge[i].to
#define RepG(i,x) for(int i = head[x];~ i;i = edge[i].next)
#define fl edge[i].f
#define vfl edge[i ^ 1].f
#define Rep(i,n) for(int i = 1;i <= n;++ i)
using namespace std;
const int inf = 1 << 29;
const int N = 20005;
const int M = 400005;
int S = 0,T = N - 1,head[N],cnt,dep[N],q[N],ans,n;
struct Edge{int next,to,f;}edge[M];
void save(int a,int b,int c){edge[cnt]=(Edge){head[a],b,c},head[a] = cnt ++;edge[cnt] = (Edge){head[b],a,0},head[b] = cnt ++;}
bool bfs()
{
    int h,t,x;
    memset(dep,-1,sizeof(dep));
    dep[q[h = t = 0] = S] = 0;
    while(h <= t)
    {
        x = q[h ++];
        RepG(i,x)if(dep[v] == -1 && fl)dep[q[++ t] = v] = dep[x] + 1;
    }
    return dep[T] > 0;
}
int dfs(int x,int f)
{
    if(x == T)return f;
    int s = 0,ff;
    RepG(i,x)if(dep[v] == dep[x] + 1 && f && fl && (ff = dfs(v,min(f,fl))))
        s += ff,fl -= ff,vfl += ff,f -=ff;
    if(!s)dep[x] = -1;
    return s;
}
int mxf(){int s = 0;for(;bfs();s += dfs(S,inf));return s;}
int ax[N],ay[N],m;
void ReBuild(int ans)
{
    memset(head,-1,sizeof(head));cnt = 0;
    Rep(i,n)save(S, i,ans);
    Rep(i,m)
    {
        save(n + i, T, 1);
        save(ax[i], n + i, 1);
        save(ay[i], n + i, 1);
    }
} 
bool check(){return mxf() < m;}//答案会比它大。 
int main()
{
    scanf("%d%d",&n,&m);
    Rep(i,m)scanf("%d%d",&ax[i],&ay[i]);
    int l = 0,r = 10000,mid;
    while(l < r)
    {
        mid = l + r >> 1;
        ReBuild(mid);
        if(check())l = mid + 1;
        else r = mid;
    }
    printf("%d\n",l);
    return 0;
}
//fast

很妙啊。
在都是重边的时候,第一份代码明显慢于第二份(试试就知道了)。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:43421次
    • 积分:1686
    • 等级:
    • 排名:千里之外
    • 原创:128篇
    • 转载:3篇
    • 译文:0篇
    • 评论:41条
    单向膜拜