当前位置:网站首页>zoj-Swordfish-2022-5-6
zoj-Swordfish-2022-5-6
2022-07-24 10:11:00 【一个努力学习的萌新加油哦】
图模型


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;
}

边栏推荐
- [C language] implementation of three versions of address book small project (including source code)
- The best time to buy and sell stocks (leetcode-121)
- Learn more about the synchronized lock upgrade process [concurrent programming]
- Raspberry Pie: serial port login does not display print information
- 757. Set the intersection size to at least 2: greedy application question
- When the hot tea is out of stock, what does the new tea drink rely on to continue its life?
- 【LeeCode】获取2个字符串的最长公共子串
- 多表查询之子查询_单行单列情况
- Taurus. How to upgrade and run MVC on net6 and net7
- Boundless dialogue | participate in the live broadcast on July 25 and win the prize
猜你喜欢

MySQL query database capacity size

Knapsack problem of dynamic programming -- three lectures on knapsack (01 knapsack, complete knapsack, multiple knapsack)

757. Set the intersection size to at least 2: greedy application question
![[STM32 learning] (11) STM32 Mifare_ Use of one (S50) m1s50 (read, write, key modification, control bit interpretation)](/img/fd/4290525914b5146fe0eb653517fef9.png)
[STM32 learning] (11) STM32 Mifare_ Use of one (S50) m1s50 (read, write, key modification, control bit interpretation)

Dark king | analysis of zego low illumination image enhancement technology

Ask you to build a small program server

error: field ‘XXX’ declared as a function
![CAS principle [concurrent programming]](/img/f0/77e7e1079f70198c601b0f1e25106e.png)
CAS principle [concurrent programming]

Homologous policy solutions

When the hot tea is out of stock, what does the new tea drink rely on to continue its life?
随机推荐
缓冲区的概念真的理解么?带你揭开缓冲区的面纱~
Synchronized scope "concurrent programming"
Notes on using setupproxy
[STM32 learning] (4) press the key to control the flow light
Sub query of multi table query_ Single row and single column
Dynamic programming -- a collection of stock problems
Balance between management / business and technology
Exception: pyqtgraph requires Qt version >= 5.12 (your version is 5.9.5)
C # +opencvsharp+wpf learning notes (I)
[STM32 learning] (12) STM32 realizes LCD1602 simple static reality
Development history of the first commercial humanoid biped robot in China
NIO知识点
Uniapp uses PWA
Reading makes people improve my list
二叉树、二叉树排序树的实现及遍历
(3) Current situation of low code platform and R & D changes based on it basic components
Recursion - if the function calls itself internally, then the function is a recursive function & the effect is the same as that of the loop & the push condition return should be added, otherwise stack
Raspberry Pie:: no space left on device
How does ribbon get the default zoneawareloadbalancer?
[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus