I - QS Network

本文介绍了一道关于QS生物间网络连接的算法题目,通过最小生成树算法解决网络搭建成本最小化问题。考虑到每只QS生物有其偏好的网络适配器品牌,且两两之间的距离不等,任务是确定构建网络的最低成本。

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

题目:
Sunny Cup 2003 - Preliminary Round
April 20th, 12:00 - 17:00
Problem E: QS Network

In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunicate with each other via networks. If two QS want to get connected, they need to buy two network adapters (one for each QS) and a segment of network cable. Please be advised that ONE NETWORK ADAPTER CAN ONLY BE USED IN A SINGLE CONNECTION.(ie. if a QS want to setup four connections, it needs to buy four adapters). In the procedure of communication, a QS broadcasts its message to all the QS it is connected with, the group of QS who receive the message broadcast the message to all the QS they connected with, the procedure repeats until all the QS’s have received the message.
A sample is shown below:

A sample QS network, and QS A want to send a message.

Step 1. QS A sends message to QS B and QS C;

Step 2. QS B sends message to QS A ; QS C sends message to QS A and QS D;

Step 3. the procedure terminates because all the QS received the message.
Each QS has its favorate brand of network adapters and always buys the brand in all of its connections. Also the distance between QS vary. Given the price of each QS’s favorate brand of network adapters and the price of cable between each pair of QS, your task is to write a program to determine the minimum cost to setup a QS network.

Input

The 1st line of the input contains an integer t which indicates the number of data sets.

From the second line there are t data sets.

In a single data set,the 1st line contains an interger n which indicates the number of QS.

The 2nd line contains n integers, indicating the price of each QS’s favorate network adapter.

In the 3rd line to the n+2th line contain a matrix indicating the price of cable between ecah pair of QS.
Constrains:
all the integers in the input are non-negative and not more than 1000.

Output

for each data set,output the minimum cost in a line. NO extra empty lines needed.

Sample Input

1
3
10 20 30
0 100 200
100 0 300
200 300 0

Sample Output

370

代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 1005
struct Node{int x,y,weight;}node[MAX * MAX];
int f[MAX];
int cost[MAX];
int find_father(int x)
{
    int r = x,temp;
    while(r != f[r]) r = f[r];
    while(x != f[x]){
        temp = f[x];
        f[x] = r;
        x = temp;
    }
    return x;
}
void combine(int a,int b)
{
    int c = find_father(a);
    int d = find_father(b);
    if(c == d) return;
    f[d] = c;
}
int kruskal(int m)
{
    int ans = 0;
    for(int i = 0;i < m;i++){
        if(find_father(node[i].x) != find_father(node[i].y)){
            combine(node[i].x,node[i].y);
            ans += node[i].weight;
        }
    }
    return ans;
}
bool cmp(Node a,Node b) {return a.weight < b.weight;}
int main()
{
    int t,n,distance,k,ans;
    cin >> t;
    while(t--){
        cin >> n;
        for(int i = 0;i < n;i++){
            cin >> cost[i];
            f[i] = i;
        }
        k = 0;
        for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++){
                cin >> distance;
                node[k].x = i;
                node[k].y = j;
                node[k++].weight = distance + cost[i] + cost[j];
            }
        }
        sort(node,node + k,cmp);
        ans = kruskal(k);
        cout << ans << endl;
    }
    return 0;
}

这道题是一道最小生成树的题,与以往题不同,这道题中的权值 = 边的权值 + 两个点的值。之后使用kruskal算法即可。

