HDOJ 1233 还是畅通工程| HDOJ 1863 畅通工程 | HDOJ 1879 继续畅通工程

资料

最小生成树(MST)讲解请看:
http://www.wutianqi.com/?p=1284


HDOJ 1233 还是畅通工程:
题目传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1233

解答报告:

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
#include <iostream>
#include <algorithm>
using namespace std;
 
typedef struct Road
{
	int c1, c2, cost;
}Road;
 
bool myCompare(const Road &a, const Road &b)
{
	if(a.cost < b.cost)
		return 1;
	return 0;
}
 
Road road[5051];
int city[101];
 
int Find(int n)
{
	if(city[n] == -1)
		return n;
	return city[n] = Find(city[n]);
}
 
bool Merge(int s1, int s2)
{
	int r1 = Find(s1);
	int r2 = Find(s2);
	if(r1 == r2)
		return 0;
	if(r1 < r2)
		city[r2] = r1;
	else
		city[r1] = r2;
	return 1;
}
 
int main()
{
	//freopen("input.txt", "r", stdin);
	int n;
	while(scanf("%d", &n) && n)
	{
		int m = n*(n-1)/2;
		memset(city, -1, sizeof(city));
		for(int i=0; i<m; ++i)
			scanf("%d %d %d", &road[i].c1, &road[i].c2, &road[i].cost);
		sort(road, road+m, myCompare);
		int sum = 0, count = 0;
		for(int i=0; i<m; ++i)
		{
			if(Merge(road[i].c1, road[i].c2))
			{
				count ++;
				sum += road[i].cost;
			}
			if(count == n-1)
				break;
		}
		printf("%d\n", sum);
	}
	return 0;
}

HDOJ 1863 畅通工程
题目传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1863

好吧,这题是送你的,因为这题和1233基本上没区别。。。。
解答报告:

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
#include <iostream>
#include <algorithm>
using namespace std;
 
typedef struct Road
{
    int c1, c2, cost;
}Road;
 
bool myCompare(const Road &a, const Road &b)
{
    if(a.cost < b.cost)
        return 1;
    return 0;
}
 
Road road[5051];
int city[101];
 
int Find(int n)
{
    if(city[n] == -1)
        return n;
    return city[n] = Find(city[n]);
}
 
bool Merge(int s1, int s2)
{
    int r1 = Find(s1);
    int r2 = Find(s2);
    if(r1 == r2)
        return 0;
    if(r1 < r2)
        city[r2] = r1;
    else
        city[r1] = r2;
    return 1;
}
 
int main()
{
    //freopen("input.txt", "r", stdin);
    int n, m;// m-道路条数, n-村庄数
    while(scanf("%d %d", &m, &n) && m)
    {
        memset(city, -1, sizeof(city));
        for(int i=0; i<m; ++i)
            scanf("%d %d %d", &road[i].c1, &road[i].c2, &road[i].cost);
        sort(road, road+m, myCompare);
        int sum = 0, count = 0;
        for(int i=0; i<m; ++i)
        {
            if(Merge(road[i].c1, road[i].c2))
            {
                count ++;
                sum += road[i].cost;
            }
            if(count == n-1)
                break;
        }
        if(count == n-1)
            printf("%d\n", sum);
        else
            printf("?\n");
    }
    return 0;
}

HDOJ 1879 继续畅通工程
题目传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1879
这题币上面两题要活一些。
可以考察大家对MST的掌握。
好好分析代码,相信你能看懂的。

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
#include <iostream>
#include <algorithm>
using namespace std;
typedef struct Road
{
    int c1, c2, cost, state;
}Road;
 
bool myCompare(const Road &a, const Road &b)
{
    if(a.cost < b.cost)
        return 1;
    return 0;
}
 
Road road[5051];
int city[101];
 
int Find(int n)
{
    if(city[n] == -1)
        return n;
    return city[n] = Find(city[n]);
}
 
bool Merge(int s1, int s2)
{
    int r1 = Find(s1);
    int r2 = Find(s2);
    if(r1 == r2)
        return 0;
    if(r1 < r2)
        city[r2] = r1;
    else
        city[r1] = r2;
    return 1;
}
 
int main()
{
    //freopen("input.txt", "r", stdin);
    int n;
    while(scanf("%d", &n) && n)
    {
        int m = n*(n-1)/2;
        memset(city, -1, sizeof(city));
        int count = 0;
        for(int i=0; i<m; ++i)
        {
            scanf("%d %d %d %d", &road[i].c1, &road[i].c2, &road[i].cost, &road[i].state);
            if(road[i].state == 1)
            {
                count ++;
                Merge(road[i].c1, road[i].c2);
            }
        }
        sort(road, road+m, myCompare);
        int sum = 0;
        for(int i=0; i<m; ++i)
        {
            if(Merge(road[i].c1, road[i].c2) && road[i].state == 0)
            {
                count ++;
                sum += road[i].cost;
            }
            if(count == n-1)
                break;
        }
        printf("%d\n", sum);
    }
    return 0;
}

发布者

Tanky Woo

Tanky Woo,[个人主页:https://tankywoo.com] / [新博客:https://blog.tankywoo.com]

《HDOJ 1233 还是畅通工程| HDOJ 1863 畅通工程 | HDOJ 1879 继续畅通工程》有599个想法

  1. 博主,第三个有错误,虽然代码可以AC。测试用例:
    4
    1 2 1 1
    1 3 2 1
    2 3 4 1
    1 4 3 0
    2 4 5 0
    3 4 6 0
    很显然结果是3,但博主代码输出为0.
    原因:
    if(road[i].state == 1)
    {
    count ++;
    Merge(road[i].c1, road[i].c2);
    }
    这里没有判断已有公路是否存在环的问题,可以修改为:
    if(road[i].state == 1))
    {
    if(Merge(road[i].c1, road[i].c2)
    count ++;
    }

发表评论

电子邮件地址不会被公开。 必填项已用*标注