P1111 修复公路

题目背景

A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车。政府派人修复这些公路。

题目描述

给出A地区的村庄数N,和公路数M,公路是双向的。并告诉你每条公路的连着哪两个村庄,并告诉你什么时候能修完这条公路。问最早什么时候任意两个村庄能够通车,即最早什么时候任意两条村庄都存在至少一条修复完成的道路(可以由多条公路连成一条道路)

输入输出格式

输入格式:
第1行两个正整数N,M

下面M行,每行3个正整数x, y, t,告诉你这条公路连着x,y两个村庄,在时间t时能修复完成这条公路。

输出格式:
如果全部公路修复完毕仍然存在两个村庄无法通车,则输出-1,否则输出最早什么时候任意两个村庄能够通车。

输入输出样例

输入样例#1:
4 4
1 2 6
1 3 4
1 4 5
4 2 3
输出样例#1:
5
说明

N<=1000,M<=100000

x<=N,y<=N,t<=100000

题解

并查集模版。按修复完成时间排序,依次加入并查集,集合内元素为N时输出时间。

#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,f[1005],t;
struct r
{
    int x,y,t;
}a[100005];
bool cmp(const r x,const r y)
{
    return x.t<y.t;
}
int fr(int x)
{
    return f[x]==x?x:f[x]=fr(f[x]);
}
void u(int x,int y)
{
    int fx=fr(x),fy=fr(y);
    if(fx!=fy)
    {
        t--;
        f[fx]=fy;
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    t=n;
    for(int i=1;i<=m;i++)
        scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].t);
    for(int i=1;i<=n;i++)
        f[i]=i;
    sort(a+1,a+m+1,cmp);
    int time;
    for(int i=1;i<=m;i++)
    {
        u(a[i].x,a[i].y);
        if(t==1)
        {
            time=a[i].t;
            break;
        }
    }
    if(t==1) printf("%d",time); else printf("-1");
    return 0;   
}
import arcpy import os # ============================================== # 加油路线规划:XY10区域内最近加油路线 # 数据路径及参数设置 # ============================================== workspace = r"F:\Project\试题基础数据" arcpy.env.workspace = workspace arcpy.env.overwriteOutput = True # 用户信息 name_initials = "xy" student_id_tail = "10" output_prefix = f"{name_initials}{student_id_tail}" toll_rate_per_km = 0.5 # 高速公路单价(元/公里),可根据实际调整 # 创建文件地理数据库 gdb_name = f"{output_prefix}_RoutePlanning.gdb" gdb_path = os.path.join(workspace, gdb_name) if not arcpy.Exists(gdb_path): arcpy.CreateFileGDB_management(workspace, gdb_name) # 创建要素数据集,并设置投影与道路一致 dataset_name = "NetworkDataset" roads_shp = os.path.join(workspace, "道路.shp") spatial_ref = arcpy.Describe(roads_shp).spatialReference fds_path = os.path.join(gdb_path, dataset_name) if not arcpy.Exists(fds_path): arcpy.CreateFeatureDataset_management(gdb_path, dataset_name, spatial_ref) # 导入要素 inputs = { "Roads": "道路.shp", "Stations": "加油站.shp", "Communities": "小区.shp", "Districts": "区划范围.shp" } for fc_name, shp in inputs.items(): in_fc = os.path.join(workspace, shp) out_fc = os.path.join(fds_path, fc_name) if not arcpy.Exists(out_fc): arcpy.CopyFeatures_management(in_fc, out_fc) # 修正道路几何,并添加高速公路收费字段 roads_fc = os.path.join(fds_path, "Roads") arcpy.RepairGeometry_management(roads_fc) # 添加字段 Toll 元素 toll_field = "TollCost" fields = [f.name for f in arcpy.ListFields(roads_fc)] if toll_field not in fields: arcpy.AddField_management(roads_fc, toll_field, "DOUBLE") # 计算收费:高速公路收费 = Shape_Leng(km)*rate,其它类型收费=0 # 假设 Shape_Leng 单位为米 arcpy.CalculateField_management( in_table=roads_fc, field=toll_field, expression=f"(!Shape_Leng!/1000) * {toll_rate_per_km} if !类型! == '高速公路' else 0", expression_type="PYTHON3" ) # 创建网络数据集 # 创建网络数据集 network_name = "RoadNet" network_path = os.path.join(fds_path, network_name) if not arcpy.Exists(network_path): arcpy.na.CreateNetworkDataset( in_dataset=fds_path, out_name=network_name, source_feature_classes=[roads_fc], source_network_attributes=[ ["Length", "COST", "DOUBLE", "", "", "Shape_Leng", "METERS", "METERS"], ["Toll", "COST", "DOUBLE", "", "", "TollCost", "", ""] ] ) arcpy.na.BuildNetwork(network_path) # 创建路线图层(Route) route_layer = f"{output_prefix}_区域内最近加油路线" arcpy.na.MakeRouteAnalysisLayer( network_dataset=network_path, layer_name=route_layer, impedance_attribute="Length", accumulate_attributes=["Toll"] ) # 加载设施(加油站)及顺序点(小区) facilities = os.path.join(fds_path, "Stations") comm_fc = os.path.join(fds_path, "Communities") arcpy.na.AddLocations(route_layer, "Stops", comm_fc, search_tolerance="500 Meters") arcpy.na.AddLocations(route_layer, "Facilities", facilities, search_tolerance="500 Meters") # 求解路线 arcpy.na.Solve(route_layer) # 导出结果路线 routes_out = os.path.join(gdb_path, f"{output_prefix}_区域内最近加油路线") arcpy.CopyFeatures_management(route_layer + "/Routes", routes_out) # 添加字段存储总收费及总里程 for fld, ftype in [("TotalLength", "DOUBLE"), ("TotalToll", "DOUBLE")]: if fld not in [f.name for f in arcpy.ListFields(routes_out)]: arcpy.AddField_management(routes_out, fld, ftype) # 提取路线属性并写入字段 with arcpy.da.UpdateCursor(routes_out, ["Name", "Total_Length", "Toll", "TotalLength", "TotalToll"]) as ucur: for row in ucur: # NA Results 字段:Total_Length 单位米,Toll 为累计高速收费 row[3] = row[1] row[4] = row[2] ucur.updateRow(row) print("路线规划完成,结果存储于:", routes_out) 这段代码中,网络数据集创建老不成功,道路拓扑没有修正好,请重新修订代码,完成代码的任务。
最新发布
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值