Party (Standard IO)

本文探讨了N头牛参加农场派对并返回家的最短路径问题,通过构建有向图并使用SPFA算法正反两次计算,找出所有牛往返路径中最长的那条路径。

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

题意/Description:

        N头牛要去参加一场在编号为x(1<=x<=n)的牛的农场举行的派对(1<=N<=1000),有M(1<=m<=100000)条有向道路,每条路长ti(1<=ti<=100);

  每头牛都必须参加完派对后回到家,每头牛都会选择最短路径,求这n个牛的最短路径(一个来回)中最长的一条的长度。特别提醒:可能有权值不同的重边。


读入/Input

        第1行: N,M,X;

  第2~m+1行: Ai,Bi,Ti,表示有一条从Ai到Bi的路,长度为Ti.


输出/Output

        最长最短路的长度。


题解/solution

        正和反各做一遍SPFA,用一个数组累加,找最大值。


代码/Code

const
  maxE=20001;
  maxV=200001;
type
  arr=record
    x,y,w:longint;
    next:longint;
  end;

var
  nm,n,m,xy,max:longint;
  tu:array [0..maxV] of arr;
  ls,v,d,f,ans:array [0..maxE] of longint;

procedure add(o,p,op:longint);
begin
  inc(nm);
  with tu[nm] do
    begin
      x:=o; y:=p; w:=op;
      next:=ls[o];
      ls[o]:=nm;
    end;
end;

procedure spfa;
var
  he,ta,i,j:longint;
begin
  he:=0; ta:=1;
  v[xy]:=1; d[1]:=xy; f[xy]:=0;
  repeat
    inc(he);
    j:=d[he];
    i:=ls[j];
    while i<>0 do
      with tu[i] do
        begin
          if f[x]+w<f[y] then
            begin
              f[y]:=f[x]+w;
              if v[y]=0 then
                begin
                  inc(ta);
                  v[y]:=1;
                  d[ta]:=y;
                end;
            end;
          i:=next;
        end;
    v[j]:=0;
  until he=ta;
end;

procedure init1;
var
  i,xx,yy,zz:longint;
begin
  readln(n,m,xy);
  for i:=1 to m do
    begin
      readln(xx,yy,zz);
      add(xx,yy,zz);
    end;
  fillchar(v,sizeof(v),0);
  fillchar(d,sizeof(d),0);
  fillchar(f,sizeof(f),$7f div 3);
  spfa;
end;

procedure init2;
var
  i,t:longint;
begin
  fillchar(ls,sizeof(ls),0);
  for i:=1 to nm do
    with tu[i] do
      begin
        t:=x; x:=y; y:=t;
        next:=ls[x];
        ls[x]:=i;
      end;
  fillchar(v,sizeof(v),0);
  fillchar(d,sizeof(d),0);
  fillchar(f,sizeof(f),$7f div 3);
end;

procedure main;
var
  i:longint;
begin
  max:=0;
  for i:=1 to n do
    ans[i]:=f[i];
  init2;
  spfa;
  for i:=1 to n do
    if max<ans[i]+f[i] then max:=ans[i]+f[i];
  write(max);
end;

begin
  init1;
  main;
end.


System.IO.FileNotFoundException: Could not find file "/Users/zhoufeng/mmzhu/ET-release8.1/Unity/Assets/Bundles/Code/Unity.Model.dll.bytes" File name: '/Users/zhoufeng/mmzhu/ET-release8.1/Unity/Assets/Bundles/Code/Unity.Model.dll.bytes' at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x0019e] in <ffede847aa21468d8e163d4cea4bee49>:0 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize) [0x00000] in <ffede847aa21468d8e163d4cea4bee49>:0 at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int) at System.IO.File.ReadAllBytes (System.String path) [0x00000] in <ffede847aa21468d8e163d4cea4bee49>:0 ET.CodeLoader.Start () [0x000d8] /Users/zhoufeng/mmzhu/ET-release8.1/Unity/Assets/Scripts/Loader/CodeLoader.cs:62 ET.Init.StartAsync () [0x001e0] /Users/zhoufeng/mmzhu/ET-release8.1/Unity/Assets/Scripts/Loader/MonoBehaviour/Init.cs:41 ET.ETTask.GetResult () [0x00030] /Users/zhoufeng/mmzhu/ET-release8.1/Unity/Assets/Scripts/ThirdParty/ETTask/ETTask.cs:126 ET.ETTask.InnerCoroutine () [0x00018] /Users/zhoufeng/mmzhu/ET-release8.1/Unity/Assets/Scripts/ThirdParty/ETTask/ETTask.cs:71 UnityEngine.Debug:LogError (object) ET.UnityLogger:Error (string) (at Assets/Scripts/Loader/UnityLogger.cs:33) ET.Log:Error (System.Exception) (at Assets/Scripts/Core/World/Module/Log/Log.cs:76) ET.AsyncETVoidMethodBuilder:SetException (System.Exception) (at Assets/Scripts/ThirdParty/ETTask/AsyncETVoidMethodBuilder.cs:33) ET.ETTask/<InnerCoroutine>d__10:MoveNext () (at Assets/Scripts/ThirdParty/ETTask/ETTask.cs:71) ET.StateMachineWrap`1<ET.ETTask/<InnerCoroutine>d__10>:Run () (at Assets/Scripts/ThirdParty/ETTask/StateMachineWrap.cs:56) ET.ETAsyncTaskMethodBuilder:SetException (System.Exception) (at Assets/Scripts/ThirdParty/ETTask/AsyncETTaskMethodBuilder.cs:35) ET.Init/<StartAsync>d__1:MoveNext () (at Assets/Scripts/Loader/MonoBehaviour/Init.cs:41) ET.StateMachineWrap`1<ET.Init/<StartAsync>d__1>:Run () (at Assets/Scripts/ThirdParty/ETTask/StateMachineWrap.cs:56) ET.ETTask:SetResult () (at Assets/Scripts/ThirdParty/ETTask/ETTask.cs:145) ET.ETAsyncTaskMethodBuilder:SetResult () (at Assets/Scripts/ThirdParty/ETTask/AsyncETTaskMethodBuilder.cs:47) ET.ResourcesComponent/<CreatePackageAsync>d__2:MoveNext () (at Assets/Scripts/Loader/Resource/ResourcesComponent.cs:85) ET.StateMachineWrap`1<ET.ResourcesComponent/<CreatePackageAsync>d__2>:Run () (at Assets/Scripts/ThirdParty/ETTask/StateMachineWrap.cs:56) System.Threading.Tasks.TaskCompletionSource`1<object>:TrySetResult (object) YooAsset.AsyncOperationBase:SetFinish () (at ./Library/PackageCache/com.tuyoogame.yooasset@2.1.1/Runtime/OperationSystem/AsyncOperationBase.cs:111) YooAsset.OperationSystem:Update () (at ./Library/PackageCache/com.tuyoogame.yooasset@2.1.1/Runtime/OperationSystem/OperationSystem.cs:83) YooAsset.YooAssets:Update () (at ./Library/PackageCache/com.tuyoogame.yooasset@2.1.1/Runtime/YooAssets.cs:84) YooAsset.YooAssetsDriver:Update () (at ./Library/PackageCache/com.tuyoogame.yooasset@2.1.1/Runtime/YooAssetsDriver.cs:13) 报错
最新发布
07-02
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值