POJ 2485 Highways (水题入门最小生成树)

28 篇文章 0 订阅

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They’re planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.

Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. 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 length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.
Input

The first line of input is an integer T, which tells how many test cases followed.
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.
Output

For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.
Sample Input

1

3
0 990 692
990 0 179
692 179 0
Sample Output

692

题目大意
在 1~n 个城镇之间建设公路,求可以把所有城镇连接起来的最小花费,输出其中最长的边的权值。

解题思路

  1. Prim算法: Prim算法是选取一个点作为出发点,在其余点中选取距离该点最近的顶点,与出发点合并为集合,并标记已经添加过的顶点避免重复访问;之后不断往集合中添加距离现有生成树最近的点,扩大现有生成树集合,直至所有的点均被标记,现有生成树集合不再被更新,退出算法。

2.Kruskal算法:Kruskal算法是先把边递增排序,然后由小边至大边进行检索,若加入该点会成环,那么舍弃;否则现有生成树存在顶点数 +1,直至顶点数等于图所有顶点,退出算法。在该算法中,使用并查集来判断两点是否属于同一集合,即是否存在环。

代码实现

//Prim算法

#include <iostream>
#include<cstdio>
using namespace std;
#define maxv 506
#define INF (1<<26)
int edge[maxv][maxv],n;
void prim()
{
    int mincost[maxv];
    bool flag[maxv];
    for(int i=0;i<n;i++)
    {
        mincost[i]=INF;
        flag[i]=false;
    }
    mincost[0]=true;
    int res=0;
    while(true)
    {
        int v=-1;
        for(int u=0;u<n;u++)
        {
            if(!flag[u]&&(v==-1||mincost[u]<mincost[v]))
                v=u;
        }
        if(v==-1)break;
        flag[v]=true;
        res=max(res,mincost[v]);
        for(int u=0;u<n;u++)
            mincost[u]=min(mincost[u],edge[u][v]);
    }
    printf("%d\n",res);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%d",&edge[i][j]);
        prim();
    }
    return 0;
}

//kruskal算法

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 506
int n;
int pre[maxn];
bool flag[maxn];
struct node
{
    int x,y,l;
} edge[maxn*maxn];
bool cmp(node a,node b)
{
    return a.l<b.l;
}
int find_pre(int x)
{
    int r=x;
    while(pre[r]!=r)
    {
        r=pre[r];
    }
    return r;
}
bool make_union(int a,int b)
{
    int fx=find_pre(a);
    int fy=find_pre(b);
    if(fx!=fy)
    {
        pre[fx]=fy;
        return true;
    }
    return false;
}
int main()
{
    int T,v,len;
    scanf("%d",&T);
    while(T--)
    {
        memset(flag,false,sizeof(flag));
        scanf("%d",&n);
        v=0;
        for(int i=0; i<n; i++)
        {
            pre[i]=i;
            for(int j=0; j<n; j++)
            {
                scanf("%d",&len);
                if(i>j)
                {
                    edge[v].x=i;
                    edge[v].y=j;
                    edge[v].l=len;
                    v++;
                }
            }
        }
        int nowv=0;
        int maxx=0;
        sort(edge,edge+v,cmp);
        for(int i=0; i<v; i++)
        {
            if(make_union(edge[i].x,edge[i].y))
            {
                nowv++;
                if(maxx<edge[i].l)
                    maxx=edge[i].l;
                if(nowv==v-1) break;
            }

        }
        printf("%d\n",maxx);
    }
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Functional Safety Island(功能安全小岛)是一个术语,用于描述在一些特定领域中,需要将功能安全保证独立于其他工作流程、系统或模块的一片区域。功能安全指的是在系统或产品中保证安全性能,并防止可能的危险或事故。 在汽车行业中,Functional Safety Island常常指的是车辆的电子控制单元(ECU),如发动机控制模块或刹车控制模块等。这些ECU在车辆上被独立地部署,并且有严格的安全标准和要求。它们负责控制汽车的关键功能,如制动、加速和转弯等,以确保车辆在行驶过程中的安全。 Functional Safety Island在其他领域也有应用。例如,在工业自动化中,控制系统通常将功能安全独立地集成到关键过程中。这样可以确保即使在系统的其他部分出现故障或错误时,安全功能仍然能够正常运行,并防止潜在的事故发生。 Functional Safety Island的概念强调了功能安全的重要性,并提醒我们需要将安全性作为一项独立的设计和控制因素考虑。通过将功能安全独立于其他工作流程、系统或模块,可以提高系统的可靠性和安全性,减少潜在风险,并保护人们的生命和财产安全。 ### 回答2: Functional Safety Island 是指一个系统中的一个功能安全领域。在汽车、航空、医疗、工业等关键领域中,使用的系统通常需要具备高可靠性和安全性。为了实现这一目标,系统通常被划分为多个功能安全领域,每个领域被称为一个“岛屿”。 Functional Safety Island 的目的是通过将系统分割成多个独立的区域,限制错误或故障的扩散,以确保系统在发生故障时仍能保持可用和安全。每个岛屿中会包含特定的功能单元,并通过安全机制和措施来监测和管理其运行状态。 每个 Functional Safety Island 都有一定的独立性和自主性,可以在一个岛屿内独立地进行故障处理和控制。这种独立性使得系统能够更好地应对单个岛屿内部发生的错误或故障,并且在某个岛屿发生故障时,其他岛屿仍然可以正常工作,从而保证整个系统的功能连续性和安全性。 Functional Safety Island 的设计和实施需要考虑到系统的功能要求、风险评估和安全标准等因素。对于每个岛屿,会进行系统设计、故障分析、可靠性评估等工作,以确保系统的功能安全性能符合要求。 总之,Functional Safety Island 是系统安全保障的一种重要手段,通过将系统划分为多个独立的区域,限制错误或故障的扩散,提高系统的可用性和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值