spfa(或digstra)一遍过
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int p[1001][1001]; 5 int g[1001][1001]; 6 int n,m,x,y,k1,d,a,b,ans; 7 int main(){ 8 int i,k,j; 9 scanf("%d%d",&n,&m); 10 memset(g,0x3f,sizeof(g)); 11 for(i=1;i<=m;i++){ 12 scanf("%d%d%d",&x,&y,&k1); 13 p[x][y]=p[y][x]=k1; 14 g[x][y]=g[y][x]=0; 15 } 16 scanf("%d",&d); 17 for(i=1;i<=d;i++){ 18 scanf("%d%d",&x,&y); 19 g[x][y]=g[y][x]=p[x][y]; 20 } 21 for(k=1;k<=n;k++){ 22 for(i=1;i<=n;i++){ 23 for(j=1;j<=n;j++){ 24 if(g[i][j]>g[i][k]+g[k][j]){ 25 g[i][j]=g[i][k]+g[k][j]; 26 } 27 } 28 } 29 } 30 scanf("%d%d",&a,&b); 31 printf("%d",g[a][b]); 32 }
原文地址:https://www.cnblogs.com/ainiyuling/p/11149651.html
时间: 2024-10-15 10:57:03