UVA - 1198 && POJ - 1612 The Geodetic Set Problem (floyd+set+模拟)

博客介绍了如何使用Floyd算法解决POJ 1612和UVA 1198问题,该问题是关于无向图中两点间最短路径经过的点集合是否等于所有点的集合。通过模拟操作和n^4的时间复杂度实现,利用二维容器存储路径信息,并在比较路径长度时更新点集。
摘要由CSDN通过智能技术生成

题目链接:http://poj.org/problem?id=1612点击打开链接

The Geodetic Set Problem
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 760 Accepted: 264

Description

Let G = (V,E) be a connected graph without loops and multiple edges, where V and E are the vertex and edge, respectively, sets of G. For any two vertices u, v ∈ V ,the distance between vertices u and v in G is the number of edges in a shortest u-v path. A shortest path between u and v is called a u-v geodesic. Let I(u, v) denote the set of vertices such that a vertex is in I(u, v) if and only if it is in some u-v geodesic of G and, for a set S <= V , I(S) =  u,v∈SI(u, v). A vertex set D in graph G is called a geodetic set if I(D) = V . The geodetic set problem is to verify whether D is a geodetic set or not. We use Figure 3 as an example. In Figure 3, I(2, 5) = {2, 3, 4, 5} since there are two shortest paths between vertices 2 and 5. We can see that vertices 3 and 4 are lying on one of these two shortest paths respectively. However, I(2, 5) is not a geodetic set since I(2, 5) != V . Vertex set {1, 2, 3, 4, 5} is intuitively a geodetic set of G. Vertex set D = {1, 2, 5} is also a geodetic set of G since vertex 3 (respectively,vertex 4) is in the shortest path between vertices 1 and 5 (respectively, vertices 2 and 5). Thus, I(D) = V . Besides, vertex sets {1, 3, 4} and {1, 4, 5} are also geodetic sets. 
However, D = {3, 4, 5} is not a geodetic set since vertex 1 is not in I(D).

Input

The input file consists of a given graph and several test cases. The first line contains an integer n indicating the number of vertices in the given graph,where 2 <= n <= 40. The vertices of a graph are labeled from 1 to n. Each vertex has a distinct label. The following n lines represent the adjacent vertices of vertex i, i = 1, 2, ... , n. For example, the second line of the sample input indicates that vertex 1 is adjacent with vertices 2 and 3. Note that any two integers in each line are separated by at least one space. After these n lines, there is a line which contains the 

number of test cases. Each test case is shown in one line and represents a given subset D of vertices. You have to determine whether D is a geodetic set or not. 

Output

For each test case, output 'yes' in one line if it is a geodetic set or 'no' otherwise.

Sample Input

5
2 3
1 3 4
1 2 5
2 5
3 4
6
1 2 3 4 5
1 2 5
2 4
1 3 4
1 4 5
3 4 5

Sample Output

yes
yes
no
yes
yes
no

不喜欢uva的pdf就挂poj的链接了

题目的大意是给你一个无向图

然后再给你一个点集

问在这个无向图中 所给的点集中两两点的最短路经过的点的集合是否是这个无向图的全部点的集合

因为点的个数只有40

因此可以按题意来模拟操作

n^4复杂度不过分吧^-^

具体思路就是创建一个二维的容器 表示所有点两两点之间的最短路所经过的点的集合

如此一来询问的时候只要再创一个容器把所有两两点的集合装进去判断个数即可

至于二维容器的创建 因为是对所有的点对其他点的最短路径 时间条件也允许 因此用floyd

在判断map[i][j]>map[i][k]+map[k][j]时

如果更改邻接图中的路径 即同样需要将容器中的点集清空重置

如果相等 则无需清空 直接加即可

#include <set>
#include <iostream>
#include <vector>
#include <string.h>
#include <sstream>
#include <stdio.h>
using namespace std;
int mapp[44][44];
set<int > mmap[44][44];
set<int >::iterator it;
vector<int > point[44];
int main()
{
	for(int i=0;i<44;i++)
		for(int j=0;j<44;j++)
		{
			if(i!=j)
				mapp[i][j]=1111111;
			else 
				mapp[i][j]=0;
		}
	int n;
	scanf("%d",&n);
	getchar();
	for(int i=1;i<=n;i++)
	{
		char s[1111];
		gets(s);
		string mid;
		stringstream ss;
		for(int j=0;j<strlen(s);j++)
		{
			if(s[j]==' ')
			{
				if(mid!="")
				{	
					int nummid;
					ss << mid;
					ss >> nummid;
					point[i].push_back(nummid);
					mid="";
					ss.clear();
				}
			}
			else 
			{
				mid+=s[j];
			}
		}
		if(mid!="")
		{	
			int nummid;
			ss << mid;
			ss >> nummid;
			point[i].push_back(nummid);
			mid="";
			ss.clear();
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<point[i].size();j++)
		{
			//cout << point[i][j] << " ";
			mmap[i][point[i][j]].insert(i);
			mmap[i][point[i][j]].insert(point[i][j]);
			mmap[point[i][j]][i].insert(i);
			mmap[point[i][j]][i].insert(point[i][j]);
			mapp[i][point[i][j]]=mapp[point[i][j]][i]=1;
		}
		//cout << endl;
	}
	
	for(int k=1;k<=n;k++)
	{
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
			{
				if(mapp[i][j]>mapp[i][k]+mapp[k][j])
				{
					mmap[i][j].clear();
					for(it=mmap[i][k].begin();it!=mmap[i][k].end();it++)
					{
						mmap[i][j].insert(*it);
					}
					for(it=mmap[k][j].begin();it!=mmap[k][j].end();it++)
					{
						mmap[i][j].insert(*it);
					}
					mapp[i][j]=mapp[i][k]+mapp[k][j];
				}
				else if(mapp[i][j]==mapp[i][k]+mapp[k][j])
				{
					for(it=mmap[i][k].begin();it!=mmap[i][k].end();it++)
					{
						mmap[i][j].insert(*it);
					}
					for(it=mmap[k][j].begin();it!=mmap[k][j].end();it++)
					{
						mmap[i][j].insert(*it);
					}
				}
			}
	}
	//for(int i=1;i<=n;i++)
	{
		//for(int j=1;j<=n;j++)
			//cout << mapp[i][j] << " ";
		//cout << endl;
	}
	int m;
	scanf("%d",&m);
	getchar();
	for(int i=0;i<m;i++)
	{
		vector<int > re;
		char s[1111];
		gets(s);
		string mid;
		stringstream ss;
		for(int j=0;j<strlen(s);j++)
		{
			if(s[j]==' ')
			{
				if(mid!="")
				{	
					int nummid;
					ss << mid;
					ss >> nummid;
					re.push_back(nummid);
					mid="";
					ss.clear();
				}
			}
			else 
			{
				mid+=s[j];
			}
		}
		if(mid!="")
		{	
			int nummid;
			ss << mid;
			ss >> nummid;
			re.push_back(nummid);
			mid="";
			ss.clear();
		}
		set<int > ans;
		for(int j=0;j<re.size();j++)
		{
			for(int k=j+1;k<re.size();k++)
			{
				for(it=mmap[re[j]][re[k]].begin();it!=mmap[re[j]][re[k]].end();it++)
					ans.insert(*it);
			}
		}
		//for(it=ans.begin();it!=ans.end();it++)
			//cout << (*it) << endl;
		if(ans.size()==n)
			cout << "yes" << endl;
		else 
			cout << "no" << endl;

	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值