1 条题解

  • 0
    @ 2021-6-15 9:42:35

    C++ :

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define N 101
    const int inf_=99999999;
    using namespace std;
    int n,k,m,s,t,u,v,d;
    int c[N],ma[N][N],a[N][N];
    int main()
    {
    	cin>>n>>k>>m>>s>>t;
    	for(int i=1;i<=n;i++)
    	  cin>>c[i];
    	  
    	for(int i=1;i<=k;i++)
    		for(int j=1;j<=k;j++)		
    			cin>>ma[i][j];
    		
    	
    	
    
    	for(int i=1;i<=n;i++)
    	  	for(int j=1;j<=n;j++)
    	  		a[i][j]=inf_;
    	
    	for(int i=1;i<=n;i++)
    	  a[i][i]=0;
    	  
    	  
    	  
    	for(int i=1;i<=m;i++)
    	{
    		cin>>u>>v>>d;
    		if(ma[c[u]][c[v]]==0)
    			if(d<a[v][u]) a[v][u]=d;
    		if(ma[c[v]][c[u]]==0)
    			if(d<a[u][v]) a[u][v]=d;
    	}
    	
        /*for(int i=1;i<=n;i++)
    	  	for(int j=1;j<=n;j++)
    	  		cout<<a[i][j];	//*/
    	
    	
    	for(int k=1;k<=n;k++)
    	  for(int i=1;i<=n;i++)
    	  	for(int j=1;j<=n;j++)
    	  		if(a[i][j]>a[i][k]+a[k][j])
    	  			a[i][j]=a[i][k]+a[k][j];
    	  			
    	/*for(int i=1;i<=n;i++)
    	  	for(int j=1;j<=n;j++)
    	  		cout<<a[i][j];	//*/
    	  			
    	  			
    	if(a[s][t]>10000000)cout<<-1;
    	else cout<<a[s][t];
    	return 0;
    
    }
    
    
    
    

    Pascal :

    var
     n,k,m,b,e,i,j,o,x,y,t:longint;
     aa,f:array[0..1000,0..1000] of longint;
     a:array[0..1000] of longint;
    begin
     readln(n,k,m,b,e);
     for i:=1 to n do read(a[i]);readln;
     for i:=1 to k do
    begin
     for j:=1 to k do
    begin
     read(aa[i,j]);
    end;
     readln;
    end;
     for i:=0 to 1000 do for j:=0 to 1000 do f[i,j]:=1000000000;
     for i:=1 to m do
    begin
     readln(x,y,t);
     if t<f[x,y] then f[x,y]:=t;
    end;
     for o:=1 to n do
     for i:=1 to n do
     for j:=1 to n do
     if (aa[a[j],a[i]]=0) and (f[i,j]>f[i,o]+f[o,j]) then f[i,j]:=f[i,o]+f[o,j];
    if f[b,e]=1000000000 then f[b,e]:=-1;
    writeln(f[b,e]);
    end.
    
    • 1

    信息

    ID
    303
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    4
    已通过
    1
    上传者