这道最短路的题目上次做过,这次写时,一直错了,我实在不知道错了哪里,后来改了,直接看代码就知道我错哪里了!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1005;
const int inf=0x3f3f3f3f;
int mp[maxn][maxn];
int dist[maxn];
int h[maxn];
bool p[maxn];
int t,s,d;
int max1,max2;
void init()
{
int i,j;
for(i=0; i<=maxn; i++)
{
for(j=0; j<=maxn; j++)
{
mp[i][j]=inf;
}
}
}
int diji(int end1)
{
int i,j,pos,min3;
for(i=0; i<=max2; i++)
{
p[i]=false;
dist[i]=mp[0][i];
}
p[0]=true;
dist[0]=0;
for(i=1; i<=max2; i++)
{
min3=inf;
for(j=0; j<=max2; j++)
{
if(!p[j]&&dist[j]<min3)
{
min3=dist[j];
pos=j;
}
}
p[pos]=true;
for(j=0; j<=max2; j++)
{
if(!p[j]&&dist[j]>dist[pos]+mp[pos][j])
{
dist[j]=dist[pos]+mp[pos][j];
}
}
}
return dist[end1];
}
int main()
{
int a,b,time,i;
while(scanf("%d%d%d",&t,&s,&d)!=EOF)//这里忘记加!=EOF就一直错了
{
//max2=0;
init();
max2=0;
for(i=1; i<=t; i++)
{
scanf("%d%d%d",&a,&b,&time);
max1=max(a,b);
max2=max(max1,max2);
if(mp[a][b]>time)
{
mp[a][b]=time;
mp[b][a]=time;
}
}
for(i=1; i<=s; i++)
{
int x;
scanf("%d",&x);
mp[0][x]=0;
mp[x][0]=0;
}
int y;
int min1;
min1=inf;
for(i=1; i<=d; i++)
{
scanf("%d",&h[i]);
}
for(i=1; i<=d; i++)
{
min1=min(min1,diji(h[i]));
}
printf("%d\n",min1);
}
return 0;
}