poj1966Cable TV Network(无向图最小点割集 ISAP+邻接矩阵)

题目请戳这里

邻接表的ISAP被卡了一天。。。TLE。。。。终于被卡了。。。好忧桑啊啊啊。。。

题目大意:给一张无向图,求最少去掉几个点使图不连通。

题目分析:求无向图的点连通度,拆点建图跑最大流。具体做法是:将一个点i拆成2个点:i和i+n,分别表示从第i个点出去和进入第i个点。那么i+n->i建边,边权1,对于每一条边(a,b),建边a->b + n,b->a+n,边权无穷。然后枚举没有边直接相连的点对(a,b),以a为源点,b+n为汇点跑最大流,最大流量就是该图的一个割,枚举所有不相邻点对,求出最小割。具体原理就是求不相邻点对(a,b)之间的最大独立轨数目,其实就是从a出发到达b的没有公共交点的路径数目。按照上述方式建图后,每个点i和i+n之间边权为1,保证每个点只存在某一条独立轨中,这样最大流的流量就是a到b的独立轨数量。

这题一开始熟悉的邻接表+ISAP做的,怎么交都是TLE了,没办法,只好改成邻接矩阵,竟然很快过了。这题复杂度还是很高的,说明数据不强,但是邻接表为什么就TLE了呢。。。

终于发现问题了。。。原来是输入函数导致的TLE。。。

详情请见代码:

邻接表+ISAP:

 

#include <iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

const int N = 101;

const int M = 50005;

const int inf = 0x3f3f3f3f;

int n,m,num;

struct node

{

    int to,next,c,pre;

}arc[M];

int head[N],que[N],sta[N],cnt[N],dis[N],rpath[N];

int st,ed;

bool map[N][N];

int d[M][2];

void build(int s,int e,int cap)

{

    arc[num].to = e;

    arc[num].c = cap;

    arc[num].next = head[s];

    head[s] = num ++;

    arc[num - 1].pre = num;

    arc[num].pre = num - 1;

    arc[num].to = s;

    arc[num].c = 0;

    arc[num].next = head[e];

    head[e] = num ++;

}

void re_Bfs()

{

    int i,front,rear;

    for(i = 0;i < n + n;i ++)

    {

        dis[i] = inf;

        cnt[i] = 0;

    }

    front = rear = 0;

    cnt[0] = 1;

    dis[ed] = 0;

    que[rear ++] = ed;

    while(front != rear)

    {

        int u = que[front ++];

        for(i = head[u];i != -1;i = arc[i].next)

        {

            if(arc[arc[i].pre].c == 0 || dis[arc[i].to] < inf)

                continue;

            dis[arc[i].to] = dis[u] + 1;

            cnt[dis[arc[i].to]] ++;

            que[rear ++] = arc[i].to;

        }

    }

}

int ISAP()

{

    re_Bfs();

    int i,u,v,ret = 0;

    for(i = 0;i < n + n;i ++)

        sta[i] = head[i];

    u = st;

    while(dis[st] < n + n)

    {

        if(u == ed)

        {

            int curflow = inf;

            for(i = st;i != ed;i = arc[sta[i]].to)

                curflow = min(curflow,arc[sta[i]].c);

            for(i = st;i != ed;i = arc[sta[i]].to)

            {

                arc[sta[i]].c -= curflow;

                arc[arc[sta[i]].pre].c += curflow;

            }

            ret += curflow;

            u = st;

        }

        for(i = sta[u];i != -1;i = arc[i].next)

            if(arc[i].c > 0 && dis[u] == dis[arc[i].to] + 1)

                break;

        if(i != -1)

        {

            sta[u] = i;

            rpath[arc[i].to] = arc[i].pre;

            u = arc[i].to;

        }

        else

        {

            if((-- cnt[dis[u]]) == 0)

                break;

            sta[u] = head[u];

            int tmp = n + n + 1;

            for(i = sta[u];i != -1;i = arc[i].next)

                if(arc[i].c > 0)

                    tmp = min(tmp,dis[arc[i].to]);

            dis[u] = tmp + 1;

            cnt[dis[u]] ++;

            if(u != st)

                u = arc[rpath[u]].to;

        }

    }

    return ret;

}

int nextint()

{

    int ret;

    char ch;

    while((ch = getchar()) > '9' || ch < '0')

        ;

    ret = ch - '0';

    while((ch - getchar()) >= '0' && ch <= '9')

        ret = ret * 10 + ch - '0';

    return ret;

}

void buildgraph()

