3 条题解

  • 1
    @ 2021-11-07 16:46:11

    #include<iostream>
    using namespace std;
    const int N=31;
    int n,a[N][N],ans=0x7f7f7f,b[N];
    void sb(int x,int y)
    {
    if(x>n)
    {
    if(y<ans)
    {
    ans=y;
    }
    return ;
    }
    if(y>ans)
    {
    return ;
    }
    for(int i=1;i<=n;i++)
    {
    if(b[i]==0)
    {
    b[i]=1;
    sb(x+1,y+a[i][x]);
    b[i]=0;
    }
    }
    }
    int main()
    {
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    for(int j=1;j<=n;j++)
    {
    cin>>a[i][j];
    }
    }
    sb(1,0);
    cout<<ans;
    return 0;
    }

  • 1
    @ 2018-09-18 21:34:34

    pascal语言
    var
    w:text;
    n,k,u,o,ans,sum,min,i,j:longint;
    a:array[1..1000,1..1000]of longint;
    b:array[1..1000]of boolean;

    procedure ss(djg:longint);
    var r:longint;
    begin
    for r:=1 to n do
    if not(b[r]) then
    begin
    b[r]:=true;
    sum:=sum+a[djg,r];
    if djg=n then begin if sum<min then min:=sum; end
    else if sum<min then ss(djg+1);
    sum:=sum-a[djg,r];
    b[r]:=false;
    end;
    end;

    begin
    read(n);
    for i:=1 to n do
    for j:=1 to n do
    read(a[i,j]);
    sum:=0;
    min:=maxlongint;
    ss(1);
    write(min);
    end.

  • 0
    @ 2020-06-22 20:54:56

    最优解

    uses math;
    var
    ans,n,i,j:longint;
    a:array[0..25,0..25] of longint;
    is:array[0..21] of boolean;
    tt:array[0..30] of int64;
    procedure dfs (k,s:longint);
    var i:longint;
    begin
    if k>n then begin ans:=min(ans,s); exit; end;
    if s+tt[k]<ans then begin
    for i:=1 to n do
    if is[i] then begin
    is[i]:=false;
    dfs(k+1,s+a[k,i]);
    is[i]:=true;
    end;
    end;
    end;
    begin
    for i:=1 to 21 do is[i]:=true;
    read(n);
    for i:=1 to n do begin tt[i]:=maxlongint; for j:=1 to n do begin read(a[i,j]); tt[i]:=min(tt[i],a[i,j]); ans:=ans+a[i,j]; end; end;
    for i:=n-1 downto 1 do tt[i]:=tt[i]+tt[i+1];
    dfs(1,0);
    write(ans);
    end.

  • 1

信息

难度
6
分类
(无)
标签
递交数
71
已通过
19
通过率
27%
上传者