zoj-Swordfish-2022-5-6

图模型

在这里插入图片描述
在这里插入图片描述

zoj 1203

zoj1203
求最短路径.
当做一个平面图。
有 横 纵 坐 标 x, y 轴

最小生成树

so Gabriel wants to find out the minimal total length of the tunnel required to connect all these cites. Now he asks you to write a computer program to find out the minimal length.
在这里插入图片描述在这里插入图片描述

kruskal求最小生成树

//kruskal求最小生成树
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string.h>
using namespace std;
int f[110], n, m;
struct node
{
    int a, b;
    double c;
}t[5000];
int cmp(struct node x, struct node y)
{
    return x.c < y.c;
}
int find(int x)
{
    if (x != f[x])
        f[x] = find(f[x]);
    return f[x];
}
double krus()
{
    int i, k = 0, x, y;
    double s=0;
    for (i = 1; i < m; i++) {
        x = find(t[i].a);
        y = find(t[i].b);
        if (x != y) {
            s += t[i].c;
            k++;
            if (k == n - 1)
                break;
            f[x] = y;
        }
    }
    return s;
}
int main()
{
    int i, j, k = 0;
    double s, x[110], y[110];
    memset(x, 0, sizeof(x));
    memset(y, 0, sizeof(y));
    while (cin>>n) {
        if (n == 0)
            break;
        if (k >= 1)
            cout << endl;
        k++;
        for (i = 1; i <= n; i++) {
            cin >> x[i] >> y[i];
            f[i] = i;
        }
        m = 1;
        for (i = 1; i <= n; i++)
            for (j = 1; j < i; j++) {
                t[m].a = i;
                t[m].b = j;
                t[m++].c = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));       //计算两两间的距离
            }
        sort(t + 1, t + m, cmp);
        s = krus();
        printf("Case #%d:\nThe minimal distance is: %.2lf\n", k, s);
    }
    return 0;
}

在这里插入图片描述

zoj 2048

All highways can be used in both directions. Highways can freely cross each other.
but a driver can only switch between highways at a town that is located at the end of both highways.

The Flatopian government wants to minimize the cost of building new highways.
Thus, the least expensive highway system will be the one that minimizes the total highways length.

Input

The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built.

The first line of the input contains a single integer N (1 <= N <= 750), representing the number of towns.
The next N lines each contain two integers, xi and yi separated by a space.
These values give the coordinates of ith town (for i from 1 to N).
Coordinates will have an absolute value no greater than 10000. Every town has a unique location.

The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway.

Output

Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by a space.

If no new highways need to be built (all towns are already connected), then the output should be created but it should be empty.

加边,使得所有点都连接,用最少的公路长度。

Sample Input

1

9
1 5
0 0
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2

Sample Output

1 6
3 7
4 9
5 7
8 3

已经修好的路,边权为0,没修的,边权为最大值。

用 PointB 完成并查集和比较权重的作用
struct PointB  //专门的空间负责记录端点间的权重
{
	int parent;
	double price;
};

AC代码

#include<iostream>
#include<cmath>
using namespace std;
#define MAX_PRICE 1<<30
#define MAXN 1001
int f[1000];
struct node
{
	int x;
	int y;
	
}towns[750];

struct PointB  //专门的空间负责记录端点间的权重
{
	int parent;
	double price;
};

double map[MAXN][MAXN];
bool is_out;
PointB minimun[MAXN];      //parent表示父节点,price表示代价
int j = 0, ct = 0, i = 0, n = 0, m = 0;
bool used[MAXN];

int find(int x)
{
	if (x != f[x])
		f[x] = find(f[x]);
	return f[x];
}

