题目:hdu2066
就是找最短路径问题。
本来是多源起点的,但是把小草的加当作是0的话,就变成单源起点了。
这条直接用优先队列就可以了,再存路径的时候,推荐使用 vector ,更方便一点。而且存路径应该是双向的。
#include <bits/stdc++.h>
using namespace std;
const int inf=1<<30;
struct edge
{
int city,t;
bool operator <(const edge &D)const
{
return t>=D.t;
}
};
vector<edge> A[1005]; //存路径
priority_queue<edge> Q;
int T,S,D,a,b,t;
int en[1005],d[1005]; // en 存终点,d[i] 表示 0 到 i 的最短时间
bool init(int n)
{
for(int i=1; i<=D; i++)
if(n==en[i])
return true;
return false;
}
void bfs()
{
while(!Q.empty())
Q.pop();
for(int i=0; i<=1000; i++)
d[i]=inf;
edge temp,tem;
temp.city=0;
temp.t=0;
Q.push(temp);
while(!Q.empty())
{
temp=Q.top();
Q.pop();
int c=temp.city;
if(init(c))
{
cout<<temp.t<<endl;
return;
}
int si=A[c].size();
for(int i=0; i<si; i++)
{
tem.city=A[c][i].city;
tem.t=A[c][i].t+temp.t;
if(tem.t>=d[tem.city])
continue;
Q.push(tem);
d[tem.city]=tem.t;
}
}
}
int main()
{
while(cin>>T>>S>>D)
{
for(int i=0; i<=1000; i++)
A[i].clear();
edge temp;
for(int i=1; i<=T; i++)
{
cin>>a>>b>>t;
temp.city=b;
temp.t=t;
A[a].push_back(temp);
temp.city=a;
A[b].push_back(temp); //路径是双向的
}
temp.t=0;
for(int i=1; i<=S; i++)
{
cin>>a;
temp.city=a;
A[0].push_back(temp); //A[0] 存起点
}
for(int i=1; i<=D; i++)
cin>>en[i];
bfs();
}
return 0;
}