标签:ace car 12px 最短路径 不能 esc hdu img 数据
6 2 3 1 3 5 1 4 7 2 8 12 3 8 4 4 9 12 9 10 2 1 2 8 9 10
9
最短路径问题,没有负值,dijkstra算法,优先队列实现
dijkstra思想就在这个图片里面
附上代码:
#include <stdio.h>
#include <string.h>
#include <queue>
#define inf 0x3fffffff
using namespace std;
int map[1005][1005],vis[1005],min_road,max_road;
struct node
{
int time,pos;
friend bool operator<(node a,node b)
{
return a.time>b.time;
}
};
priority_queue<node>S;
int dijkstra(int star,int end)
{
node temp;
int x=inf;
temp.pos=star,temp.time=0;
S.push(temp);
while(!S.empty())
{
node temp1;
temp1=temp=S.top();
S.pop();
vis[temp.pos]=1;
if(temp.pos==end)
{
x=temp.time;
break;
}
for(int i=min_road;i<=max_road;i++)
{
if(!vis[i]&&map[temp.pos][i]<1000000)
{
temp.time=map[temp.pos][i]+temp.time;
temp.pos=i;
S.push(temp);
}
temp=temp1;
}
}
return x;
}
int main()
{
int t,s,d;
while(scanf("%d %d %d",&t,&s,&d)!=EOF)
{
min_road=inf,max_road=-inf;
memset(map,100,sizeof(map));
for(int i=0;i<t;i++)
{
int a,b,time;
scanf("%d %d %d",&a,&b,&time);
if(map[a][b]>time)//可能有多种路径选取最小的
map[a][b]=map[b][a]=time;
if(a>max_road)//max_road,min_road只为了缩短一点点时间
max_road=a;
if(b>max_road)
max_road=b;
if(a<min_road)
min_road=a;
if(b<min_road)
min_road=b;
}
int star[1005],min_time=inf;
memset(star,0,sizeof(star));
for(int i=0;i<s;i++)
scanf("%d",&star[i]);
for(int i=0;i<d;i++)
{
int end;
scanf("%d",&end);
for(int j=0;j<s;j++)
{
while(!S.empty())
S.pop();
memset(vis,0,sizeof(vis));
int temp=dijkstra(star[j],end);
if(min_time>temp)
min_time=temp;
}
}
printf("%d\n",min_time);
}
return 0;
}标签:ace car 12px 最短路径 不能 esc hdu img 数据
原文地址:http://www.cnblogs.com/jhcelue/p/6763509.html