(Jizhong) 2405. The risk coefficient (danger)] [Floyed

(File IO): input: danger.in output: danger.out
time limit: 1000 ms space constraints: 262144 KB specific restrictions
Goto ProblemSet


Title Description
F J FJ in the same boat, the sea has N ( 1 < = N < = 100 ) N(1<=N<=100) islands, numbered 1.. N 1..N , and now his task is to order in accordance with a given access A 1 , A 2 , . A M A_1,A_2,….A_M To explore this M ( 2 < = M < = 10 , 000 ) M(2<=M<=10,000) islands, has been known risk factor between any two islands, let you explore to find a sequence, just to satisfy your quest sequence contains a given A 1 . . A M A_1..A_M This sequence can be (not necessarily consecutive) so as to minimize the total risk factor.


Enter the
first 1 1 line: two numbers, N N and M M
first 2.. M + 1 2..M+1 : Line i + 1 i+1 row represents a given sequence of i i islands A i A_i
The first M + 2.. N + M + 1 M+2..N+M+1 line: each row N N integers representing risk factor between islands, must be left diagonal 0 0

Output
minimum risk coefficient output to meet the requirements of


Sample input
. 3. 4
. 1
2
. 1
. 3
0. 5. 1
. 5 0 2
. 1 0 2

Sample output
7


Data range limit


Tips
we can follow 1 3 2 3 1 3 1,3,2,3,1,3 order to explore, to meet the predetermined sequence is a sequence of words of the sequence, the risk factor is ( 1 , 3 ) + ( 3 , 2 ) + ( 2 , 3 ) + ( 3 , 1 ) + ( 1 , 3 ) = 7 (1,3)+(3,2)+(2,3)+(3,1)+(1,3)=7


Problem-solving ideas
shortest, n < = 100 n<=100 F l The Y e d Floyed go from. Very simple. . .


Code

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
int n,m,a[10010],f[110][110],ans;
int main(){
    freopen("danger.in","r",stdin);
    freopen("danger.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            scanf("%d",&f[i][j]);
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i!=j&&i!=k&&j!=k&&f[i][k]+f[k][j]<f[i][j])
                    f[i][j]=f[i][k]+f[k][j];
    for(int i=2;i<=m;i++)
        ans+=f[a[i-1]][a[i]];
    printf("%d",ans);
}
Published 119 original articles · won praise 8 · views 4914

Guess you like

Origin blog.csdn.net/kejin2019/article/details/104860713