POJ 1388

Hinge Node Problem

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 497   Accepted: 312

Description

In decades, people have realized the significance of data communication. Most of the designs and analysis of communication networks usually model their topologies as graphical representations because many relevant problems of networks can be solved by using graph theoretic results. As usual, a communication network is modeled by a graph that nodes and edges in a graph correspond to the communication sites and links, respectively. A network G = (N,E) consists of a set N of nodes together with a set E of edges, representing pairs of nodes. If the pairs are considered to be unordered, then we have an undirected network and the edge joining two nodes u and v is represented by (u, v). For example, Figure 3 depicts a network G which contains 10 nodes and 16 edges. 


In a network G, the distance between two nodes u and v, denoted by dG(u, v), is the number of edges of a shortest path from u to v in G. A sequence of vertices v1, v2, ..., vk is a path from v1 to vk of length k-1 in G provided that there is an edge between vi and vi+1 for i = 1, 2, ..., k-1. If no path exists between nodes u and v, then dG(u, v) = ∞. A path is a shortest path between nodes u and v if its length is minimum among all of the paths between u and v. A network is connected if there exists a path between any two nodes. The failure of a node w means that w and all its incident edges are removed from G, and the remaining subnetwork is denoted by G-w.A node w is called a hinge node if there exist two other nodes u and v in G such that dG-w(u, v)>dG(u, v). It means that the distance between u and v is increased after w is removed from G. Thus, a hinge node can be viewed as a critical node of the corresponding network and the failure of such a node will increase the communication cost to the remaining subnetwork. For example, we consider the network G in Figure 3. The node v2 is a hinge node since dG-v2(v8, v9)=3 > dG(v8, v9)=2. Indeed, the set of hinge nodes contained in G is {v2, v3, v4, v7, v8, v10} 

Suppose that we have several networks. Each network is connected and contains at most n nodes, where 3 <= n<= 100. Assume now that you are hired to serve as a network administrator and you should analyze the communication cost. For this reason, you will be interested in finding all hinge nodes in a network. In particular, you should design a program that can efficiently calculate the total number of hinge nodes for each of the given networks. 

Input

The input file consists more than one and less than six networks (cases). Each test case starts with a positive integer n, where 3 <= n <= 100. The following n lines represents the adjacency matrix of a network G. The last case is followed by a "0" to indicate "end of input." An adjacency matrix of a network G with n nodes, denoted by A(G) = [au,v], is an n x n 0, 1-matrix such that au,v = 1 if (u, v) ∈ E, and au,v = 0 otherwise. Note that there is not any delimiter between any two elements in each line of a 0, 1-matrix. For example, the adjacency matrix of the graph in Figure 3 is shown in test case 3 of the sample input.

Output

For each test case, output the total number of hinge nodes in a line. 

Sample Input

3
010
101
010
3
011
101
110
10
0110001000
1001000111
1000001100
0100010101
0000000101
0001000001
1010000010
0111100000
0100001000
0101110000
0 

Sample Output

1
0
6 

Source

Taiwan 2001

大致题意:

在网络G中,由dG(u,v)表示的两个节点u和v之间的距离是从G到u的最短路径的边数。一系列顶点v1,v2,...... ,vk是在G中长度为k-1的v1到vk的路径,条件是对于i = 1,2,...,k-1,在vi和vi1之间存在边缘。如果节点u和v之间不存在路径,则dG(u,v)=∞。如果路径是u和v之间的所有路径中的最小长度,则路径是节点u和v之间的最短路径。如果在任何两个节点之间存在路径,则连接网络。节点w的失败意味着w及其所有入射边缘从G中移除,剩余的子网由Gw表示。如果在G中存在另外两个节点u和v,则节点w称为铰链节点dG -w(u,v)> dG(u,v)。这意味着在从G移除w之后,u和v之间的距离增加。因此,铰链节点可以被视为相应网络的关键节点,并且这样的节点的故障将增加剩余子网的通信成本。 。例如,我们考虑图3中的网络G.节点v2是铰链节点,因为dG-v2(v8,v9)= 3> dG(v8,v9)= 2。实际上,G中包含的铰链节点集是{v2,v3,v4,v7,v8,v10}

假设我们有几个网络。每个网络都连接在一起,最多包含n个节点,其中3 <= n <= 100.假设您现在被聘为网络管理员,您应该分析通信成本。因此,您将有兴趣找到网络中的所有铰链节点。特别是,您应该设计一个程序,可以有效地计算每个给定网络的铰链节点总数。


输入

输入文件包含多个且少于六个网络(个案)。每个测试用例以正整数n开始,其中3 <= n <= 100.以下n行表示网络G的邻接矩阵。最后一种情况后面跟着“0”表示“输入结束”。具有n个节点的网络G的邻接矩阵,由A(G)= [au,v]表示,是nxn 0,1-矩阵,使得au,v = 1 if(u,v)∈E,并且au ,否则v = 0。请注意,在0,1个矩阵的每一行中的任何两个元素之间没有任何分隔符。例如,图3中的图的邻接矩阵在样本输入的测试用例3中示出。

输出

对于每个测试用例,输出一行中铰链节点的总数。

解题思路:

采用弗洛伊德算法

若有中间结点,则结果加一

代码:


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN=128;
char s[MAXN][MAXN];
int g[MAXN][MAXN];
int hinge[MAXN][MAXN];
int vis[MAXN];
int main()
{
	int n;
	while(scanf("%d",&n)==1&&n){
		memset(hinge,0,sizeof(hinge));
		for(int i=1;i<=n;++i)
			scanf("%s",s[i]);
		for(int i=1;i<=n;++i)
			for(int j=1;j<=n;++j)
				g[i][j]=s[i][j-1]-'0';
		for(int k=1;k<=n;++k)
			for(int i=1;i<=n;++i)
				for(int j=1;j<=n;++j)
					if(g[i][k]&&g[k][j]){
						if(g[i][j]==0||g[i][k]+g[k][j]<g[i][j])
							hinge[i][j]=k,g[i][j]=g[i][k]+g[k][j];
						else if(g[i][k]+g[k][j]==g[i][j])
							hinge[i][j]=0;
					}					
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;++i)
			for(int j=i+1;j<=n;++j)
				vis[hinge[i][j]]=1;
		int ans=0;
		for(int i=1;i<=n;++i)
			ans+=vis[i];
		printf("%d\n",ans);
	}
	return 0;	
}
发布了158 篇原创文章 · 获赞 34 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_40421671/article/details/96607479