题目链接: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).
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.
![](https://i-blog.csdnimg.cn/blog_migrate/79668232797aa09ccbc440fd6df3109d.jpeg)
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;
}
}