题目请戳这里

邻接表的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

poj1966Cable TV Network(无向图最小点割集 ISAP+邻接矩阵)的更多相关文章

  1. poj1966Cable TV Network——无向图最小割(最大流)

    题目:http://poj.org/problem?id=1966 把一个点拆成入点和出点,之间连一条边权为1的边,跑最大流即最小割: 原始的边权赋成inf防割: 枚举源点和汇点,直接相邻的两个点不必 ...

  2. POJ 1966 Cable TV NETWORK(网络流-最小点割集)

                                    Cable TV NETWORK The interconnection of the relays in a cable TV net ...

  3. POJ 1966:Cable TV Network(最小点割集)***

    http://poj.org/problem?id=1966 题意:给出一个由n个点,m条边组成的无向图.求最少去掉多少点才能使得图中存在两点,它们之间不连通. 思路:将点i拆成a和b,连一条a-&g ...

  4. UVA-1660 Cable TV Network (最小割)

    题目大意:给一张n个点.m条边的无向图,求最小点割集的基数. 题目分析:求无向图最小点割集的基数可以变成求最小割.考虑单源s单汇t的无向图,如果要求一个最小点集,使得去掉这个点集后图不再连通(连通分量 ...

  5. POJ 1966 Cable TV Network (无向图点连通度)

    [题意]给出一个由n个点,m条边组成的无向图.求最少去掉多少点才能使得图中存在两点,它们之间不连通. [思路]回想一下s->t的最小点割,就是去掉多少个点能使得s.t不连通.那么求点连通度就枚举 ...

  6. POJ--1966--Cable TV Network【无向图顶点连通度】

    链接:http://poj.org/problem?id=1966 题意:一个无向图,n个点,m条边,求此图的顶点连通度. 思路:顶点连通度,即最小割点集里的割点数目.一般求无向图顶点连通度的方法是转 ...

  7. POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]

    妖怪题目,做到现在:2017/8/19 - 1:41…… 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Tim ...

  8. POJ 1966 Cable TV Network(顶点连通度的求解)

                               Cable TV Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissi ...

  9. UVA1660 电视网络 Cable TV Network

    题目地址:UVA1660 电视网络 Cable TV Network 枚举两个不直接连通的点 \(S\) 和 \(T\) ,求在剩余的 \(n-2\) 个节点中最少去掉多少个可以使 \(S\) 和 \ ...

随机推荐

  1. spring管理hibernate4 transaction getCurrentSession为什么报错?

    hibernate4不支持你用hibernate3的 getcurrentSession,建议你用openSession

  2. 使用Memcache在PHP中调试方法的介绍及应用

    使用Memcache在PHP中调试方法的介绍及应用 如果我们在网络开发中,特别是大访问量的web项目开发中,为了提高响应速度,减少数据查询运算,那么我们都会选用memcahce.首先我们必须要安装,接 ...

  3. structs 拦截器

    首先,要跟大家道个歉,前一阵子为给客户个一个DEMO,忙得不可开交,所以很久没有更新Blog.提到这个DEMO我想顺便跟大家分享一下心得——如果大家希望快速开发,一个类似Struts 2这样的简单方便 ...

  4. Clash of Clans(COC)资源压缩解密

    Clash of Clans,简称为COC,中文名<部落冲突>,是ios平台上一款相当火爆的战斗策略类游戏,开发商是芬兰的supercell,据说日收入上百万美刀,创造了手游史上的一个神话 ...

  5. Android 属性动画(Property Animation) 全然解析 (下)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38092093 上一篇Android 属性动画(Property Animatio ...

  6. 查看Linux操作系统版本

      1.查看内核版本命令: [root@server1 Desktop]# cat /proc/version Linux version 2.6.32-358.el6.x86_64 (mockbui ...

  7. Entity Framework with MySQL

    Get Entity Framework: http://msdn.microsoft.com/en-us/data/ee712906 Entity Framework 6 Tools for Vis ...

  8. 读取本地文件理解FileReader对象的方法和事件以及上传按钮的美化。

    一.FileReader对象 用来把文件读入内存,并且读取文件中的数据.FileReader对象提供了异步API,使用该API可以在浏览器主线程中异步访问文件系统,读取文件中的数据. 浏览器支持情况, ...

  9. [原创] Assistant editor 取消拖拽 binding 的 UI 元素

    1. press up-right button "show the utilities" 2. press "show the Connections inspecto ...

  10. java菜鸟篇<四> ZTree入门篇

    今天准备入手ZTree,于是在百度上搜了搜,找到了开源网址和一些大神们的教程,于是乎下午开始了组织树(ZTree)的练习 初步完整的作品是这个样子的: 1.咱们要去这个工具的开源网里找下载的东西: ( ...