(File IO): input: danger.in output: danger.out
time limit: 1000 ms space constraints: 262144 KB specific restrictions
Goto ProblemSet
Title Description
in the same boat, the sea has
islands, numbered
, and now his task is to order in accordance with a given access
To explore this
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
This sequence can be (not necessarily consecutive) so as to minimize the total risk factor.
Enter the
first
line: two numbers,
and
first
: Line
row represents a given sequence of
islands
The first
line: each row
integers representing risk factor between islands, must be left diagonal
。
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
order to explore, to meet the predetermined sequence is a sequence of words of the sequence, the risk factor is
。
Problem-solving ideas
shortest,
,
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);
}