hdu3790

这道题目要考虑距离与花销的最小,并且优先满足距离最小。

所以在给dist[u]添加边时会遇到两种情况

1.dist[u]+map1[u][j]<dist[j]

很简单,直接记录cost[j],dist[j]就好了

2.dist[u]+map[u][j]==dist[j]

如果还满足cost[u]+map2[u][j]<cost[j],更新cost[j]的花销




#include <stdio.h>

#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define INF 100000000
#define N 1050
int map1[N][N];//记录距离 
int map2[N][N]; //记录路的花销 
int dist[N],cost[N];//最短路径集,花销集
int s1[N];
int n,s,t;//点个数,起始点,终点 




void dijstra()
{
int i,j;
int min,u;
for(i=0;i<n;i++)
{
if(map1[s][i]<INF)
{
dist[i]=map1[s][i];
cost[i]=map2[s][i];
}
}
dist[s]=0;
cost[s]=0;
s1[s]=1;
for(i=1;i<n;i++)
{
min=INF;
for(j=0;j<n;j++)
{
if((min>dist[j])&&(s1[j]==0))
{
u=j;
min=dist[j];
}
}


s1[u]=1;
for(j=0;j<n;j++)
{
if(dist[u]+map1[u][j]<dist[j])
{
dist[j]=dist[u]+map1[u][j];
cost[j]=cost[u]+map2[u][j];
}
else if(dist[u]+map1[u][j]==dist[j])
{
if(cost[u]+map2[u][j]<cost[j])//距离相同则刷新花销 
{
cost[j]=cost[u]+map2[u][j];
}
}
}
}
}










int main()
{
int i,j,m,x,y,c,lon;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m==0&&n==0)
{
return 0;
}


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
map1[i][j]=0;
map2[i][j]=0;
}
else
{
map1[i][j]=INF;
map2[i][j]=INF;
}
}
s1[i]=0;
dist[i]=INF;
cost[i]=INF;
}

for(i=0;i<m;i++)
{
scanf("%d%d%d%d",&x,&y,&lon,&c);
x--;y--;
if(map1[x][y]>lon)
{
map1[x][y]=lon;
map1[y][x]=lon;
map2[x][y]=c;
map2[y][x]=c;
}
else if(map1[x][y]==lon)
{
if(map2[x][y]>c)
{
map1[x][y]=lon;
map1[y][x]=lon;
map2[x][y]=c;
map2[y][x]=c;
}
}
}





scanf("%d%d",&s,&t);
s--;t--;
dijstra();
printf("%d %d\n",dist[t],cost[t]);

}

return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值