当前位置:网站首页>[gplt] 2022 popular lover (Floyd)
[gplt] 2022 popular lover (Floyd)
2022-07-25 09:34:00 【self_ disc】
There is about an hour to write about this popular lover during the game , Burst by the network card (, The newly written code is ready for submission , Network timeout ...oms Direct card return .. Roll in again and knock again to hand in the card .. Today, I found a place where I could hand in the questions, so I adjusted it bug,ac 了 ( You still have to be careful when typing code in the evaluation system , In the future, I will write in the local compiler ,)
Title Description :

Input :

Output & Examples :

The title is a little smelly and long , Roughly explain the meaning of the topic
Yes N personal , Men and women ,a->b(a Yes b It matters , The sense of distance is num1),b->c(b Yes c It matters , The sense of distance is num2) You can have a->c(a Yes c It matters , The sense of distance is num1+num2). Because a person's heterosexual relationship is determined by the person who is the most indifferent to him , therefore A The heterosexual relationship can be transformed into A All the opposite sex are right A The value of the maximum sense of distance ( Most senseless ). The lover of the public is the one with the largest distance and the smallest distance between the opposite sex among the same-sex people ( There can be multiple ) Output the popular lover among women and the popular lover among men .
take N The relationships between individuals are connected into a directed graph ( Note that it is not an undirected graph ,a->b And b->a The sense of distance can vary ). Because everyone is required to have everything The opposite sex treats him / she The shortest distance ( Relationships can deliver ), so , It can be transformed into multi-source shortest path problem .
N<=500, The data range is not very large , You can directly consider simple and rude Floyd( The core algorithm is only five lines !).1. Find the shortest path between any two . 2. Ask everyone again / Her heterosexual relationship ( The decision with the greatest distance ) For convenience , I directly used two structure arrays to record female and male Opposite sex and oneself The maximum distance . 3. Sort them from small to large , The one with the largest and smallest distance from the opposite sex is the public lover , Output directly .
See code for details :
#include <bits/stdc++.h>
using namespace std;
struct node
{
char sex; // Gender
int idx, num; // Corresponding to subscript and maximum distance respectively ( Heterosexual reasons for him / The opposite sex she's least interested in )
} a[510], b[510], c[510]; // a Save the initial input data of everyone ,b Save female data ,c Save male
int e[510][510], cnt1, cnt2; // cnt1 Record the number of women ,cnt2 Record the number of men
bool cmp(node x, node y)
{
if (x.num == y.num)
return x.idx < y.idx;
return x.num < y.num;
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++) // floyd initialization ( Initially, there is no relationship between the two , Set to inf)
for (int j = 1; j <= n; j++)
if (i != j)
e[i][j] = 1e9;
else
e[i][j] = 0;
for (int i = 1; i <= n; i++)
{
getchar(); // Absorb the last line break
int m;
scanf("%c%d", &a[i].sex, &m);
a[i].idx = i; // Deposit subscript
for (int j = 1; j <= m; j++)
{
int x, y;
scanf("%d:%d", &x, &y);
e[i][x] = y; // i->x The distance to y
}
}
for (int k = 1; k <= n; k++) // Enumerate transit points
for (int i = 1; i <= n; i++) // Enumeration starting point
for (int j = 1; j <= n; j++) // Enumerate endpoints
if (e[i][j] > e[i][k] + e[k][j]) // Can pass k to update i,j The distance is updated i,j One of the most short circuit
e[i][j] = e[i][k] + e[k][j];
for (int i = 1; i <= n; i++) // Ask everyone for the opposite sex
{
int res = 0; // The current heterosexual relationship , To find the one with the largest distance, the initial value is the minimum , I'm going to set it to 0
int j;
for (j = 1; j <= n; j++)
{
if (a[i].sex != a[j].sex) // j And i For the opposite sex -> Update heterosexual relationship
res = max(res, e[j][i]); // Be careful ! yes j->i instead of i->j, Because it is the distance between the opposite sex and oneself
}
if (a[i].sex == 'F') // i For women , Put her subscript , Heterosexual value is stored in b
{
b[++cnt1].num = res;
b[cnt1].idx = a[i].idx;
}
else // i For men , Put his subscript , Heterosexual value is stored in c
{
c[++cnt2].num = res;
c[cnt2].idx = a[i].idx;
}
}
sort(b + 1, b + 1 + cnt1, cmp); // Sort to find the one with the smallest distance from the opposite sex
sort(c + 1, c + 1 + cnt2, cmp);
int i = 1, ok = 0;
while (b[i].num == b[1].num && i <= cnt1) // Output all the women in parallel
{
if (ok)
cout << " "; // Output format processing , There must be no extra space at the end of the line
cout << b[i].idx;
i++;
ok = 1;
}
i = 1;
ok = 0;
cout << endl;
while (c[i].num == c[1].num && i <= cnt2) // Output all juxtaposed men
{
if (ok)
cout << " ";
cout << c[i].idx;
i++;
ok = 1;
}
}The code can be a bit lengthy , But the idea is still relatively simple
ending
If there are any mistakes, please correct them ! Thank you for !
边栏推荐
- Week小结
- 基本的网络知识
- 【代码源】每日一题 算的我头都大啦
- 【代码源】每日一题 三段式
- What are stand-alone, cluster and distributed?
- Understand why we should rewrite the equals method and hashcode method at the same time + example analysis
- 变量名可以用中文?直接把人干蒙了
- 正奇边形可划分成多少区域
- 学习 Redis linux 安装Redis
- Jspdf generates PDF files. There is a problem of incomplete files. Files are downloaded in the background, but not in the foreground
猜你喜欢

对象数据如何转化成数组

浏览器访问swagger失败,显示错误ERR_UNSAFE_PORT

How to write Android switching interface with kotlin
![[GKCTF 2021]easynode](/img/f0/1daf6f83fea66fdefd55608cbddac6.png)
[GKCTF 2021]easynode

作业7.21 约瑟夫环问题与进制转换

Browser access to swagger failed with error err_ UNSAFE_ PORT

@5-1 CCF 2019-12-1 报数

@3-1 CCF 2020-09-1 称检测点查询

¥1-3 SWUST oj 942: 逆置顺序表

Publish Yum private server using nexus3 (offline intranet)
随机推荐
Go基础4
Data query language (DQL)
How to customize the title content of uni app applet (how to solve the problem that the title of applet is not centered)
Redis database foundation
[GPLT] 2022 大众情人(floyd)
[GYCTF2020]Node Game
Numpy - 数组array的构造
uni-app如何获取位置信息(经纬度)
How to deploy the jar package to the server? Note: whether the startup command has nohup or not has a lot to do with it
[GKCTF 2021]easynode
Redis数据库基础
[GYCTF2020]Ez_Express
@4-1 CCF 2020-06-1 线性分类器
MongoDB数据库文件的读与写
Jspdf generates PDF files. There is a problem of incomplete files. Files are downloaded in the background, but not in the foreground
[De1CTF 2019]SSRF Me
神经网络方法——美国波士顿房价(回归问题)
js中map()函数的使用
OC--Foundation--数组
What is cerebral fissure?