#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#define inf 10000000
using namespace std;
struct node
{
int to,weight,next,ww;
}road[100010];
int head[1500];int mark[1500];int dis[1500];int cost[1500];
int n,m,cut;
void init()
{
memset(road,0,sizeof(road));
memset(head,-1,sizeof(head));
cut=0;
for(int i=0;i<1500;i++)
{
mark[i]=0;
dis[i]=inf;
cost[i]=inf;
}
}
void add(int x,int y,int z,int w)
{
road[cut].weight=z;
road[cut].ww=w;
road[cut].to=y;
road[cut].next=head[x];
head[x]=cut++;
}
void spfa_bfs(int s)
{
queue<int>q;
int l,i;
mark[s]=1;
dis[s]=0;
cost[s]=0;
while(!q.empty()) q.pop();
q.push(s);
while(!q.empty())
{
l=q.front();
q.pop();
mark[l]=0;
for(i=head[l];i+1;i=road[i].next)
{
if(road[i].weight+dis[l]<dis[road[i].to])
{
dis[road[i].to]=road[i].weight+dis[l];
cost[road[i].to]=road[i].ww+cost[l];
if(mark[road[i].to]==0)
{
q.push(road[i].to);
mark[road[i].to]=1;
}
}
else if(road[i].weight +dis[l]==dis[road[i].to]&&cost[l]+road[i].ww<dis[road[i].to])
{
// dis[road[i].to ]=road[i].weight +dis[l];
cost[road[i].to]=road[i].ww+cost[l];
if(mark[road[i].to]==0)
{
q.push(road[i].to);
mark[road[i].to]=1;
}
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0||m==0) break;
init();
int a,b,p,d;
for(int i=0;i<m;i++)
{
scanf("%d%d%d%d",&a,&b,&d,&p);
add(a,b,d,p);
add(b,a,d,p);
}
int start,end;
scanf("%d%d",&start,&end);
spfa_bfs(start);
printf("%d %d\n",dis[end],cost[end]);
}
return 0;
}
HDU3790——最短路径问题(spfa)
最新推荐文章于 2021-03-22 22:02:50 发布