{

    int i,j;

    memset(head,-1,sizeof(head));

    num = 0;

    for(i = 0;i < n;i ++)

        build(i + n,i,1);

    for(i = 1;i <= m;i ++)

    {

        build(d[i][0],d[i][1] + n,inf);

        build(d[i][1],d[i][0] + n,inf);

    }

}

int main()

{

    int i,j;

    int a,b;

    //freopen("in.txt","r",stdin);

    while(scanf("%d",&n) != EOF)

    {

        scanf("%d",&m);

        memset(map,false,sizeof(map));

        i = 1;

        while(m --)

        {

            scanf(" (%d,%d)",&a,&b);

            //a = nextint();b = nextint();

            if(map[a][b] == false)

            {

                map[a][b] = map[b][a] = true;

                d[i][0] = a;

                d[i ++][1] = b;

            }

        }

        m = i - 1;

        int ans = inf;

        for(i = 0;i < n;i ++)

        {

            for(j = 0;j < i;j ++)

            {

                if(map[i][j] == false)

                {

                    st = i;ed = j + n;

                    buildgraph();

                    ans = min(ans,ISAP());

                }

            }

        }

        if(ans == inf)

            ans = n;

        printf("%d\n",ans);

    }

    return 0;

}

//168K	16MS

为什么那个输入函数会导致TLE呢,只是想过滤掉字符而已。下面的代码也是这样用的啊啊 啊。路过的大神求科普!!

 

邻接矩阵+ISAP:

 

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

const int N = 101;

const int inf = 0x3f3f3f3f;

int cap[N][N];

int flow[N][N];

bool flag[N][N];

int cnt[N],pre[N],dis[N],que[N];

int m,n,num,st,ed;



void re_Bfs()

{

    int front,rear,i;

    for(i = 0;i < n + n;i ++)

        dis[i] = inf,cnt[i] = 0;

    dis[ed] = 0;

    cnt[0] = 1;

    front = rear = 0;

    que[rear ++] = ed;

    while(front != rear)

    {

        int u = que[front ++];

        for(i = 0;i < n + n;i ++)

            if(flow[i][u] < cap[i][u] && dis[i] > n + n)

            {

                dis[i] = dis[u] + 1;

                cnt[dis[i]] ++;

                que[rear ++] = i;

            }

    }

}

int ISAP()

{

    int i,u,ret = 0;

    memset(pre,-1,sizeof(pre));

    memset(flow,0,sizeof(flow));

    re_Bfs();

    u = st;

    while(dis[st] < n + n)

    {

        if(u == ed)

        {

            int tmp = inf;

            for(i = ed;i != st;i = pre[i])

                tmp = min(tmp,cap[pre[i]][i] - flow[pre[i]][i]);

            for(i = ed;i != st;i = pre[i])

            {

                flow[pre[i]][i] += tmp;

                flow[i][pre[i]] -= tmp;

            }

            ret += tmp;

            u = st;

        }

        for(i = 0;i < n + n;i ++)

            if(cap[u][i] > flow[u][i] && dis[u] == dis[i] + 1)

                break;

        if(i < n + n)

        {

            pre[i] = u;

            u = i;

        }

        else

        {

            if((-- cnt[dis[u]]) == 0)

                break;

            int tmp = n + n;

            for(i = 0;i < n + n;i ++)

                if(cap[u][i] > flow[u][i] && dis[i] + 1 < tmp)

                    tmp = dis[i] + 1;

            dis[u] = tmp;

            cnt[tmp] ++;

            if(pre[u] != st)

                u = pre[u];

        }

    }

    return ret;

}

int nextint()

{

    int ret;

    char c;

    while((c = getchar()) > '9' || c < '0')

        ;

    ret = c - '0';

    while((c = getchar()) >= '0' && c <= '9')

        ret = ret * 10 + c - '0';

    return ret;

}

int main()

{

    int i,j;

    int a,b;

    while(scanf("%d%d",&n,&m) != EOF)

    {

        memset(cap,0,sizeof(cap));

        memset(flag,false,sizeof(flag));

        for(i = 0;i < n;i ++)

            cap[i + n][i] = 1;

        for(i = 1;i <= m;i ++)

        {

            a = nextint();b = nextint();

            cap[a][b + n] = inf;

            cap[b][a + n] = inf;

            flag[a][b] = flag[b][a] = true;

        }

        int ans = inf;

        for(i = 0;i < n;i ++)

            for(j = 0;j < i;j ++)

                if(flag[i][j] == false)

                {

                    st = i;ed = j + n;

                    ans = min(ans,ISAP());

                }

        if(ans == inf)

            ans = n;

        printf("%d\n",ans);

    }

    return 0;

}

//444K	47MS


 

 

你可能感兴趣的:(NetWork)