dropbear的config配置文件如下:config dropbear option PasswordAuth 'on' option RootPasswordAuth 'on' option Port '22' # option BannerFile '/etc/banner' dropbear的启动init.d文件如下:#!/bin/sh /etc/rc.common # Copyright (C) 2006-2010 OpenWrt.org # Copyright (C) 2006 Carlos Sobrinho START=50 STOP=50 SERVICE_USE_PID=1 NAME=dropbear PROG=/usr/sbin/dropbear PIDCOUNT=0 EXTRA_COMMANDS="killclients" EXTRA_HELP=" killclients Kill ${NAME} processes except servers and yourself" PATH_SSH_ALGO_VERSION="/tmp/ssh_algo_version" dropbear_start() { append_ports() { local ifname="$1" local port="$2" grep -qs "^ *$ifname:" /proc/net/dev || { append args "-p $port" return } for addr in $( ifconfig "$ifname" | sed -ne ' /addr: *fe[89ab][0-9a-f]:/d s/.* addr: *\([0-9a-f:\.]*\).*/\1/p ' ); do append args "-p $addr:$port" done } local section="$1" # check if section is enabled (default) local enabled config_get_bool enabled "${section}" enable 1 [ "${enabled}" -eq 0 ] && return 1 # verbose parameter local verbosed config_get_bool verbosed "${section}" verbose 0 # increase pid file count to handle multiple instances correctly PIDCOUNT="$(( ${PIDCOUNT} + 1))" # prepare parameters (initialise with pid file) local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid" local args="-P $pid_file" local val # A) password authentication config_get_bool val "${section}" PasswordAuth 1 [ "${val}" -eq 0 ] && append args "-s" # B) listen interface and port local port local interface config_get interface "${section}" Interface config_get interface "${interface}" ifname "$interface" config_get port "${section}" Port 22 append_ports "$interface" "$port" # C) banner file config_get val "${section}" BannerFile [ -f "${val}" ] && append args "-b ${val}" # D) gatewayports config_get_bool val "${section}" GatewayPorts 0 [ "${val}" -eq 1 ] && append args "-a" # E) root password authentication config_get_bool val "${section}" RootPasswordAuth 1 [ "${val}" -eq 0 ] && append args "-g" # F) root login config_get_bool val "${section}" RootLogin 1 [ "${val}" -eq 0 ] && append args "-w" # G) host keys config_get val "${section}" rsakeyfile [ -f "${val}" ] && append args "-r ${val}" config_get val "${section}" dsskeyfile [ -f "${val}" ] && append args "-d ${val}" # H) enable ssh session login #config_get_bool val "${section}" RemoteSSH 0 #[ "${val}" -eq 1 ] && append args "-L" # I) linux account login config_get_bool val "${section}" SysAccountLogin 1 [ "${val}" -eq 0 ] && append args "-C" # 6)Disable ipv6 socket config_get_bool val "${section}" DisableIpv6 0 [ "${val}" -eq 1 ] && append args "-6" #enable allowblankpass append args "-B" # execute program and return its exit code [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}" SERVICE_PID_FILE="$pid_file" service_start ${PROG} ${args} } keygen() { local rsa2048_enable=$(uci get system.system.rsa2048_enable) local bits=1024 if [ "$rsa2048_enable" == "true" ]; then bits=2048 fi for keytype in rsa; do # check for keys key=dropbear/dropbear_${keytype}_host_key [ -f /tmp/$key -o -s /etc/$key ] || { # generate missing keys # mkdir -p /tmp/dropbear mkdir -p /tmp/dropbear/succ_cli [ -x /usr/bin/dropbearkey ] && { /usr/bin/dropbearkey -t $keytype -f /tmp/$key -s $bits 2>&- >&- && exec /etc/rc.common "$initscript" start } & #exit 0 } done lock /tmp/.switch2jffs mkdir -p /etc/dropbear mv /tmp/dropbear/dropbear_rsa_host_key /etc/dropbear/ lock -u /tmp/.switch2jffs chown root /etc/dropbear chmod 0700 /etc/dropbear } guest_portal() { if [ ! -d "/tmp/guest_portal" ]; then mkdir -p /tmp/guest_portal mount -t tmpfs -o rw,noatime,size=2m,mode=755 none /tmp/guest_portal chown admin:1000 /tmp/guest_portal fi } keygen_ecdsa() { for keytype in ecdsa; do # check for keys key=dropbear/dropbear_${keytype}_host_key [ -f /tmp/$key -o -s /etc/$key ] || { # generate missing keys mkdir -p /tmp/dropbear [ -x /usr/bin/dropbearkey ] && { /usr/bin/dropbearkey -t $keytype -f /tmp/$key -s 521 2>&- >&- && exec /etc/rc.common "$initscript" start } & #exit 0 } done lock /tmp/.switch2jffs mkdir -p /etc/dropbear mv /tmp/dropbear/dropbear_ecdsa_host_key /etc/dropbear/ lock -u /tmp/.switch2jffs chown root /etc/dropbear chmod 0700 /etc/dropbear } start() { [ -s /etc/dropbear/dropbear_rsa_host_key ] || keygen extern_partition=$(uci get profile.@backup_restore[0].extern_partition -c "/etc/profile.d" -q) for img_type in $extern_partition; do if [ "$img_type" = "portal-logo" ] || [ "$img_type" = "portal-back" ]; then guest_portal break fi done [ -s /etc/dropbear/dropbear_ecdsa_host_key ] || keygen_ecdsa local algo_version=`uci get dropbear.dropbear.AlgoVersion` [ -z "$algo_version" ] && { algo_version=0 } if [ ! -f $PATH_SSH_ALGO_VERSION ]; then touch $PATH_SSH_ALGO_VERSION echo $algo_version > $PATH_SSH_ALGO_VERSION fi # disable all client access dropbear default [ -f "/sbin/knock_functions.sh" ] && /sbin/knock_functions.sh start include /lib/network scan_interfaces config_load "${NAME}" config_foreach dropbear_start dropbear } stop() { local pid_file pid_files pid_files=`ls /var/run/${NAME}.*.pid 2>/dev/null` [ -z "$pid_files" ] && return 1 for pid_file in $pid_files; do SERVICE_PID_FILE="$pid_file" service_stop ${PROG} && { rm -f ${pid_file} } done } killclients() { local ignore='' local server local pid # if this script is run from inside a client session, then ignore that session pid="$$" while [ "${pid}" -ne 0 ] do # get parent process id pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"` [ "${pid}" -eq 0 ] && break # check if client connection grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && { append ignore "${pid}" break } done # get all server pids that should be ignored for server in `cat /var/run/${NAME}.*.pid` do append ignore "${server}" done # get all running pids and kill client connections local skip for pid in `pidof "${NAME}"` do # check if correct program, otherwise process next pid grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || { continue } # check if pid should be ignored (servers, ourself) skip=0 for server in ${ignore} do if [ "${pid}" == "${server}" ] then skip=1 break fi done [ "${skip}" -ne 0 ] && continue # kill process echo "${initscript}: Killing ${pid}..." kill -KILL ${pid} done } 如何修改上述文件,让外部dropbear远程连接该样机时要在键盘区输入密码验证
最新发布
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值