hdu 1385 Minimum Transport Cost Floyd

本文探讨了在HDU 1385问题中,通过比较使用Dijkstra算法与Floyd算法解决路径查找问题的过程与效率提升。详细介绍了如何使用Floyd算法进行优化,并提供了相应的代码实现与实例解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In this problem,First I use dijkstra to solve it.But WA defeated me.I finally use Flyod.

The portal:http://acm.hdu.edu.cn/showproblem.php?pid=1385

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 1005
const int INF = 0x3f3f3f3f;

int cost[MAXN][MAXN];
int route[MAXN][MAXN];
int power[MAXN];

void Input(){
	int n;
	while(scanf("%d",&n),n){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				scanf("%d",cost[i]+j);
				if(cost[i][j]==-1){
					cost[i][j] = INF;				
				}
				route[i][j] = j;	
			}			
		}
		for(int i=1;i<=n;i++){
			scanf("%d",power+i);			
		}
		for(int k=1;k<=n;k++){
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					int temp = cost[i][k] + cost[k][j] + power[k] ;	
					if(temp < cost[i][j]){
						cost[i][j] = temp;
						route[i][j] = route[i][k];					
					}				
					else if(temp == cost[i][j]){
						if(route[i][j] > route[i][k]){
							route[i][j] = route[i][k];						
						}					
					}
				}
			}				
		}
		int g,h;
		while(scanf("%d %d",&g,&h),g+h+2){
	        printf("From %d to %d :\n",g,h);  
            printf("Path: %d",g);  
            int temp=g;  
            while(temp!=h)  
            {  
                printf("-->%d",route[temp][h]);  
                temp=route[temp][h];  
            }  
            printf("\n");  
            printf("Total cost : %d\n\n",cost[g][h]);   	
		}
	}
}

int main(){
	//freopen("a.in","r",stdin);
	Input();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值