1 条题解

  • 0
    @ 2021-6-15 1:39:24

    C++ :

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    
    const int N=310,M=N*2;
    
    int head[N],nex[M],en[M],pri[M],tot;
    void addedge(int u,int v,int w) {
    	en[++tot]=v;
    	pri[tot]=w;
    	nex[tot]=head[u];
    	head[u]=tot;
    }
    
    int n,m,s,ans;
    
    queue<int>q;
    int pre[N],d[N],cost[N];
    int bfs(int s) {
    	int res=s;
    	memset(d,-1,sizeof(d));
    	memset(pre,0,sizeof(pre));
    	q.push(s);
    	d[s]=0;
    	while(!q.empty()) {
    		int u=q.front(); q.pop();
    		if(d[res]<d[u])res=u;
    		for(int k=head[u];k;k=nex[k]) {
    			int v=en[k],w=pri[k];
    			if(d[v]!=-1)continue;
    			d[v]=d[u]+w;
    			pre[v]=u;
    			cost[v]=w;
    			q.push(v);
    		}
    	}
    	return res;
    }
    
    bool set[N],inq[N];
    int ECC() {
    	int res=0;
    	memset(d,0x7f,sizeof(d));
    	for(int i=1;i<=n;i++)if(set[i])q.push(i),d[i]=0,inq[i]=1;
    	while(!q.empty()) {
    		int u=q.front(); q.pop();
    		inq[u]=0;
    		for(int k=head[u];k;k=nex[k]) {
    			int v=en[k],w=pri[k];
    			if(d[v]>d[u]+w) {
    				d[v]=d[u]+w;
    				if(inq[v])continue;
    				q.push(v);
    				inq[v]=1;
    			}
    		}
    	}
    	for(int i=1;i<=n;i++)res=max(res,d[i]);
    	return res;
    }
    
    int main() {
    	while(~scanf("%d%d",&n,&s)) {
    		memset(head,0,sizeof(head));
    		tot=0;
    		for(int i=1;i<n;i++) {
    			int u,v,w;
    			scanf("%d%d%d",&u,&v,&w);
    			addedge(u,v,w);
    			addedge(v,u,w);
    		}
    		int a=bfs(1);
    		int b=bfs(a);
    		ans=1<<30;
    		int L,R;
    		L=b;
    		do {
    			R=L;
    			set[R]=1;
    			int use=s;
    			while(use>=cost[R]&&pre[R]) {
    				use-=cost[R];
    				R=pre[R];
    				set[R]=1;
    			}
    			ans=min(ans,ECC());
    			set[L]=0;
    			L=pre[L];
    		}while(R!=a);
    		printf("%d\n",ans);
    	}
    	return 0;
    }
    

    Pascal :

    var map:array[1..300,1..300]of longint;
        d,road:array[1..300]of longint;
        hash,use:array[1..300]of boolean;
        n,maxs,m,r,ans:longint;
    procedure dushu;
      var u,v,d,i:longint;
        begin
          fillchar(map,sizeof(map),$f);
          readln(n,maxs);
          for i:=2 to n do
            begin
              readln(u,v,d);
              map[u][v]:=d;
              map[v][u]:=d;
            end;
          for i:=1 to n do map[i][i]:=0;
        end;
    procedure floyd; 
      var k,i,j:longint;
        begin
          for k:=1 to n do
            for i:=1 to n do
              for j:=1 to n do
                if map[i][k]+map[k][j]<map[i][j] then
                  map[i][j]:=map[i][k]+map[k][j];
        end;
    procedure getr;               
      var i,j:longint;
        begin
          for i:=1 to n do
            for j:=1 to n do
              if map[i][j]>r then r:=map[i][j];
        end;
    procedure add(s:longint);      
      begin
        if hash[s] then exit;
        inc(m); d[m]:=s;
        hash[s]:=true;
      end;
    procedure getd; 
      var i,j,k:longint;
        begin
          for i:=1 to n do
            for j:=i to n do
              if map[i][j]=r then
                begin
                  add(i);
                  add(j); 
                  for k:=1 to n do
                    if map[i][k]+map[k][j]=r then add(k);
                end;
        end;
    procedure solve;
      var i,j,k,u,v,now,min,maxroad,l:longint;
        begin
          ans:=1000000;
          for i:=1 to m do
            for j:=i to m do
              begin
                u:=d[i]; v:=d[j]; now:=0; maxroad:=0;
                if map[u][v]<=maxs then 
                  begin
                    fillchar(use,sizeof(use),false); 
                    for k:=1 to n do
                      begin
                        if map[u][k]+map[k][v]=map[u][v] then 
                          begin
                            if not use[k] then 
                              begin
                                inc(maxroad);
                                use[k]:=true;
                                road[maxroad]:=k; 
                              end;
                          end;
                      end;
                    for k:=1 to n do
                      begin
                        min:=100000;
                        for l:=1 to maxroad do
                          if map[k][road[l]]<min then min:=map[k][road[l]];
                        if min>now then now:=min;    
                      end;
                    if now<ans then ans:=now;
                  end;
              end;
        end;
    procedure print;
      begin
        writeln(ans);
      end;
    begin
      dushu;
      floyd;
      getr;
      getd;
      solve;
      print;
    end.
    
    • 1

    信息

    ID
    259
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者