24h購物| | PChome| 登入
2013-07-31 17:15:45| 人氣2,068| 回應0 | 上一篇 | 下一篇

[UVA] 1198 - The Geodetic Set Problem

推薦 0 收藏 0 轉貼0 訂閱站台

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 $ in$ 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 $ subseteq$ V , I(S) = $displaystyle bigcup_{u,vin S}^{}$I(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) $ neq$ 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$ le$n$ le$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.

epsfbox{p2818.eps}

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

題目描述:

題目給定一張圖,根據定義 I(D) 去進行判斷,D 是一個點的集合,而 I(D) 表示從 D 中認抓兩個點出來作最短路徑,所有可能在路徑上的點集合。如果 I(D) = V 即所有點都可能在最短路徑上。

題目解法:

做一次 all-pair 最短路徑,窮舉 D 中的任兩點檢查最短路徑上有哪些點。

最後去檢查有沒有 = V。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int main() {
int n, q;
int i, j, k;
char s[105];
while(scanf("%d", &n) == 1) {
while(getchar() != '\n');
int g[50][50] = {};
for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++) {
g[i][j] = 0xffff;
}
g[i][i] = 0;
}
for(i = 1; i <= n; i++) {
gets(s);
int f = 0, tmp = 0;
for(j = 0; s[j]; j++) {
if(s[j] >= '0' && s[j] <= '9')
tmp = tmp*10 + s[j]-'0', f = 1;
else {
if(f) {
g[i][tmp] = g[tmp][i] = 1;
tmp = 0, f = 0;
}
}
}
if(f) {
g[i][tmp] = g[tmp][i] = 1;
}
}
for(k = 1; k <= n; k++)
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
g[i][j] = min(g[i][j], g[i][k]+g[k][j]);
scanf("%d", &q);
while(getchar() != '\n');
while(q--) {
gets(s);
int f = 0, tmp = 0;
int D[105], didx = 0;
for(i = 0; s[i]; i++) {
if(s[i] >= '0' && s[i] <= '9')
tmp = tmp*10 + s[i]-'0', f = 1;
else {
if(f) {
D[didx++] = tmp;
tmp = 0, f = 0;
}
}

}
if(f) {
D[didx++] = tmp;
}
int I[105] = {};
for(i = 0; i < didx; i++) {
for(j = i; j < didx; j++) {
for(k = 1; k <= n; k++) {
if(g[D[i]][k] + g[k][D[j]] == g[D[i]][D[j]])
I[k] = 1;
}
}
}
int ret = 1;
for(i = 1; i <= n; i++)
ret &= I[i];
puts(ret ? "yes" : "no");
}
}
return 0;
}
 

台長: Morris
人氣(2,068) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA] 11047 - The Scrooge Co Problem
此分類上一篇:[UVA] 925 - No more prerequisites, please!

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文