对图论的理解floyd算法hdu1690

本文提供了一道关于最短路径算法的题解,详细介绍了如何使用Floyd算法解决公交车站间最小费用的问题。通过计算任意两个站点间的距离,再依据特定条件确定费用,并利用Floyd算法优化费用矩阵。

Bus System

题目链接:                                                           http://acm.hdu.edu.cn/showproblem.php?pid=1690


View Code
# include<stdio.h>
# include<string.h>
#define Max 0x7fffffffffffff///第一不用“;”,第二,这是64位,是个很大的数
__int64 L1,L2,L3,L4,C1,C2,C3,C4;///这里尽量用__int64
__int64 map[120][120]; ///
__int64 point[120]; ///
int n,m;
__int64 s,e,len; ///
int main()
{
int k1,ncase,i,j,k;
scanf("%d",&ncase);
for(k1=1;k1<=ncase;k1++)
{
scanf("%I64d%I64d%I64d%I64d",&L1,&L2,&L3,&L4);
scanf("%I64d%I64d%I64d%I64d",&C1,&C2,&C3,&C4);
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%I64d",&point[i]);
for(i=0;i<n;i++) ////这里就是算出任意2站点之间的费用的地方,存在了map[][]中;
{
map[i][i]=0;
for(j=i+1;j<n;j++)
{
len=point[i]-point[j];
if(len<0)
len=-len;
if(len<=L1)
{
map[i][j]=C1;///这里尽量分开写;
map[j][i]=C1;
}
else if(len<=L2)
{
map[i][j]=C2;
map[j][i]=C2;
}
else if(len<=L3)
{
map[i][j]=C3;
map[j][i]=C3;
}
else if(len<=L4)
{
map[i][j]=C4;
map[j][i]=C4;
}
else
{
map[i][j]=Max;
map[j][i]=Max;
}
}
}
for(k=0;k<n;k++) ////floyd的算法的,很简短,3个for循环,但能够把map[][]全部更新到最优
for(j=0;j<n;j++)
for(i=0;i<n;i++)
if(map[j][i]>map[j][k]+map[k][i])///这就是仅需处理的地方
map[j][i]=map[j][k]+map[k][i];
printf("Case %d:\n",k1);
for(i=1;i<=m;i++)
{
scanf("%I64d%I64d",&s,&e);
if(map[s-1][e-1]==Max) ////由于上面是从0开始的,这里要减1;
printf("Station %I64d and station %I64d are not attainable.\n",s,e);
else
printf("The minimum cost between station %I64d and station %I64d is %I64d.\n",s,e,map[s-1][e-1]);
}
}
return 0;
}
 
 

  

转载于:https://www.cnblogs.com/world-ding/articles/2136947.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值