PAT TOP 1013. Image Segmentation (35)

问题描述:

1013. Image Segmentation (35)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
ZHU, Jianke

Image segmentation is usually formulated as a graph partition problem, where each segment corresponds to a connected component. Moreover, each pixel is the vertex of the graph. Each edge has a weight, which is a non-negative dissimilarity between neighboring pixels. So, the goal of image segmentation is to decompose the image graph into several disconnected components, where the elements in a component are similar and the elements in the different components are dissimilar.

The components are defined as follows:

  • A component is made of a set of connected vertices;
  • Any two components have no shared vertices;
  • The dissimilarity D(C1, C2) of any two components C1 and C2 is larger than the confidence H of any of C1 and C2.
  • The dissimilarity D(C1, C2) is defined to be the minimum edge weight of all the edges connecting C1 and C2, or infinity if no such edge exists;
  • The confidence of a component CH(C), is defined to be the maximum edge weight of the minimum spanning tree of C, plus a function f(C) = c/|C| where c is a positive constant and |C| is the size of the component C;
  • A set of vertices must not be treated as a component if they can be partitioned into two or more components.

Your job is to write a program to list all the components.

Input Specification:

Each input file contains one test case. For each case, the first line contains three integers: Nv (0 < Nv <=1000), the total number of vertices (and hence the vertices are numbered from 0 to Nv-1); Ne, the total number of edges; and c, the constant in the function f(C). Then Ne lines follow, each gives an edge in the format:

V1 V2 Weight

Note: it is guaranteed that each pixel has no more than 8 neighboring pixels. The constant and all the weights are positive and are no more than 1000.

Output Specification:

For each case, list each component in a line. The vertices in a component must be printed in increasing order, separated by one space with no extra space at the beginning or the end of the line. The components must be listed in increasing order of their first vertex.

Sample Input 1:
10 21 100
0 1 10
0 3 60
0 4 90
1 2 90
1 3 50
1 4 200
1 5 86
2 4 95
2 5 5
3 4 95
3 6 15
3 7 101
4 5 500
4 6 100
4 7 101
4 8 101
5 7 300
5 8 50
6 7 90
7 8 84
7 9 34
Sample Output 1:
0 1 3 6
2 5 8
4
7 9
Sample Input 2:
7 7 100
0 1 10
1 2 61
2 3 50
3 4 200
4 5 82
5 0 200
3 6 90
Sample Output 2:
0 1
2 3 6
4 5

又是一道和 最小生成树 有关的题,这一题其实就是将 克鲁斯卡尔算法 的判断条件修改成为题设中的 置信度和差异度 的条件就能得到答案。。。

而且,最重要的是

PAT TOP 1013. Image Segmentation (35)_第1张图片

any of 是 ...中的任意一个 的意思,不要思维定势地认为,差异度要比两个组分的置信度都要大。。。

(ps.这应该是3.18前的最后一篇关于pat的blog了,希望这次能上90。关于甲级题目,如果有时间<咕咕咕>的话,我应该会整理一下发出来的。。。)

AC代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include
using namespace std;
struct node
{
	int id;
	int pid;
	int w;
	bool operator< (const node& en) const
	{
		return w>en.w;
	}
} no;
struct vec
{
	int root;
	int size;
	int mmst;
	bool visit;
} ve;
vector v;
inline int fr(int i)
{
	int ii=i;
	for(;v[i].root!=-1;i=v[i].root);
	if(ii!=i)
	v[ii].root=i;
	return i;
}
int main()
{
//	freopen("data.txt","r",stdin);
//	ios::sync_with_stdio(false);
	int n,m,c,c1,c2,w;
	scanf("%d%d%d",&n,&m,&c);

	ve.size=1;
	ve.root=-1;
	ve.mmst=0;
	ve.visit=true;
	
	v.resize(n,ve);
	
	priority_queue q;
	for(;m--;)
	{
		scanf("%d%d%d",&no.id,&no.pid,&no.w);
		q.push(no);
	}
	for(;!q.empty();)
	{
		no=q.top();
		q.pop();
		int r1=fr(no.id);
		int r2=fr(no.pid);
		if(r1!=r2)
		{
			int h1=c/v[r1].size+v[r1].mmst;
			int h2=c/v[r2].size+v[r2].mmst;
			h1=min(h1,h2);
			
			int rr1=min(r1,r2);
			int rr2=max(r1,r2);
			
			if(no.w<=h1)
			{
				v[rr1].size+=v[rr2].size;
				v[rr1].mmst=max(no.w,max(v[r1].mmst,v[r2].mmst));
				v[rr2].root=rr1;
			}
		}
	}
	vector > vr;
  	for(int i=0;i 
      

你可能感兴趣的:(PAT,TOP)