3 2 1 2 5 6 2 3 4 5 1 3 0 0
9 11
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
int n,m,cost[1005][1005],len[1005][1005],vis[1005];//cost存贮花费,len存贮长度,vis标记
struct node
{
int pay,l,pos;
friend bool operator< (const node a,const node b)//根据题意要求,如果路径相等按照花费排序
{
if(a.l>b.l) return true;
if(b.l==a.l&&a.pay>b.pay) return true;
return false;
}
};
priority_queue<node>s;
void dijkstra(int star,int end)
{
node temp,x;
temp.pos=star,temp.l=0,temp.pay=0;
s.push(temp);
while(!s.empty())
{
temp=s.top();
s.pop();
star=temp.pos;
if(star==end)
break;
x=temp;
for(int i=1;i<=n;i++)//从1开始
{
if(!vis[i]&&len[star][i]<1000000)
{
temp.l+=len[star][i];
temp.pay+=cost[star][i];
temp.pos=i;
s.push(temp);
vis[star]=1;
}
temp=x;
}
}
printf("%d %d\n",temp.l,temp.pay);
}
int main()
{
int star,end;
while(scanf("%d %d",&n,&m)!=EOF,(m||n))
{
memset(vis,0,sizeof(vis));
memset(cost,100,sizeof(cost));
memset(len,100,sizeof(len));
for(int i=0;i<m;i++)
{
int a,b,d,p;
scanf("%d %d %d %d",&a,&b,&d,&p);
if(d<len[a][b]||(d==len[a][b]&&p<cost[a][b]))//避免重边,判断一下
len[a][b]=len[b][a]=d,cost[a][b]=cost[b][a]=p;;
}
scanf("%d %d",&star,&end);
dijkstra(star,end);
while(!s.empty())
s.pop();
}
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
hdu3790 最短路径问题(dijkstra/优先队列实现)
原文地址:http://blog.csdn.net/su20145104009/article/details/46858099