Problem E: Communication

本文介绍了一种解决多组点间连接问题的方法,通过最小生成树算法寻找连接所有点的最短路径,并提供了一个C++实现案例,详细解释了算法思路和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem E: Communication

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 66  Solved: 11
[Submit][Status][Web Board]

Description

Military training has started. Many groups is training all day. But because NEU has no playground which is big enough to hold all groups, fresh boys and girls have to train in different positions. 

For easier command communication, leader decides to set several communicating lines which connect all the groups. Each line connect two groups by straight line. Obviously, it is not need to connect each two groups directly. N groups just need N-1 lines to make all groups connected indirectly.

Your task is, give you N groups' position, you should find N-1 lines that connect all the groups and the total distance of all lines is minimum.

Input

There are several test cases.

In each case, the first line is N, the number of groups. N<=1000. Then N lines, each line has two decimal or integer, which is the ith x-y coordinate.(0<=x,y<=1000).There are no two groups in one same position.

Output

Each case output a line, the minimum sum distance of N-1 lines with exactly two digits after a decimal point.

Sample Input
3
0 0
0 1
0 2
3
0 0
1 1
0 1
Sample Output
2.00
2.00
 
 
 

这道题是一道图论类型的题目,思路很简单就是从一个点出发,每次都从这个最小生成树出发找出距离这可最小生成树最近的点将这些距离累加就是结果注意:思路很简单,可是很容易超时,正常的思路写下去,会是一个O的3次方,很容易出现超时,为了避免超时,做一个数组,用来记录距离,首先从定点0出发,记录剩余的所有点到这个点的所有距离放在这个数组中,然后,每次找到了一个新的节点时,检测剩余的没有被访问过的点到这个新的节点的距离,如果距离有小于数组中已有的记录dis[i],则更新这个距离,也就是说在dis这个数组中记录的永远是剩余的点到这个最小生成树最小的距离,因此在找下一个点时只用做一层循环了每次找出这个数组中剩下的最小的值即是下一个要找的节点,依次类推下去。

 
 
 

#include <iostream>

#include <math.h>

#include <stdio.h>

using namespace std;

typedef struct

{

double x,y;

bool value;

}point;

double getdistance(point a,point b)

{

return pow(pow(a.x-b.x,2)+pow(a.y-b.y,2),0.5);

}

point p[1005];

int main()

{

int t,count,index;

double d,d1,sum,length[1001][1001],distan[1005];

while(cin>>t)

{

count = 1;

for(int i = 0; i < t; i++)

{

cin>>p[i].x>>p[i].y;

p[i].value = true;

}

for(int i=0;i<t;i++)

for(int j=0;j<t;j++)

{

length[i][j] = getdistance(p[i],p[j]);

if(i==0) distan[j] = length[i][j];

}

p[0].value = false;

sum = 0;

index = 0;

while(count<t)

{

d = 100000000;

for(int j = 0; j < t; j++)

{

if(p[j].value==true&&distan[j]<d)

{

d = distan[j];

index = j;

//cout<<d<<" "<<index<<endl;

}

}

sum += d;//cout<<index<<endl;

p[index].value = false;

count++;

for(int j = 0; j < t; j++)

{

if(p[j].value==true&&length[index][j]<distan[j])

distan[j] = length[index][j];

}

}

printf("%.2f\n",sum);

}

return 0;

}

/**************************************************************

Problem: 1115

User: FOT

Language: C++

Result: Accepted

Time:709 ms

Memory:9056 kb

****************************************************************/

 
a对应E,b对应Q,c对应T,d对应N,e对应G,f对应Z,g对应C,h对应L,i对应B,j对应A,k对应M,l对应R,m对应P,n对应U,o对应V,p对应S,q对应X,r对应K,s对应F,t对应H,u对应D,v对应I,w对应W,x对应O,y对应Y,z对应J。不分大小写,将下面一段明文根据对应,写出对应的密文The technology for securely sharing data has grown extensively in recent years. Many users are willing to share their lightweight mobile device data via social networks or the cloud. A novel matchmaking encryption primitive was proposed in CRYPTO’19, whose potential for privacy protection and data sharing security was introduced. However, matchmaking encryption technology faces challenges in flexibly realizing critical functions, such as one-to-many non-interactive scenarios, no key escrow problem, stronger security, lightweight computation and low communication overheads for mobile devices, which impede their widespread application. To achieve the above functions, we present a lightweight certificateless multi-user matchmaking encryption (LC-MUME) for mobile devices, which enhances security flexibly and performance based on standard hard assumptions and low-consumption pairing-free technology, while also avoiding one-by-one encryption for each user. The proposed LC-MUME scheme enjoys minor computation and communication overheads in a one-to-many non-interactive certificateless cryptosystem. We prove that our scheme achieves indistinguishability-based chosen-ciphertext attack (IND-CCA) security, the existential unforgeability under a chosen message attack (EU-CMA) security and anonymity-CCA security under the random oracle model. Our LC-MUME scheme outperforms the state-of-the-art schemes regarding efficiency and flexibility, as demonstrated by the performance comparison and analysis, and therefore is a practical solution for resource-constrained mobile devices.
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值