void init()
{
	cin>>n;
	for (int i = 1; i <= n; i++) {
		cin >> towns[i].x>> towns[i].y;
		used[i] = false;
		minimun[i].price = MAX_PRICE;
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			map[i][j] = sqrt((towns[i].x - towns[j].x) * (towns[i].x - towns[j].x) + (towns[i].y - towns[j].y) * (towns[i].y - towns[j].y));
			map[j][i] = map[i][j];
		}
	}
	int m;
	cin >> m;
	int x, y;
	//对于已经连通的,距离为0
	for (int i = 0; i < m; i++) {
		cin >> x >> y;
		map[x][y] = 0;
		map[y][x] = 0;
	}

}
void prim()
{
	minimun[1].price = 0;
	used[1] = true;
	int now = 1;
	for (int i = 1; i < n; i++) {
		double min = MAX_PRICE;
		int next = 0;
		for (int j = 1; j <= n; j++) {
			if (!used[j]) {
				if (minimun[j].price > map[now][j]) 
				{
					minimun[j].price = map[now][j];
					minimun[j].parent = now;
				}
				if (min > minimun[j].price) {
					min = minimun[j].price;
					next = j;
				}
			}
		}
		//没有连通的才输出
		if (min != 0) {
			is_out = false;
			printf("%d %d\n", next, minimun[next].parent);
		}
		now = next;
		used[now] = true;
	}

}

int main()
{
	int t = 0;
	cin >> t;
	while (t--) {
			is_out = true;
			init();
			prim();
			if (t)
				printf("\n");
	}
	return 0;

}

拓扑排序

有先修课程和后修课程的关系。

AOV网络

课程例题
排队,必须从高到矮排。

作业: zoj 2193

2193
Window Pains
Time Limit: 2000 msMemory Limit: 65536 KB

Boudreaux likes to multitask, especially when it comes to using his computer. Never satisfied with just running one application at a time, he usually runs nine applications, each in its own window. Due to limited screen real estate, he overlaps these windows and brings whatever window he currently needs to work with to the foreground. If his screen were a 4 x 4 grid of squares, each of Boudreaux’s windows would be represented by the following 2 x 2 windows:
在这里插入图片描述
When Boudreaux brings a window to the foreground, all of its squares come to the top, overlapping any squares it shares with other windows. For example, if window 1 and then window 2 were brought to the foreground, the resulting representation would be:
在这里插入图片描述
. . . and so on . . .

Unfortunately, Boudreaux’s computer is very unreliable and crashes often. He could easily tell if a crash occurred by looking at the windows and seeing a graphical representation that should not occur if windows were being brought to the foreground correctly. And this is where you come in . . .
Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.
A single data set has 3 components:

Start line - A single line:

在这里插入图片描述
固定有一些窗口填入数字
我们发现第一个和最右边和上面的,就是四个角的数字分别为1,3,7,9 ,固定不变的
在这里插入图片描述

#include<stdio.h>
#include<string.h>
int main()
{
	int win[9][4][4];
	memset(win, 0, sizeof(win));
	int i, j, k, bi, bj;
	for (k = 0; k < 9; k++)
	{
		bi = k / 3;
		bj = k % 3;
		for (i = bi; i < bi + 2; i++)
			for (j = bj; j < bj + 2; j++)
				win[k][i][j] = k + 1;
	}
	int map[10][10], num;
	char s[11];
	while (gets(s), strcmp(s, "ENDOFINPUT"))
	{
		memset(map, 0, sizeof(map));
		for (i = 0; i < 4; i++)
			for (j = 0; j < 4; j++)
			{
				scanf("%d", &num);
				for (k = 0; k < 9; k++)
					if (win[k][i][j] && win[k][i][j] != num)
						map[num][win[k][i][j]] = 1;
			}
		for (k = 1; k <= 9; k++)
			for (i = 1; i <= 9; i++)
				for (j = 1; j <= 9; j++)
					map[i][j] = map[i][j] || (map[i][k] && map[k][j]);
		int flag = 1;
		for (i = 1; i <= 9; i++)
			for (j = 1; j <= 9; j++)
				if (map[i][j] && map[j][i])
					flag = 0;
		puts(flag ? "THESE WINDOWS ARE CLEAN" : "THESE WINDOWS ARE BROKEN");
		getchar();
		gets(s);
	}
	return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值