地址:
点击打开链接
这个题目,我用floyd,我知道可能会有超时间的问题,但是为了熟悉一边,我还是写了,但是发现re了,百思不得其解。求大神指点一下,Orz
代码如下:
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
#define MAX 1500
const int INF= 471604252;
int Map[MAX][MAX];
//int Path[MAX][MAX];
int n ,m,s ,limit_bus_num,from, to;
void floyd(){
for(int k = 1 ; k <= n ; k ++)
{
for(int i = 1 ; i<= n ; i ++)
{
for(int j =1 ; j <=n ; j++)
{
int time = Map[i][k]+Map[k][j];
//cout <<i <<" to "<<j<<" with "<< k <<" "<< Map[i][j]<<" " << time <<endl;
if(Map[i][j]>time)
{
Map[i][j] = time ;
// Path[i][j]= Path[i][k];
}
}
}
}
}
int main(){
while(~scanf("%d%d%d",&n,&m,&s))
{
memset(Map,INF,sizeof(Map));
int a,b,time ;
for(int i =1 ; i <= m ; i ++)
{
scanf("%d%d%d",&a,&b,&time);
if(Map[a][b]>time)
{
Map[a][b]= time ;
}
}
for(int i = 1;i<= m; i ++)
Map[i][i] = 0 ;
floyd();
scanf("%d",&limit_bus_num);
int low =471604252 ,tt;
while(limit_bus_num--)
{
scanf("%d",&tt);
if(Map[tt][s]<low)
{
low = Map[tt][s];
}
}
if(low==471604252)
{
printf("-1\n");
}else{
printf("%d\n",low);
}
}
}