第六十二题 UVA1660 电视网络 Cable TV Network

The interconnection of the relays in a cable TV network is bi-directional. The network is connected if there is at least one interconnection path between each pair of relays present in the network. Otherwise the network is disconnected. An empty network or a network with a single relay is considered connected. The safety factor f of a network with n relays is: 1. n, if the net remains connected regardless the number of relays removed from the net. 2. The minimal number of relays that disconnect the network when removed. Figure 1. Cable TV networks For example, consider the nets from figure 1, where the circles mark the relays and the solid lines correspond to interconnection cables. The network (a) is connected regardless the number of relays that are removed and, according to rule (1), f = n = 3. The network (b) is disconnected when 0 relays are removed, hence f = 0 by rule (2). The network (c) is disconnected when the relays 1 and 2 or 1 and 3 are removed. The safety factor is 2. Input Write a program that reads several data sets from a text file and computes the safety factor for the cable networks encoded by the data sets. Each data set starts with two integers: 0 ≤ n ≤ 50, the number of relays in the net, and m, the number of cables in the net. Follow m data pairs (u, v), u < v, where u and v are relay identifiers (integers in the range 0 . . . n − 1). The pair (u, v) designates the cable that interconnects the relays u and v. The pairs may occur in any order. Except the (u, v) pairs, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct. Output For each data set, the program prints on the standard output, from the beginning of a line, the safety factor of the encoded net. Note about the sample: The first data set encodes an empty network, the second data set corresponds to a network with a single relay, and the following three data sets encode the nets shown in figure 1. Sample Input 0 0 1 0 3 3 (0,1) (0,2) (1,2) 2 0 5 7 (0,1) (0,2) (1,3) (1,2) (1,4) (2,3) (3,4) Sample Output 0 1 3 0 2

本来的打算是这个寒假不复习网络流了,结果怎么就一不小心碰了网络流呢,我真是罪该万死

现在是 2020年1月27日00:57:21  啊啊啊啊啊啊 午夜一点我还在写代码,上一个真的是**了,改不出来了,从网上找了一个代码,chao了一遍

题意:求一个无向图的点连通度。

把一个点拆成一个入点和一个出点,之间连一条容量为1的有向边,表示能被用一次。最大流求最小割即可。

一些细节的东西:1.源点固定,汇点要枚举一遍,因为最小割割断以后会形成连通分量,在分割以后那个连通分量里的割会更大。

2.每次枚举重建一下图。3.从入点进,出点出,才认为是经过了一个原来的点,那么源点和汇点是不必经过的,所以一个在出点,另外一个枚举入点。

另附: 

1.  基本概念

(1)一个具有 N 个顶点的图,在去掉任意 K-1 个顶点后 (1<=K<=N) 所得的子图仍连通,而去掉 K 个顶点后的图不连通则称 G 是连通的, 那么K 称作图 G 的点连通度

(2)相应地如果至少去掉 K 条边使这个图不连通,则 K 成为图的边连通度

2.  求解思路

  •  对于求解边联通度的问题,为每条边赋权值为1,然后求确定一点作为源点,枚举此点外的每个点作为汇点求最大流。
  • 点联通度问题可以转换到边联通度问题上来,具体转换方法如下
    • 若 G 为无向图,假设有n个点:

          (1) 原 G 图中的每个顶点 v 变成两个顶点 v' 和 v+n ,顶点 v 至 v+n 有一条弧(有向边)连接,弧容量为 1;

          (2) 原 G 图中的每条边  e = uv ,连一条 u+n 到 v 的弧,再连一条 v+n 到 u 的弧,容量均为INF

          (3) A” 为源顶点, B' 为汇顶点

           注意:弧是有向边

    • 若 G 为有向图,假设有n个点:

          (1) 原 G 图中的每个顶点 v 变成两个顶点 v' 和 v+n ,顶点 v 至 v+n 有一条弧(有向边)连接,弧容量为 1;

          (2) 原 G 图中的每条弧  e = uv 变成一条有向轨 u'u"v'v" ,其中轨上的弧 u"v' 的容量为 ∞;

          (3) A” 为源顶点, B' 为汇顶点

  • 指定一个源点 A" ,枚举汇点B',求 A" 到 B' 的最大流 F

我不管写的好不好,你看不看得懂了,自己搞去吧,我是个很少很少熬夜的人,结果现在....

#include<bits/stdc++.h>
using namespace std;

struct Edge{ int v,flow,nxt; };
const int Maxn = 102;
const int INF = 0x3f3f3f3f;
vector<Edge> E,bak;
int head[Maxn];
inline void Add_Edge(int u,int v,int w) {
	bak.push_back({v,w,head[u]});
	head[u] = bak.size() - 1;
	bak.push_back({u,0,head[v]});
	head[v] = bak.size() - 1;
}

int S,T,cur[Maxn],d[Maxn],q[Maxn];

bool BFS() {
	memset(d,0,sizeof(d));
	int l = 0,r = 0;
	q[r++] = S; d[S] = 1;
	while(r > l) {
		int u = q[l++];
		for(int i=head[u]; ~i; i=E[i].nxt) {
			Edge &e = E[i];
            if(!d[e.v] && e.flow>0){
                d[e.v] = d[u] + 1;
                q[r++] = e.v;
            }
		}
	}
	return d[T];
}

int DFS(int u,int flow) {
	if(u == T || !flow) return flow;
	int ret = 0 ,x;
	for(int &i=cur[u]; ~i; i=E[i].nxt) {
		Edge &e = E[i];
		if(d[e.v] == d[u] + 1 && (x = DFS(e.v,min(e.flow,flow))) > 0) {
			ret += x; flow -= x;
			e.flow -= x; E[i^1].flow += x;
			if(!flow) break;
		}
	}
	return ret;
}

int MaxFlow() {
	int flow = 0;
	while(BFS()) {
		memcpy(cur,head,sizeof(head));
		flow += DFS(S,INF);
	}
	return flow;
}

int main(int argc,char* argv[]) {
	int n,m;
	while(~scanf("%d%d",&n,&m)) {
		memset(head,-1,sizeof(head));
		bak.clear();
		for(int i=1; i<n; i++) Add_Edge(i,i + n,1);
		for(int u,v,i=1; i<=m; i++) {
			scanf(" (%d,%d)",&u,&v);
			Add_Edge(u + n,v,INF); Add_Edge(v + n,u,INF);
		}
		int Ans = n;
		S = n;
		for(T=1; T<n; T++) {
			E = bak;
			Ans = min(Ans,MaxFlow());
		}
		printf("%d\n",Ans);
	}
	return 0;
}
发布了732 篇原创文章 · 获赞 31 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/qq_35776409/article/details/104090509
今日推荐