http://acm.hdu.edu.cn/showproblem.php?pid=2680
裸的有向图最短路问题 使用SPFA + 邻接表 有一个小地方是需要将图反向。
数组实现邻接表:
struct edge{
int to;
int cost;
int next;
}adj[20010];
int head[1010],num;
void addedge(int from, int to, int cost){
adj[num].to = to;
adj[num].cost = cost;
adj[num].next = head[from];
head[from] = num++;
}
SPFA:
int dis[1010],vis[1010],enter[1010];
bool SPFA(int scr){
for(int i=1; i<=V; i++){
dis[i] = 1e9;
vis[i] = 0;
enter[i] = 0;
}
vis[scr] = 1;
dis[scr] = 0;
enter[scr] = 1;
queue <int> Q;
Q.push(scr);
while(!Q.empty()){
int tmp = Q.front();
Q.pop();
vis[tmp] = 0;
for(int i=head[tmp]; i!=-1; i=adj[i].next){
if(dis[adj[i].to] > dis[tmp] + adj[i].cost){
dis[adj[i].to] = dis[tmp] + adj[i].cost;
if(!vis[adj[i].to]){
vis[adj[i].to] = 1;
enter[adj[i].to]++;
if(enter[adj[i].to] >= V)
return false;
Q.push(adj[i].to);
}
}
}
}
return true;
}
主函数:
int main(){
// freopen("in.txt", "r", stdin);
while(scanf("%d%d%d",&V,&E,&key) == 3){
memset(head, -1, sizeof(head));
num = 0;
for(int i=0; i<E; i++){
int from, to, cost;
scanf("%d%d%d",&from,&to,&cost);
addedge(to, from, cost);
}
scanf("%d",&goodcnt);
for(int i=0; i<goodcnt; i++)
scanf("%d",&id[i]);
int ans = 1e9;
SPFA(key);
for(int i=0; i<goodcnt; i++)
ans = min(ans, dis[id[i]]);
if(ans == 1e9) ans = -1;
printf("%d\n",ans);
}
return 0;
}