poj3615

来源:互联网 发布:如何防噪音知乎 编辑:程序博客网 时间:2024/04/29 20:49

迪杰斯特拉算法入门题。

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>int min(int a,int b){  if(a>b) return b;  return a; }int max(int a,int b){  if(a>=b) return a;  return b;}int main(){  int n,m,t;  int i,j,k;  int dist[310][310];  int a,b,c;  int x,y;  memset(dist,-1,sizeof(dist));  scanf("%d%d%d",&n,&m,&t);  for(i=1;i<=m;i++)  {    scanf("%d%d%d",&a,&b,&c);    dist[a][b]=c;         }  for(i=1;i<=n;i++)    dist[i][i]=0;  for(k=1;k<=n;k++)  {    for(i=1;i<=n;i++)    {      if(i==k)          continue;      for(j=1;j<=n;j++)      {        if(i==k||j==k)          continue;        if(dist[i][k]==-1||dist[k][j]==-1)          continue;        if(dist[i][j]!=-1)        {          dist[i][j]=min(dist[i][j],          max(dist[i][k],dist[k][j]));        }        else        {          dist[i][j]=max(dist[i][k],dist[k][j]);        }      }    }  }  for(i=1;i<=t;i++)  {    scanf("%d%d",&x,&y);    printf("%d\n",dist[x][y]);  }  return 0;}


0 0
原创粉丝点击